Skip to content

Commit 2f171a3

Browse files
committed
fix: multiple unit test failures
Signed-off-by: Vincent Biret <[email protected]>
1 parent 4d9c17b commit 2f171a3

File tree

6 files changed

+16
-24
lines changed

6 files changed

+16
-24
lines changed

src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public static Type MapOpenApiPrimitiveTypeToSimpleType(this OpenApiSchema schema
153153
throw new ArgumentNullException(nameof(schema));
154154
}
155155

156-
var type = ((schema.Type.Value ^ JsonSchemaType.Null).ToIdentifier(), schema.Format?.ToLowerInvariant(), schema.Type.Value & JsonSchemaType.Null) switch
156+
var type = ((schema.Type & ~JsonSchemaType.Null).ToIdentifier(), schema.Format?.ToLowerInvariant(), schema.Type & JsonSchemaType.Null) switch
157157
{
158158
("integer" or "number", "int32", JsonSchemaType.Null) => typeof(int?),
159159
("integer" or "number", "int64", JsonSchemaType.Null) => typeof(long?),

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,7 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType schemaType, IOpenApiWriter
726726
? OpenApiConstants.NullableExtension
727727
: OpenApiConstants.Nullable;
728728

729-
var nullable = (schemaType & JsonSchemaType.Null) == JsonSchemaType.Null;
730-
731-
if (!HasMultipleTypes(schemaType ^ JsonSchemaType.Null) && nullable) // checks for two values and one is null
729+
if (!HasMultipleTypes(schemaType & ~JsonSchemaType.Null) && (schemaType & JsonSchemaType.Null) == JsonSchemaType.Null) // checks for two values and one is null
732730
{
733731
foreach (JsonSchemaType flag in jsonSchemaTypeValues)
734732
{
@@ -739,10 +737,7 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType schemaType, IOpenApiWriter
739737
writer.WriteProperty(OpenApiConstants.Type, flag.ToIdentifier());
740738
}
741739
}
742-
if (!nullable || version is not OpenApiSpecVersion.OpenApi2_0)
743-
{
744-
writer.WriteProperty(nullableProp, true);
745-
}
740+
writer.WriteProperty(nullableProp, true);
746741
}
747742
else if (!HasMultipleTypes(schemaType))
748743
{

test/Microsoft.OpenApi.Hidi.Tests/Formatters/PowerShellFormatterTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void RemoveAnyOfAndOneOfFromSchema()
6969
Assert.NotNull(openApiDocument.Components.Schemas);
7070
Assert.NotNull(testSchema);
7171
Assert.Null(averageAudioDegradationProperty.AnyOf);
72-
Assert.Equal(JsonSchemaType.Number, averageAudioDegradationProperty.Type);
72+
Assert.Equal(JsonSchemaType.Number | JsonSchemaType.Null, averageAudioDegradationProperty.Type);
7373
Assert.Equal("float", averageAudioDegradationProperty.Format);
7474
Assert.Equal(JsonSchemaType.Null, averageAudioDegradationProperty.Type & JsonSchemaType.Null);
7575
Assert.Null(defaultPriceProperty.OneOf);
@@ -163,11 +163,10 @@ private static OpenApiDocument GetSampleOpenApiDocument()
163163
{
164164
AnyOf = new List<IOpenApiSchema>
165165
{
166-
new OpenApiSchema() { Type = JsonSchemaType.Number },
166+
new OpenApiSchema() { Type = JsonSchemaType.Number | JsonSchemaType.Null },
167167
new OpenApiSchema() { Type = JsonSchemaType.String }
168168
},
169169
Format = "float",
170-
Type = JsonSchemaType.Number | JsonSchemaType.Null | JsonSchemaType.String
171170
}
172171
},
173172
{

test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.SerializeSchemaWRequiredPropertiesAsV2JsonWorksAsync_produceTerseOutput=False.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"type": "object",
3+
"x-nullable": true,
34
"title": "title1",
45
"required": [
56
"property1"
Original file line numberDiff line numberDiff line change
@@ -1 +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"}}
1+
{"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"}}

test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ public async Task SerializeAdvancedSchemaNumberAsV3JsonWorks()
240240
"maximum": 42,
241241
"minimum": 10,
242242
"exclusiveMinimum": true,
243-
"nullable": true,
244243
"type": "integer",
244+
"nullable": true,
245245
"default": 15,
246246
"externalDocs": {
247247
"url": "http://example.com/externalDocs"
@@ -253,9 +253,7 @@ public async Task SerializeAdvancedSchemaNumberAsV3JsonWorks()
253253
var actual = await AdvancedSchemaNumber.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0);
254254

255255
// Assert
256-
actual = actual.MakeLineBreaksEnvironmentNeutral();
257-
expected = expected.MakeLineBreaksEnvironmentNeutral();
258-
Assert.Equal(expected, actual);
256+
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(expected), JsonNode.Parse(actual)));
259257
}
260258

261259
[Fact]
@@ -266,6 +264,7 @@ public async Task SerializeAdvancedSchemaObjectAsV3JsonWorks()
266264
"""
267265
{
268266
"title": "title1",
267+
"type": "object",
269268
"nullable": true,
270269
"properties": {
271270
"property1": {
@@ -305,9 +304,7 @@ public async Task SerializeAdvancedSchemaObjectAsV3JsonWorks()
305304
var actual = await AdvancedSchemaObject.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0);
306305

307306
// Assert
308-
actual = actual.MakeLineBreaksEnvironmentNeutral();
309-
expected = expected.MakeLineBreaksEnvironmentNeutral();
310-
Assert.Equal(expected, actual);
307+
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(expected), JsonNode.Parse(actual)));
311308
}
312309

313310
[Fact]
@@ -318,6 +315,7 @@ public async Task SerializeAdvancedSchemaWithAllOfAsV3JsonWorks()
318315
"""
319316
{
320317
"title": "title1",
318+
"type": "object",
321319
"nullable": true,
322320
"allOf": [
323321
{
@@ -334,6 +332,7 @@ public async Task SerializeAdvancedSchemaWithAllOfAsV3JsonWorks()
334332
},
335333
{
336334
"title": "title3",
335+
"type": "object",
337336
"nullable": true,
338337
"properties": {
339338
"property3": {
@@ -360,9 +359,7 @@ public async Task SerializeAdvancedSchemaWithAllOfAsV3JsonWorks()
360359
var actual = await AdvancedSchemaWithAllOf.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0);
361360

362361
// Assert
363-
actual = actual.MakeLineBreaksEnvironmentNeutral();
364-
expected = expected.MakeLineBreaksEnvironmentNeutral();
365-
Assert.Equal(expected, actual);
362+
Assert.True(JsonObject.DeepEquals(JsonObject.Parse(expected), JsonObject.Parse(actual)));
366363
}
367364

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

475-
Assert.Equal(JsonSchemaType.String, actualSchema.Type);
476-
Assert.Equal("date", actualSchema.Format);
472+
Assert.Equal(JsonSchemaType.String, actualSchema.Type & JsonSchemaType.String);
477473
Assert.Equal(JsonSchemaType.Null, actualSchema.Type & JsonSchemaType.Null);
474+
Assert.Equal("date", actualSchema.Format);
478475
}
479476

480477
[Fact]

0 commit comments

Comments
 (0)