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
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/// <param name="writer">The writer.</param>
/// <param name="name">The property name.</param>
/// <param name="value">The property value.</param>
public static void WriteProperty(this IOpenApiWriter writer, string name, string? value)

Check warning on line 23 in src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

All 'WriteProperty' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)

Check warning on line 23 in src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

All 'WriteProperty' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)
{
if (value == null)
{
Expand Down Expand Up @@ -139,11 +139,11 @@
T? value,
Action<IOpenApiWriter, T> action)
{
if (value != null)

Check warning on line 142 in src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

Use a comparison to 'default(T)' instead or add a constraint to 'T' so that it can't be a value type. (https://rules.sonarsource.com/csharp/RSPEC-2955)

Check warning on line 142 in src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

Use a comparison to 'default(T)' instead or add a constraint to 'T' so that it can't be a value type. (https://rules.sonarsource.com/csharp/RSPEC-2955)
{
if (value is IEnumerable values && !values.GetEnumerator().MoveNext())
if (value is IEnumerable values && value is not JsonArray && !values.GetEnumerator().MoveNext())
{
return; // Don't render optional empty collections
return; // Don't render optional empty collections except for the Default properties which are JsonArray
}

writer.WriteRequiredObject(name, value, action);
Expand All @@ -167,7 +167,7 @@
Utils.CheckArgumentNull(action);

writer.WritePropertyName(name);
if (value != null)

Check warning on line 170 in src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

Use a comparison to 'default(T)' instead or add a constraint to 'T' so that it can't be a value type. (https://rules.sonarsource.com/csharp/RSPEC-2955)
{
action(writer, value);
}
Expand Down Expand Up @@ -262,7 +262,7 @@
/// <param name="name">The property name.</param>
/// <param name="elements">The map values.</param>
/// <param name="action">The map element writer action.</param>
public static void WriteRequiredMap(

Check warning on line 265 in src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

All 'WriteRequiredMap' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)

Check warning on line 265 in src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

All 'WriteRequiredMap' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)
this IOpenApiWriter writer,
string name,
IDictionary<string, string>? elements,
Expand Down Expand Up @@ -421,7 +421,7 @@
{
foreach (var item in elements)
{
if (item != null)

Check warning on line 424 in src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

Use a comparison to 'default(T)' instead or add a constraint to 'T' so that it can't be a value type. (https://rules.sonarsource.com/csharp/RSPEC-2955)
{
action(writer, item);
}
Expand Down Expand Up @@ -460,7 +460,7 @@
foreach (var item in elements)
{
writer.WritePropertyName(item.Key);
if (item.Value != null)

Check warning on line 463 in src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

Use a comparison to 'default(T)' instead or add a constraint to 'T' so that it can't be a value type. (https://rules.sonarsource.com/csharp/RSPEC-2955)
{
action(writer, item.Key, item.Value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,40 @@ public void CloningSchemaWithExamplesAndEnumsShouldSucceed()
Assert.Equivalent(6, clone.Default.GetValue<int>());
}

[Fact]
public void DefaultEmptyCollectionShouldRoundTrip()
{
// Given
var serializedSchema =
"""
{
"type": "array",
"items": {
"type": "string",
"default": []
}
}
""";
using var textWriter = new StringWriter();
var writer = new OpenApiJsonWriter(textWriter);

// When
var schema = OpenApiModelFactory.Parse<OpenApiSchema>(serializedSchema, OpenApiSpecVersion.OpenApi3_1, new(), out _, "json", SettingsFixture.ReaderSettings);

var deserializedArray = Assert.IsType<JsonArray>(schema.Items.Default);
Assert.Empty(deserializedArray);

schema.SerializeAsV31(writer);
var roundTrippedSchema = textWriter.ToString();

// Then
var parsedResult = JsonNode.Parse(roundTrippedSchema);
var parsedExpected = JsonNode.Parse(serializedSchema);
Assert.True(JsonNode.DeepEquals(parsedExpected, parsedResult));
var resultingArray = Assert.IsType<JsonArray>(parsedResult["items"]?["default"]);
Assert.Empty(resultingArray);
}

[Fact]
public async Task SerializeV31SchemaWithMultipleTypesAsV3Works()
{
Expand Down
Loading