Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix: multiple unit test failures
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
  • Loading branch information
baywet committed Feb 3, 2025
commit 2f171a3476ea0f3227ecdcb724f1d1af5406ec0e
2 changes: 1 addition & 1 deletion 2 src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
return schemaType switch
{
JsonSchemaType.Null => "null",
JsonSchemaType.Boolean => "boolean",

Check warning on line 41 in src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'boolean' 4 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
JsonSchemaType.Integer => "integer",

Check warning on line 42 in src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'integer' 8 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
JsonSchemaType.Number => "number",

Check warning on line 43 in src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'number' 14 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
JsonSchemaType.String => "string",

Check warning on line 44 in src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'string' 13 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
JsonSchemaType.Array => "array",
JsonSchemaType.Object => "object",
_ => null,
Expand All @@ -61,7 +61,7 @@
"null" => JsonSchemaType.Null,
"boolean" => JsonSchemaType.Boolean,
"integer" or "int" => JsonSchemaType.Integer,
"number" or "double" or "float" or "decimal"=> JsonSchemaType.Number,

Check warning on line 64 in src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'double' 7 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)

Check warning on line 64 in src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'float' 5 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
"string" => JsonSchemaType.String,
"array" => JsonSchemaType.Array,
"object" => JsonSchemaType.Object,
Expand Down Expand Up @@ -153,7 +153,7 @@
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 9 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 21 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
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.