Skip to content

Commit 0f4a837

Browse files
committed
chore: address PR feedback
1 parent a731be6 commit 0f4a837

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ public static string[] ToIdentifier(this JsonSchemaType schemaType)
4949
return types.ToArray();
5050
}
5151

52+
/// <summary>
53+
/// Returns the first identifier from a string array.
54+
/// </summary>
55+
/// <param name="schemaType"></param>
56+
/// <returns></returns>
57+
public static string FirstIdentifier(this JsonSchemaType schemaType)
58+
{
59+
var identifier = schemaType.ToIdentifier();
60+
return identifier[0];
61+
}
62+
5263
#nullable restore
5364

5465
/// <summary>
@@ -77,8 +88,13 @@ public static JsonSchemaType ToJsonSchemaType(this string identifier)
7788
/// </summary>
7889
/// <param name="identifier"></param>
7990
/// <returns></returns>
80-
public static JsonSchemaType ToJsonSchemaType(this string[] identifier)
91+
public static JsonSchemaType? ToJsonSchemaType(this string[] identifier)
8192
{
93+
if (identifier == null)
94+
{
95+
return null;
96+
}
97+
8298
JsonSchemaType type = 0;
8399
foreach (var id in identifier)
84100
{
@@ -170,7 +186,7 @@ public static Type MapOpenApiPrimitiveTypeToSimpleType(this OpenApiSchema schema
170186
throw new ArgumentNullException(nameof(schema));
171187
}
172188
var isNullable = (schema.Type & JsonSchemaType.Null) == JsonSchemaType.Null;
173-
var nonNullable = (schema.Type & ~JsonSchemaType.Null).ToIdentifier().FirstOrDefault();
189+
var nonNullable = (schema.Type & ~JsonSchemaType.Null)?.FirstIdentifier();
174190

175191
var type = (nonNullable, schema.Format?.ToLowerInvariant(), isNullable) switch
176192
{

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ internal void WriteJsonSchemaKeywords(IOpenApiWriter writer)
413413
internal void WriteAsItemsProperties(IOpenApiWriter writer)
414414
{
415415
// type
416-
writer.WriteProperty(OpenApiConstants.Type, (Type & ~JsonSchemaType.Null).ToIdentifier()?.FirstOrDefault());
416+
writer.WriteProperty(OpenApiConstants.Type, (Type & ~JsonSchemaType.Null)?.FirstIdentifier());
417417

418418
// format
419419
WriteFormatProperty(writer);
@@ -651,14 +651,14 @@ Extensions is not null &&
651651
break;
652652
case OpenApiSpecVersion.OpenApi3_0 when isNullable && type.Value == JsonSchemaType.Null:
653653
writer.WriteProperty(OpenApiConstants.Nullable, true);
654-
writer.WriteProperty(OpenApiConstants.Type, JsonSchemaType.Object.ToIdentifier().FirstOrDefault());
654+
writer.WriteProperty(OpenApiConstants.Type, JsonSchemaType.Object.FirstIdentifier());
655655
break;
656656
case OpenApiSpecVersion.OpenApi3_0 when isNullable && type.Value != JsonSchemaType.Null:
657657
writer.WriteProperty(OpenApiConstants.Nullable, true);
658-
writer.WriteProperty(OpenApiConstants.Type, type.Value.ToIdentifier().FirstOrDefault());
658+
writer.WriteProperty(OpenApiConstants.Type, type.Value.FirstIdentifier());
659659
break;
660660
default:
661-
writer.WriteProperty(OpenApiConstants.Type, type.Value.ToIdentifier().FirstOrDefault());
661+
writer.WriteProperty(OpenApiConstants.Type, type.Value.FirstIdentifier());
662662
break;
663663
}
664664
}
@@ -703,7 +703,7 @@ private static void UpCastSchemaTypeToV31(JsonSchemaType type, IOpenApiWriter wr
703703
var temporaryType = type | JsonSchemaType.Null;
704704
var list = (from JsonSchemaType flag in jsonSchemaTypeValues// Check if the flag is set in 'type' using a bitwise AND operation
705705
where temporaryType.HasFlag(flag)
706-
select flag.ToIdentifier().FirstOrDefault()).ToList();
706+
select flag.FirstIdentifier()).ToList();
707707
if (list.Count > 1)
708708
{
709709
writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(s));
@@ -740,7 +740,7 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType schemaType, IOpenApiWriter
740740
if (schemaType.HasFlag(flag) && flag != JsonSchemaType.Null)
741741
{
742742
// Write the non-null flag value to the writer
743-
writer.WriteProperty(OpenApiConstants.Type, flag.ToIdentifier().FirstOrDefault());
743+
writer.WriteProperty(OpenApiConstants.Type, flag.FirstIdentifier());
744744
}
745745
}
746746
writer.WriteProperty(nullableProp, true);
@@ -753,7 +753,7 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType schemaType, IOpenApiWriter
753753
}
754754
else
755755
{
756-
writer.WriteProperty(OpenApiConstants.Type, schemaType.ToIdentifier().FirstOrDefault());
756+
writer.WriteProperty(OpenApiConstants.Type, schemaType.FirstIdentifier());
757757
}
758758
}
759759
}

src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static void ValidateDataTypeMismatch(
5656
// convert value to JsonElement and access the ValueKind property to determine the type.
5757
var valueKind = value.GetValueKind();
5858

59-
var type = schema.Type.ToIdentifier().FirstOrDefault(x => x is not null);
59+
var type = (schema.Type & ~JsonSchemaType.Null)?.FirstIdentifier();
6060
var format = schema.Format;
6161

6262
// Before checking the type, check first if the schema allows null.

0 commit comments

Comments
 (0)