Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not write null for types on parameters in v2 #2128

Merged
merged 1 commit into from
Feb 6, 2025
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
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ internal void WriteJsonSchemaKeywords(IOpenApiWriter writer)
internal void WriteAsItemsProperties(IOpenApiWriter writer)
{
// type
writer.WriteProperty(OpenApiConstants.Type, Type.ToIdentifier());
writer.WriteProperty(OpenApiConstants.Type, (Type & ~JsonSchemaType.Null).ToIdentifier());

// format
WriteFormatProperty(writer);
Expand Down
30 changes: 30 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,36 @@ public async Task SerializeSchemaWithUnrecognizedPropertiesWorks()
Assert.Equal(expected.MakeLineBreaksEnvironmentNeutral(), actual.MakeLineBreaksEnvironmentNeutral());
}

[Fact]
public async Task WriteAsItemsPropertiesDoesNotWriteNull()
{
// Arrange
var schema = new OpenApiSchema
{
Type = JsonSchemaType.Number | JsonSchemaType.Null
};

var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
var writer = new OpenApiJsonWriter(outputStringWriter, new() { Terse = false });
writer.WriteStartObject();

// Act
schema.WriteAsItemsProperties(writer);
writer.WriteEndObject();
await writer.FlushAsync();

// Assert
var actual = outputStringWriter.GetStringBuilder().ToString();
var expected =
"""
{
"type": "number"
}
""";
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(expected), JsonNode.Parse(actual)));
}


internal class SchemaVisitor : OpenApiVisitorBase
{
public List<string> Titles = new();
Expand Down
Loading