Skip to content

Commit fd32d81

Browse files
committed
Cleanup
1 parent 4b74290 commit fd32d81

5 files changed

Lines changed: 25 additions & 26 deletions

File tree

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,6 @@ public string? ExclusiveMinimum
114114
private bool HasNullType
115115
=> Type.HasValue && Type.Value.HasFlag(JsonSchemaType.Null);
116116

117-
private bool HasTrueNullableExtension
118-
=> Extensions is not null &&
119-
Extensions.TryGetValue(OpenApiConstants.NullableExtension, out var nullExtRawValue) &&
120-
nullExtRawValue is JsonNodeExtension { Node: JsonNode jsonNode } &&
121-
jsonNode.GetValueKind() is JsonValueKind.True;
122-
123117
/// <inheritdoc />
124118
public string? Const { get; set; }
125119

@@ -790,18 +784,6 @@ internal void WriteAsItemsProperties(IOpenApiWriter writer)
790784
writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi2_0);
791785
}
792786

793-
internal void FinalizeDeserialization(OpenApiSpecVersion version)
794-
{
795-
if (version is OpenApiSpecVersion.OpenApi2_0 or OpenApiSpecVersion.OpenApi3_0 && HasTrueNullableExtension)
796-
{
797-
Extensions!.Remove(OpenApiConstants.NullableExtension);
798-
if (Type is not null && Type != 0)
799-
{
800-
Type |= JsonSchemaType.Null;
801-
}
802-
}
803-
}
804-
805787
private void WriteFormatProperty(IOpenApiWriter writer)
806788
{
807789
var formatToWrite = Format;

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,16 @@ public static IOpenApiSchema LoadSchema(JsonNode node, OpenApiDocument hostDocum
268268

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

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

273282
return schema;
274283
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ internal static partial class OpenApiV3Deserializer
233233
{
234234
// While we are dealing with v3 document, we are still using x-nullable extension here.
235235
// This is used only as a marker during deserialization to indicate that the schema is nullable.
236-
// When we do FinalizeDeserialization, we will modify the OpenApiSchema.Type to
237-
// include JsonSchemaType.Null (only if needed), and always remove the extension.
236+
// When we complete the deserialization, we will modify the OpenApiSchema.Type to
237+
// include JsonSchemaType.Null (**only if** needed), and always remove the extension.
238238
// The reason is that "nullable" property should only take effect if "Type" is set.
239239
// If we knew Type is set, we add "Null" to it.
240240
// Otherwise, it might be that it will be set later.
@@ -400,7 +400,19 @@ public static IOpenApiSchema LoadSchema(JsonNode node, OpenApiDocument hostDocum
400400

401401
ParseMap(jsonObject, schema, _openApiSchemaFixedFields, _openApiSchemaPatternFields, hostDocument, context);
402402

403-
schema.FinalizeDeserialization(OpenApiSpecVersion.OpenApi3_0);
403+
if (schema.Extensions?.TryGetValue(OpenApiConstants.NullableExtension, out var value) == true &&
404+
value == _nullableTrueExtension)
405+
{
406+
// If this is "our own" internal _nullableTrueExtension instance, then we know we encountered "nullable": true in the schema
407+
// at a point where Type wasn't set yet.
408+
// We check now if schema.Type is set.
409+
// If it is, we add JsonSchemaType.Null to it.
410+
schema.Extensions.Remove(OpenApiConstants.NullableExtension);
411+
if (schema.Type is not null && schema.Type != 0)
412+
{
413+
schema.Type |= JsonSchemaType.Null;
414+
}
415+
}
404416

405417
return schema;
406418
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,6 @@ public static IOpenApiSchema LoadSchema(JsonNode node, OpenApiDocument hostDocum
459459
schema.UnrecognizedKeywords[name] = value;
460460
});
461461

462-
schema.FinalizeDeserialization(OpenApiSpecVersion.OpenApi3_1);
463-
464462
if (!string.IsNullOrEmpty(identifier) && hostDocument.Workspace is not null)
465463
{
466464
// register the schema in our registry using the identifier's URL

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,6 @@ public static IOpenApiSchema LoadSchema(JsonNode node, OpenApiDocument hostDocum
459459
schema.UnrecognizedKeywords[name] = value;
460460
});
461461

462-
schema.FinalizeDeserialization(OpenApiSpecVersion.OpenApi3_2);
463-
464462
if (!string.IsNullOrEmpty(identifier) && hostDocument.Workspace is not null)
465463
{
466464
// register the schema in our registry using the identifier's URL

0 commit comments

Comments
 (0)