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
5 changes: 4 additions & 1 deletion src/Microsoft.OpenApi/Models/OpenApiExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
writer.WriteProperty(OpenApiConstants.Description, Description);

// value
writer.WriteOptionalObject(OpenApiConstants.Value, Value, (w, v) => w.WriteAny(v));
if (Value is not null)
{
writer.WriteRequiredObject(OpenApiConstants.Value, Value, (w, v) => w.WriteAny(v));
}

// externalValue
writer.WriteProperty(OpenApiConstants.ExternalValue, ExternalValue);
Expand Down
30 changes: 1 addition & 29 deletions src/Microsoft.OpenApi/Models/OpenApiMediaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
// examples
if (Examples != null && Examples.Any())
{
SerializeExamples(writer, Examples, callback);
writer.WriteOptionalMap(OpenApiConstants.Examples, Examples, callback);
}

// encoding
Expand All @@ -114,33 +114,5 @@ public virtual void SerializeAsV2(IOpenApiWriter writer)
{
// Media type does not exist in V2.
}

private static void SerializeExamples(IOpenApiWriter writer, IDictionary<string, IOpenApiExample> examples, Action<IOpenApiWriter, IOpenApiSerializable> callback)
{
/* Special case for writing out empty arrays as valid response examples
* Check if there is any example with an empty array as its value and set the flag `hasEmptyArray` to true
* */
var hasEmptyArray = examples.Values.Any( static example =>
example.Value is JsonArray arr && arr.Count == 0
);

if (hasEmptyArray)
{
writer.WritePropertyName(OpenApiConstants.Examples);
writer.WriteStartObject();
foreach (var kvp in examples.Where(static kvp => kvp.Value.Value is JsonArray arr && arr.Count == 0))
{
writer.WritePropertyName(kvp.Key);
writer.WriteStartObject();
writer.WriteRequiredObject(OpenApiConstants.Value, kvp.Value.Value, (w, v) => w.WriteAny(v));
writer.WriteEndObject();
}
writer.WriteEndObject();
}
else
{
writer.WriteOptionalMap(OpenApiConstants.Examples, examples, callback);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,16 @@ public async Task ParseMediaTypeWithEmptyArrayInExamplesWorks()
},
""examples"": {
""Success response - no results"": {
""summary"": ""empty array summary"",
""description"": ""empty array description"",
""value"": [ ]
},
""Success response - with results"": {
""summary"": ""array summary"",
""description"": ""array description"",
""value"": [
1
]
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
},
"examples": {
"Success response - no results": {
"summary": "empty array summary",
"description": "empty array description",
"value": []
},
"Success response - with results": {
"summary": "array summary",
"description": "array description",
"value": [ 1 ]
}
}
}
Loading