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
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,17 @@ 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;
JsonSchemaType combinedType = preserveNull ? JsonSchemaType.Null : 0;
foreach(var type in list.Where(static t => t is not null).Select(static t => t!.ToJsonSchemaType()))
{
combinedType |= type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,17 @@ internal static partial class OpenApiV32Deserializer
"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;
JsonSchemaType combinedType = preserveNull ? JsonSchemaType.Null : 0;
foreach(var type in list.Where(static t => t is not null).Select(static t => t!.ToJsonSchemaType()))
{
combinedType |= type;
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
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,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_2, new(), out _, "json", SettingsFixture.ReaderSettings);

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

[Fact]
public void TestSchemaCopyConstructorWithTypeArrayWorks()
{
Expand Down
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
Loading