Skip to content

Commit cb1c496

Browse files
authored
Merge pull request #2473 from melnikvitaly/main
fix: missing examples when one example is with an empty array.
2 parents f56884e + d93c6c3 commit cb1c496

File tree

4 files changed

+21
-30
lines changed

4 files changed

+21
-30
lines changed

src/Microsoft.OpenApi/Models/OpenApiExample.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
7070
writer.WriteProperty(OpenApiConstants.Description, Description);
7171

7272
// value
73-
writer.WriteOptionalObject(OpenApiConstants.Value, Value, (w, v) => w.WriteAny(v));
73+
if (Value is not null)
74+
{
75+
writer.WriteRequiredObject(OpenApiConstants.Value, Value, (w, v) => w.WriteAny(v));
76+
}
7477

7578
// externalValue
7679
writer.WriteProperty(OpenApiConstants.ExternalValue, ExternalValue);

src/Microsoft.OpenApi/Models/OpenApiMediaType.cs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
9595
// examples
9696
if (Examples != null && Examples.Any())
9797
{
98-
SerializeExamples(writer, Examples, callback);
98+
writer.WriteOptionalMap(OpenApiConstants.Examples, Examples, callback);
9999
}
100100

101101
// encoding
@@ -114,33 +114,5 @@ public virtual void SerializeAsV2(IOpenApiWriter writer)
114114
{
115115
// Media type does not exist in V2.
116116
}
117-
118-
private static void SerializeExamples(IOpenApiWriter writer, IDictionary<string, IOpenApiExample> examples, Action<IOpenApiWriter, IOpenApiSerializable> callback)
119-
{
120-
/* Special case for writing out empty arrays as valid response examples
121-
* Check if there is any example with an empty array as its value and set the flag `hasEmptyArray` to true
122-
* */
123-
var hasEmptyArray = examples.Values.Any( static example =>
124-
example.Value is JsonArray arr && arr.Count == 0
125-
);
126-
127-
if (hasEmptyArray)
128-
{
129-
writer.WritePropertyName(OpenApiConstants.Examples);
130-
writer.WriteStartObject();
131-
foreach (var kvp in examples.Where(static kvp => kvp.Value.Value is JsonArray arr && arr.Count == 0))
132-
{
133-
writer.WritePropertyName(kvp.Key);
134-
writer.WriteStartObject();
135-
writer.WriteRequiredObject(OpenApiConstants.Value, kvp.Value.Value, (w, v) => w.WriteAny(v));
136-
writer.WriteEndObject();
137-
}
138-
writer.WriteEndObject();
139-
}
140-
else
141-
{
142-
writer.WriteOptionalMap(OpenApiConstants.Examples, examples, callback);
143-
}
144-
}
145117
}
146118
}

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,16 @@ public async Task ParseMediaTypeWithEmptyArrayInExamplesWorks()
8787
},
8888
""examples"": {
8989
""Success response - no results"": {
90+
""summary"": ""empty array summary"",
91+
""description"": ""empty array description"",
9092
""value"": [ ]
93+
},
94+
""Success response - with results"": {
95+
""summary"": ""array summary"",
96+
""description"": ""array description"",
97+
""value"": [
98+
1
99+
]
91100
}
92101
}
93102
}

test/Microsoft.OpenApi.Readers.Tests/V3Tests/Samples/OpenApiMediaType/examplesWithEmptyArray.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
},
1313
"examples": {
1414
"Success response - no results": {
15+
"summary": "empty array summary",
16+
"description": "empty array description",
1517
"value": []
18+
},
19+
"Success response - with results": {
20+
"summary": "array summary",
21+
"description": "array description",
22+
"value": [ 1 ]
1623
}
1724
}
1825
}

0 commit comments

Comments
 (0)