Visitar URL original
fix: refactor ToIdentifier() to normalize flaggable enums by MaggieKimani1 · Pull Request #2156 · microsoft/OpenAPI.NET · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: address PR feedback
  • Loading branch information
MaggieKimani1 committed Feb 19, 2025
commit 0f4a837e0388e378c13539a4fa9ccaaf7b4a7763
20 changes: 18 additions & 2 deletions src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public static string[] ToIdentifier(this JsonSchemaType schemaType)
return types.ToArray();
}

/// <summary>
/// Returns the first identifier from a string array.
/// </summary>
/// <param name="schemaType"></param>
/// <returns></returns>
public static string FirstIdentifier(this JsonSchemaType schemaType)
{
var identifier = schemaType.ToIdentifier();
return identifier[0];
}

#nullable restore

/// <summary>
Expand Down Expand Up @@ -77,8 +88,13 @@ public static JsonSchemaType ToJsonSchemaType(this string identifier)
/// </summary>
/// <param name="identifier"></param>
/// <returns></returns>
public static JsonSchemaType ToJsonSchemaType(this string[] identifier)
public static JsonSchemaType? ToJsonSchemaType(this string[] identifier)
{
Comment thread
MaggieKimani1 marked this conversation as resolved.
Resolved
if (identifier == null)
{
return null;
}

JsonSchemaType type = 0;
foreach (var id in identifier)
{
Expand Down Expand Up @@ -170,7 +186,7 @@ public static Type MapOpenApiPrimitiveTypeToSimpleType(this OpenApiSchema schema
throw new ArgumentNullException(nameof(schema));
}
var isNullable = (schema.Type & JsonSchemaType.Null) == JsonSchemaType.Null;
var nonNullable = (schema.Type & ~JsonSchemaType.Null).ToIdentifier().FirstOrDefault();
var nonNullable = (schema.Type & ~JsonSchemaType.Null)?.FirstIdentifier();
Comment thread
MaggieKimani1 marked this conversation as resolved.
Resolved

var type = (nonNullable, schema.Format?.ToLowerInvariant(), isNullable) switch
{
Expand Down
14 changes: 7 additions & 7 deletions src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ internal void WriteJsonSchemaKeywords(IOpenApiWriter writer)
internal void WriteAsItemsProperties(IOpenApiWriter writer)
{
// type
writer.WriteProperty(OpenApiConstants.Type, (Type & ~JsonSchemaType.Null).ToIdentifier()?.FirstOrDefault());
writer.WriteProperty(OpenApiConstants.Type, (Type & ~JsonSchemaType.Null)?.FirstIdentifier());

// format
WriteFormatProperty(writer);
Expand Down Expand Up @@ -651,14 +651,14 @@ Extensions is not null &&
break;
case OpenApiSpecVersion.OpenApi3_0 when isNullable && type.Value == JsonSchemaType.Null:
writer.WriteProperty(OpenApiConstants.Nullable, true);
writer.WriteProperty(OpenApiConstants.Type, JsonSchemaType.Object.ToIdentifier().FirstOrDefault());
writer.WriteProperty(OpenApiConstants.Type, JsonSchemaType.Object.FirstIdentifier());
break;
case OpenApiSpecVersion.OpenApi3_0 when isNullable && type.Value != JsonSchemaType.Null:
writer.WriteProperty(OpenApiConstants.Nullable, true);
writer.WriteProperty(OpenApiConstants.Type, type.Value.ToIdentifier().FirstOrDefault());
writer.WriteProperty(OpenApiConstants.Type, type.Value.FirstIdentifier());
break;
default:
writer.WriteProperty(OpenApiConstants.Type, type.Value.ToIdentifier().FirstOrDefault());
writer.WriteProperty(OpenApiConstants.Type, type.Value.FirstIdentifier());
break;
}
}
Expand Down Expand Up @@ -703,7 +703,7 @@ private static void UpCastSchemaTypeToV31(JsonSchemaType type, IOpenApiWriter wr
var temporaryType = type | JsonSchemaType.Null;
var list = (from JsonSchemaType flag in jsonSchemaTypeValues// Check if the flag is set in 'type' using a bitwise AND operation
where temporaryType.HasFlag(flag)
select flag.ToIdentifier().FirstOrDefault()).ToList();
select flag.FirstIdentifier()).ToList();
if (list.Count > 1)
{
writer.WriteOptionalCollection(OpenApiConstants.Type, list, (w, s) => w.WriteValue(s));
Expand Down Expand Up @@ -740,7 +740,7 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType schemaType, IOpenApiWriter
if (schemaType.HasFlag(flag) && flag != JsonSchemaType.Null)
{
// Write the non-null flag value to the writer
writer.WriteProperty(OpenApiConstants.Type, flag.ToIdentifier().FirstOrDefault());
writer.WriteProperty(OpenApiConstants.Type, flag.FirstIdentifier());
}
}
writer.WriteProperty(nullableProp, true);
Expand All @@ -753,7 +753,7 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType schemaType, IOpenApiWriter
}
else
{
writer.WriteProperty(OpenApiConstants.Type, schemaType.ToIdentifier().FirstOrDefault());
writer.WriteProperty(OpenApiConstants.Type, schemaType.FirstIdentifier());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void ValidateDataTypeMismatch(
// convert value to JsonElement and access the ValueKind property to determine the type.
var valueKind = value.GetValueKind();

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

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