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 @@ -10,7 +10,7 @@
namespace Microsoft.OpenApi.Reader.V31;
internal static partial class OpenApiV31Deserializer
{
private static readonly FixedFieldMap<OpenApiSchema> _openApiSchemaFixedFields = new()

Check failure on line 13 in src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this field to reduce its Cognitive Complexity from 64 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=microsoft_OpenAPI.NET&issues=AZ5lM6JkmeMascLuyXZG&open=AZ5lM6JkmeMascLuyXZG&pullRequest=2860
{
{
"title",
Expand Down Expand Up @@ -198,21 +198,20 @@
"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
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