Skip to content

Commit d96749f

Browse files
Merge pull request #1768 from microsoft/mk/fix-empty-example-array
Fix example value of an empty array disappears after serialization
2 parents d3df64d + 27258e9 commit d96749f

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

src/Microsoft.OpenApi/Models/OpenApiMediaType.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
using System.Collections.Generic;
5+
using System.Linq;
56
using Microsoft.OpenApi.Any;
67
using Microsoft.OpenApi.Interfaces;
78
using Microsoft.OpenApi.Writers;
@@ -76,7 +77,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
7677
writer.WriteOptionalObject(OpenApiConstants.Example, Example, (w, e) => w.WriteAny(e));
7778

7879
// examples
79-
writer.WriteOptionalMap(OpenApiConstants.Examples, Examples, (w, e) => e.SerializeAsV3(w));
80+
SerializeExamples(writer, Examples);
8081

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

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
using System.IO;
55
using FluentAssertions;
66
using Microsoft.OpenApi.Any;
7+
using Microsoft.OpenApi.Extensions;
78
using Microsoft.OpenApi.Models;
89
using Microsoft.OpenApi.Readers.ParseNodes;
910
using Microsoft.OpenApi.Readers.V3;
11+
using Microsoft.OpenApi.Tests;
1012
using Xunit;
1113

1214
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
@@ -77,5 +79,43 @@ public void ParseMediaTypeWithExamplesShouldSucceed()
7779
}
7880
});
7981
}
82+
83+
[Fact]
84+
public void ParseMediaTypeWithEmptyArrayInExamplesWorks()
85+
{
86+
// Arrange
87+
var expected = @"{
88+
""schema"": {
89+
""type"": ""array"",
90+
""items"": {
91+
""type"": ""object"",
92+
""properties"": {
93+
""id"": {
94+
""type"": ""string""
95+
}
96+
}
97+
}
98+
},
99+
""examples"": {
100+
""Success response - no results"": {
101+
""value"": [ ]
102+
}
103+
}
104+
}
105+
";
106+
MapNode node;
107+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "examplesWithEmptyArray.json")))
108+
{
109+
node = TestHelper.CreateYamlMapNode(stream);
110+
}
111+
112+
// Act
113+
var mediaType = OpenApiV3Deserializer.LoadMediaType(node);
114+
var serialized = mediaType.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
115+
116+
// Assert
117+
serialized.MakeLineBreaksEnvironmentNeutral()
118+
.Should().BeEquivalentTo(expected.MakeLineBreaksEnvironmentNeutral());
119+
}
80120
}
81121
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"schema": {
3+
"type": "array",
4+
"items": {
5+
"type": "object",
6+
"properties": {
7+
"id": {
8+
"type": "string"
9+
}
10+
}
11+
}
12+
},
13+
"examples": {
14+
"Success response - no results": {
15+
"value": []
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)