Skip to content

Commit

Permalink
fix: multiple unit test failures
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Feb 3, 2025
1 parent 4d9c17b commit 2f171a3
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static Type MapOpenApiPrimitiveTypeToSimpleType(this OpenApiSchema schema
throw new ArgumentNullException(nameof(schema));
}

var type = ((schema.Type.Value ^ JsonSchemaType.Null).ToIdentifier(), schema.Format?.ToLowerInvariant(), schema.Type.Value & JsonSchemaType.Null) switch
var type = ((schema.Type & ~JsonSchemaType.Null).ToIdentifier(), schema.Format?.ToLowerInvariant(), schema.Type & JsonSchemaType.Null) switch
{
("integer" or "number", "int32", JsonSchemaType.Null) => typeof(int?),
("integer" or "number", "int64", JsonSchemaType.Null) => typeof(long?),
Expand Down
9 changes: 2 additions & 7 deletions src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,9 +726,7 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType schemaType, IOpenApiWriter
? OpenApiConstants.NullableExtension
: OpenApiConstants.Nullable;

var nullable = (schemaType & JsonSchemaType.Null) == JsonSchemaType.Null;

if (!HasMultipleTypes(schemaType ^ JsonSchemaType.Null) && nullable) // checks for two values and one is null
if (!HasMultipleTypes(schemaType & ~JsonSchemaType.Null) && (schemaType & JsonSchemaType.Null) == JsonSchemaType.Null) // checks for two values and one is null
{
foreach (JsonSchemaType flag in jsonSchemaTypeValues)
{
Expand All @@ -739,10 +737,7 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType schemaType, IOpenApiWriter
writer.WriteProperty(OpenApiConstants.Type, flag.ToIdentifier());
}
}
if (!nullable || version is not OpenApiSpecVersion.OpenApi2_0)
{
writer.WriteProperty(nullableProp, true);
}
writer.WriteProperty(nullableProp, true);
}
else if (!HasMultipleTypes(schemaType))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void RemoveAnyOfAndOneOfFromSchema()
Assert.NotNull(openApiDocument.Components.Schemas);
Assert.NotNull(testSchema);
Assert.Null(averageAudioDegradationProperty.AnyOf);
Assert.Equal(JsonSchemaType.Number, averageAudioDegradationProperty.Type);
Assert.Equal(JsonSchemaType.Number | JsonSchemaType.Null, averageAudioDegradationProperty.Type);
Assert.Equal("float", averageAudioDegradationProperty.Format);
Assert.Equal(JsonSchemaType.Null, averageAudioDegradationProperty.Type & JsonSchemaType.Null);
Assert.Null(defaultPriceProperty.OneOf);
Expand Down Expand Up @@ -163,11 +163,10 @@ private static OpenApiDocument GetSampleOpenApiDocument()
{
AnyOf = new List<IOpenApiSchema>
{
new OpenApiSchema() { Type = JsonSchemaType.Number },
new OpenApiSchema() { Type = JsonSchemaType.Number | JsonSchemaType.Null },
new OpenApiSchema() { Type = JsonSchemaType.String }
},
Format = "float",
Type = JsonSchemaType.Number | JsonSchemaType.Null | JsonSchemaType.String
}
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"type": "object",
"x-nullable": true,
"title": "title1",
"required": [
"property1"
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"type":"object","title":"title1","required":["property1"],"properties":{"property1":{"required":["property3"],"properties":{"property2":{"type":"integer"},"property3":{"type":"string","maxLength":15}}},"property4":{"properties":{"property5":{"properties":{"property6":{"type":"boolean"}}},"property7":{"type":"string","minLength":2}},"readOnly":true}},"externalDocs":{"url":"http://example.com/externalDocs"}}
{"type":"object","x-nullable":true,"title":"title1","required":["property1"],"properties":{"property1":{"required":["property3"],"properties":{"property2":{"type":"integer"},"property3":{"type":"string","maxLength":15}}},"property4":{"properties":{"property5":{"properties":{"property6":{"type":"boolean"}}},"property7":{"type":"string","minLength":2}},"readOnly":true}},"externalDocs":{"url":"http://example.com/externalDocs"}}
21 changes: 9 additions & 12 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ public async Task SerializeAdvancedSchemaNumberAsV3JsonWorks()
"maximum": 42,
"minimum": 10,
"exclusiveMinimum": true,
"nullable": true,
"type": "integer",
"nullable": true,
"default": 15,
"externalDocs": {
"url": "http://example.com/externalDocs"
Expand All @@ -253,9 +253,7 @@ public async Task SerializeAdvancedSchemaNumberAsV3JsonWorks()
var actual = await AdvancedSchemaNumber.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0);

// Assert
actual = actual.MakeLineBreaksEnvironmentNeutral();
expected = expected.MakeLineBreaksEnvironmentNeutral();
Assert.Equal(expected, actual);
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(expected), JsonNode.Parse(actual)));
}

[Fact]
Expand All @@ -266,6 +264,7 @@ public async Task SerializeAdvancedSchemaObjectAsV3JsonWorks()
"""
{
"title": "title1",
"type": "object",
"nullable": true,
"properties": {
"property1": {
Expand Down Expand Up @@ -305,9 +304,7 @@ public async Task SerializeAdvancedSchemaObjectAsV3JsonWorks()
var actual = await AdvancedSchemaObject.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0);

// Assert
actual = actual.MakeLineBreaksEnvironmentNeutral();
expected = expected.MakeLineBreaksEnvironmentNeutral();
Assert.Equal(expected, actual);
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(expected), JsonNode.Parse(actual)));
}

[Fact]
Expand All @@ -318,6 +315,7 @@ public async Task SerializeAdvancedSchemaWithAllOfAsV3JsonWorks()
"""
{
"title": "title1",
"type": "object",
"nullable": true,
"allOf": [
{
Expand All @@ -334,6 +332,7 @@ public async Task SerializeAdvancedSchemaWithAllOfAsV3JsonWorks()
},
{
"title": "title3",
"type": "object",
"nullable": true,
"properties": {
"property3": {
Expand All @@ -360,9 +359,7 @@ public async Task SerializeAdvancedSchemaWithAllOfAsV3JsonWorks()
var actual = await AdvancedSchemaWithAllOf.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0);

// Assert
actual = actual.MakeLineBreaksEnvironmentNeutral();
expected = expected.MakeLineBreaksEnvironmentNeutral();
Assert.Equal(expected, actual);
Assert.True(JsonObject.DeepEquals(JsonObject.Parse(expected), JsonObject.Parse(actual)));
}

[Theory]
Expand Down Expand Up @@ -472,9 +469,9 @@ public void OpenApiSchemaCopyConstructorSucceeds()
var actualSchema = baseSchema.CreateShallowCopy() as OpenApiSchema;
actualSchema.Type |= JsonSchemaType.Null;

Assert.Equal(JsonSchemaType.String, actualSchema.Type);
Assert.Equal("date", actualSchema.Format);
Assert.Equal(JsonSchemaType.String, actualSchema.Type & JsonSchemaType.String);
Assert.Equal(JsonSchemaType.Null, actualSchema.Type & JsonSchemaType.Null);
Assert.Equal("date", actualSchema.Format);
}

[Fact]
Expand Down

0 comments on commit 2f171a3

Please sign in to comment.