Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 7 additions & 8 deletions src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,20 @@ internal static partial class OpenApiV31Deserializer
"type",
(o, n, doc) =>
{
// Preserve any Null flag set by a preceding "nullable: true" handler
var preserveNull = o.Type.HasValue && o.Type.Value.HasFlag(JsonSchemaType.Null);
if (n is ValueNode)
{
o.Type = n.GetScalarValue()?.ToJsonSchemaType();
var parsedType = n.GetScalarValue()?.ToJsonSchemaType();
o.Type = preserveNull ? parsedType | JsonSchemaType.Null : parsedType;
}
else
{
var list = n.CreateSimpleList((n2, p) => n2.GetScalarValue(), doc);
JsonSchemaType combinedType = 0;
foreach(var type in list)
JsonSchemaType combinedType = preserveNull ? JsonSchemaType.Null : 0;
foreach(var type in list.Where(static t => t is not null).Select(static t => t!.ToJsonSchemaType()))
{
if (type is not null)
{
var schemaType = type.ToJsonSchemaType();
combinedType |= schemaType;
}
combinedType |= type;
}
o.Type = combinedType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ public void ParseSchemaWithTypeArrayWorks()
Assert.Equivalent(expected, actual);
}

[Theory]
[InlineData(@"{ ""nullable"": true, ""type"": ""string"" }")]
[InlineData(@"{ ""type"": ""string"", ""nullable"": true }")]
public void ParseSchemaWithNullableBeforeOrAfterTypePreservesNullFlag(string schemaJson)
{
// Act
var schema = OpenApiModelFactory.Parse<OpenApiSchema>(schemaJson, OpenApiSpecVersion.OpenApi3_1, new(), out _, "json", SettingsFixture.ReaderSettings);

// Assert
Assert.Equal(JsonSchemaType.String | JsonSchemaType.Null, schema.Type);
}

[Fact]
public void TestSchemaCopyConstructorWithTypeArrayWorks()
{
Expand Down
12 changes: 12 additions & 0 deletions test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ public void ParsePathFragmentShouldSucceed()
}, openApiAny);
}

[Theory]
[InlineData(@"{ ""nullable"": true, ""type"": ""string"" }")]
[InlineData(@"{ ""type"": ""string"", ""nullable"": true }")]
public void ParseSchemaWithNullableBeforeOrAfterTypePreservesNullFlag(string schemaJson)
{
// Act
var schema = OpenApiModelFactory.Parse<OpenApiSchema>(schemaJson, OpenApiSpecVersion.OpenApi3_0, new(), out _, "json", SettingsFixture.ReaderSettings);

// Assert
Assert.Equal(JsonSchemaType.String | JsonSchemaType.Null, schema.Type);
}

[Fact]
public void ParseDictionarySchemaShouldSucceed()
{
Expand Down