Skip to content

Commit 4697af4

Browse files
vitaly.melnikbaywet
authored andcommitted
fix: missing examples when one example is with an empty array.
Signed-off-by: Vincent Biret <[email protected]>
1 parent b833cdc commit 4697af4

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
@@ -138,7 +138,10 @@ public void Serialize(IOpenApiWriter writer, OpenApiSpecVersion version)
138138
writer.WriteProperty(OpenApiConstants.Description, Description);
139139

140140
// value
141-
writer.WriteOptionalObject(OpenApiConstants.Value, Value, (w, v) => w.WriteAny(v));
141+
if (Value is not null)
142+
{
143+
writer.WriteRequiredObject(OpenApiConstants.Value, Value, (w, v) => w.WriteAny(v));
144+
}
142145

143146
// externalValue
144147
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
@@ -79,7 +79,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
7979
// examples
8080
if (Examples != null && Examples.Any())
8181
{
82-
SerializeExamples(writer, Examples);
82+
writer.WriteOptionalMap(OpenApiConstants.Examples, Examples, (w, e) => e.SerializeAsV3(w));
8383
}
8484

8585
// encoding
@@ -98,33 +98,5 @@ public void SerializeAsV2(IOpenApiWriter writer)
9898
{
9999
// Media type does not exist in V2.
100100
}
101-
102-
private static void SerializeExamples(IOpenApiWriter writer, IDictionary<string, OpenApiExample> examples)
103-
{
104-
/* Special case for writing out empty arrays as valid response examples
105-
* Check if there is any example with an empty array as its value and set the flag `hasEmptyArray` to true
106-
* */
107-
var hasEmptyArray = examples.Values.Any( static example =>
108-
example.Value is OpenApiArray arr && arr.Count == 0
109-
);
110-
111-
if (hasEmptyArray)
112-
{
113-
writer.WritePropertyName(OpenApiConstants.Examples);
114-
writer.WriteStartObject();
115-
foreach (var kvp in examples.Where(static kvp => kvp.Value.Value is OpenApiArray arr && arr.Count == 0))
116-
{
117-
writer.WritePropertyName(kvp.Key);
118-
writer.WriteStartObject();
119-
writer.WriteRequiredObject(OpenApiConstants.Value, kvp.Value.Value, (w, v) => w.WriteAny(v));
120-
writer.WriteEndObject();
121-
}
122-
writer.WriteEndObject();
123-
}
124-
else
125-
{
126-
writer.WriteOptionalMap(OpenApiConstants.Examples, examples, (w, e) => e.SerializeAsV3(w));
127-
}
128-
}
129101
}
130102
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,16 @@ public void ParseMediaTypeWithEmptyArrayInExamplesWorks()
9898
},
9999
""examples"": {
100100
""Success response - no results"": {
101+
""summary"": ""empty array summary"",
102+
""description"": ""empty array description"",
101103
""value"": [ ]
104+
},
105+
""Success response - with results"": {
106+
""summary"": ""array summary"",
107+
""description"": ""array description"",
108+
""value"": [
109+
1
110+
]
102111
}
103112
}
104113
}

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)