Skip to content

Commit f66b887

Browse files
committed
Cleanup
1 parent a70df43 commit f66b887

5 files changed

Lines changed: 20 additions & 46 deletions

File tree

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ public string? ExclusiveMinimum
114114
// x-nullable is filtered out by deserializers, but keep the check here in case it gets added from user code.
115115
internal bool IsNullable
116116
{
117-
get => field || HasNullableExtension || (Type.HasValue && Type.Value.HasFlag(JsonSchemaType.Null));
117+
get => field || HasTrueNullableExtension || (Type.HasValue && Type.Value.HasFlag(JsonSchemaType.Null));
118118
set => field = value;
119119
}
120120

121-
private bool HasNullableExtension
121+
private bool HasTrueNullableExtension
122122
=> Extensions is not null &&
123123
Extensions.TryGetValue(OpenApiConstants.NullableExtension, out var nullExtRawValue) &&
124124
nullExtRawValue is JsonNodeExtension { Node: JsonNode jsonNode } &&
@@ -794,6 +794,20 @@ internal void WriteAsItemsProperties(IOpenApiWriter writer)
794794
writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi2_0);
795795
}
796796

797+
internal void FinalizeDeserialization()
798+
{
799+
if (HasTrueNullableExtension)
800+
{
801+
IsNullable = true;
802+
Extensions!.Remove(OpenApiConstants.NullableExtension);
803+
}
804+
805+
if (IsNullable && Type is not null && Type != 0)
806+
{
807+
Type |= JsonSchemaType.Null;
808+
}
809+
}
810+
797811
private void WriteFormatProperty(IOpenApiWriter writer)
798812
{
799813
var formatToWrite = Format;

src/Microsoft.OpenApi/Reader/V2/OpenApiSchemaDeserializer.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -268,17 +268,7 @@ public static IOpenApiSchema LoadSchema(JsonNode node, OpenApiDocument hostDocum
268268

269269
ParseMap(jsonObject, schema, _openApiSchemaFixedFields, _openApiSchemaPatternFields, hostDocument, context);
270270

271-
if (schema.Extensions is not null && schema.Extensions.TryGetValue(OpenApiConstants.NullableExtension, out var nullableExtension))
272-
{
273-
var isNullable = nullableExtension is JsonNodeExtension { Node: JsonNode jsonNode } && jsonNode.GetValueKind() is JsonValueKind.True;
274-
schema.IsNullable = isNullable;
275-
schema.Extensions.Remove(OpenApiConstants.NullableExtension);
276-
}
277-
278-
if (schema.IsNullable && schema.Type is not null && schema.Type != 0)
279-
{
280-
schema.Type |= JsonSchemaType.Null;
281-
}
271+
schema.FinalizeDeserialization();
282272

283273
return schema;
284274
}

src/Microsoft.OpenApi/Reader/V3/OpenApiSchemaDeserializer.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -385,17 +385,7 @@ public static IOpenApiSchema LoadSchema(JsonNode node, OpenApiDocument hostDocum
385385

386386
ParseMap(jsonObject, schema, _openApiSchemaFixedFields, _openApiSchemaPatternFields, hostDocument, context);
387387

388-
if (schema.Extensions is not null && schema.Extensions.TryGetValue(OpenApiConstants.NullableExtension, out var nullableExtension))
389-
{
390-
var isNullable = nullableExtension is JsonNodeExtension { Node: JsonNode jsonNode } && jsonNode.GetValueKind() is JsonValueKind.True;
391-
schema.IsNullable = isNullable;
392-
schema.Extensions.Remove(OpenApiConstants.NullableExtension);
393-
}
394-
395-
if (schema.IsNullable && schema.Type is not null && schema.Type != 0)
396-
{
397-
schema.Type |= JsonSchemaType.Null;
398-
}
388+
schema.FinalizeDeserialization();
399389

400390
return schema;
401391
}

src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -470,17 +470,7 @@ public static IOpenApiSchema LoadSchema(JsonNode node, OpenApiDocument hostDocum
470470
schema.UnrecognizedKeywords[name] = value;
471471
});
472472

473-
if (schema.Extensions is not null && schema.Extensions.TryGetValue(OpenApiConstants.NullableExtension, out var nullableExtension))
474-
{
475-
var isNullable = nullableExtension is JsonNodeExtension { Node: JsonNode jsonNode } && jsonNode.GetValueKind() is JsonValueKind.True;
476-
schema.IsNullable = isNullable;
477-
schema.Extensions.Remove(OpenApiConstants.NullableExtension);
478-
}
479-
480-
if (schema.IsNullable && schema.Type is not null && schema.Type != 0)
481-
{
482-
schema.Type |= JsonSchemaType.Null;
483-
}
473+
schema.FinalizeDeserialization();
484474

485475
if (!string.IsNullOrEmpty(identifier) && hostDocument.Workspace is not null)
486476
{

src/Microsoft.OpenApi/Reader/V32/OpenApiSchemaDeserializer.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -470,17 +470,7 @@ public static IOpenApiSchema LoadSchema(JsonNode node, OpenApiDocument hostDocum
470470
schema.UnrecognizedKeywords[name] = value;
471471
});
472472

473-
if (schema.Extensions is not null && schema.Extensions.TryGetValue(OpenApiConstants.NullableExtension, out var nullableExtension))
474-
{
475-
var isNullable = nullableExtension is JsonNodeExtension { Node: JsonNode jsonNode } && jsonNode.GetValueKind() is JsonValueKind.True;
476-
schema.IsNullable = isNullable;
477-
schema.Extensions.Remove(OpenApiConstants.NullableExtension);
478-
}
479-
480-
if (schema.IsNullable && schema.Type is not null && schema.Type != 0)
481-
{
482-
schema.Type |= JsonSchemaType.Null;
483-
}
473+
schema.FinalizeDeserialization();
484474

485475
if (!string.IsNullOrEmpty(identifier) && hostDocument.Workspace is not null)
486476
{

0 commit comments

Comments
 (0)