Skip to content

Commit

Permalink
Merge pull request #1768 from microsoft/mk/fix-empty-example-array
Browse files Browse the repository at this point in the history
Fix example value of an empty array disappears after serialization
  • Loading branch information
MaggieKimani1 authored Aug 5, 2024
2 parents d3df64d + 27258e9 commit d96749f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/Microsoft.OpenApi/Models/OpenApiMediaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
Expand Down Expand Up @@ -76,7 +77,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
writer.WriteOptionalObject(OpenApiConstants.Example, Example, (w, e) => w.WriteAny(e));

// examples
writer.WriteOptionalMap(OpenApiConstants.Examples, Examples, (w, e) => e.SerializeAsV3(w));
SerializeExamples(writer, Examples);

// encoding
writer.WriteOptionalMap(OpenApiConstants.Encoding, Encoding, (w, e) => e.SerializeAsV3(w));
Expand All @@ -94,5 +95,33 @@ 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 @@ -4,9 +4,11 @@
using System.IO;
using FluentAssertions;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;
using Microsoft.OpenApi.Readers.V3;
using Microsoft.OpenApi.Tests;
using Xunit;

namespace Microsoft.OpenApi.Readers.Tests.V3Tests
Expand Down Expand Up @@ -77,5 +79,43 @@ public void ParseMediaTypeWithExamplesShouldSucceed()
}
});
}

[Fact]
public void ParseMediaTypeWithEmptyArrayInExamplesWorks()
{
// Arrange
var expected = @"{
""schema"": {
""type"": ""array"",
""items"": {
""type"": ""object"",
""properties"": {
""id"": {
""type"": ""string""
}
}
}
},
""examples"": {
""Success response - no results"": {
""value"": [ ]
}
}
}
";
MapNode node;
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "examplesWithEmptyArray.json")))
{
node = TestHelper.CreateYamlMapNode(stream);
}

// Act
var mediaType = OpenApiV3Deserializer.LoadMediaType(node);
var serialized = mediaType.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);

// Assert
serialized.MakeLineBreaksEnvironmentNeutral()
.Should().BeEquivalentTo(expected.MakeLineBreaksEnvironmentNeutral());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
}
}
},
"examples": {
"Success response - no results": {
"value": []
}
}
}

0 comments on commit d96749f

Please sign in to comment.