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 @@ -138,7 +138,10 @@ public void Serialize(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 @@ -79,7 +79,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
// examples
if (Examples != null && Examples.Any())
{
SerializeExamples(writer, Examples);
writer.WriteOptionalMap(OpenApiConstants.Examples, Examples, (w, e) => e.SerializeAsV3(w));
}

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

private static void SerializeExamples(IOpenApiWriter writer, IDictionary<string, OpenApiExample> examples)
{
/* 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 OpenApiArray arr && arr.Count == 0
);

if (hasEmptyArray)
{
writer.WritePropertyName(OpenApiConstants.Examples);
writer.WriteStartObject();
foreach (var kvp in examples.Where(static kvp => kvp.Value.Value is OpenApiArray 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, (w, e) => e.SerializeAsV3(w));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,16 @@ public void 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