From d93c6c30aad36799536e9960bc8a782c0ff84ed1 Mon Sep 17 00:00:00 2001 From: "vitaly.melnik" Date: Fri, 22 Aug 2025 20:35:13 +0300 Subject: [PATCH] Fix missing examples when one example is with an empty array. --- .../Models/OpenApiExample.cs | 5 +++- .../Models/OpenApiMediaType.cs | 30 +------------------ .../V3Tests/OpenApiMediaTypeTests.cs | 9 ++++++ .../examplesWithEmptyArray.json | 7 +++++ 4 files changed, 21 insertions(+), 30 deletions(-) diff --git a/src/Microsoft.OpenApi/Models/OpenApiExample.cs b/src/Microsoft.OpenApi/Models/OpenApiExample.cs index 0ebea9da9..b23befd08 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiExample.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiExample.cs @@ -70,7 +70,10 @@ private void SerializeInternal(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); diff --git a/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs b/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs index 7520678e1..b901eacd1 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs @@ -95,7 +95,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version // examples if (Examples != null && Examples.Any()) { - SerializeExamples(writer, Examples, callback); + writer.WriteOptionalMap(OpenApiConstants.Examples, Examples, callback); } // encoding @@ -114,33 +114,5 @@ public virtual void SerializeAsV2(IOpenApiWriter writer) { // Media type does not exist in V2. } - - private static void SerializeExamples(IOpenApiWriter writer, IDictionary examples, Action callback) - { - /* 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 JsonArray arr && arr.Count == 0 - ); - - if (hasEmptyArray) - { - writer.WritePropertyName(OpenApiConstants.Examples); - writer.WriteStartObject(); - foreach (var kvp in examples.Where(static kvp => kvp.Value.Value is JsonArray 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, callback); - } - } } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs index a86a79412..dd669c889 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs @@ -87,7 +87,16 @@ public async Task 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 + ] } } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/Samples/OpenApiMediaType/examplesWithEmptyArray.json b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/Samples/OpenApiMediaType/examplesWithEmptyArray.json index 0d13dcaf2..c2b0a09e4 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/Samples/OpenApiMediaType/examplesWithEmptyArray.json +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/Samples/OpenApiMediaType/examplesWithEmptyArray.json @@ -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 ] } } } \ No newline at end of file