Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Microsoft.OpenApi/Models/OpenApiResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
// description
writer.WriteRequiredProperty(OpenApiConstants.Description, Description);

var extensionsClone = new Dictionary<string, IOpenApiExtension>(Extensions);

if (Content != null)
{
var mediatype = Content.FirstOrDefault();
Expand Down Expand Up @@ -152,14 +154,23 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)

writer.WriteEndObject();
}

writer.WriteExtensions(mediatype.Value.Extensions, OpenApiSpecVersion.OpenApi2_0);

foreach (var key in mediatype.Value.Extensions.Keys)
{
// The extension will already have been serialized as part of the call above,
// so remove it from the cloned collection so we don't write it again.
extensionsClone.Remove(key);
}
}
}

// headers
writer.WriteOptionalMap(OpenApiConstants.Headers, Headers, (w, h) => h.SerializeAsV2(w));

// extension
writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi2_0);
writer.WriteExtensions(extensionsClone, OpenApiSpecVersion.OpenApi2_0);

writer.WriteEndObject();
}
Expand Down
16 changes: 13 additions & 3 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiResponseTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System.Collections.Generic;
using System.Globalization;
using System.IO;
using FluentAssertions;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Writers;
using Xunit;
Expand Down Expand Up @@ -33,7 +35,11 @@ public class OpenApiResponseTests
Reference = new OpenApiReference {Type = ReferenceType.Schema, Id = "customType"}
}
},
Example = new OpenApiString("Blabla")
Example = new OpenApiString("Blabla"),
Extensions = new Dictionary<string, IOpenApiExtension>
{
["myextension"] = new OpenApiString("myextensionvalue"),
},
}
},
Headers =
Expand Down Expand Up @@ -158,7 +164,8 @@ public void SerializeAdvancedResponseAsV3JsonWorks()
""$ref"": ""#/components/schemas/customType""
}
},
""example"": ""Blabla""
""example"": ""Blabla"",
""myextension"": ""myextensionvalue""
}
}
}";
Expand Down Expand Up @@ -193,7 +200,8 @@ public void SerializeAdvancedResponseAsV3YamlWorks()
type: array
items:
$ref: '#/components/schemas/customType'
example: Blabla";
example: Blabla
myextension: myextensionvalue";

// Act
var actual = AdvancedResponse.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);
Expand All @@ -219,6 +227,7 @@ public void SerializeAdvancedResponseAsV2JsonWorks()
""examples"": {
""text/plain"": ""Blabla""
},
""myextension"": ""myextensionvalue"",
""headers"": {
""X-Rate-Limit-Limit"": {
""description"": ""The number of allowed requests in the current period"",
Expand Down Expand Up @@ -252,6 +261,7 @@ public void SerializeAdvancedResponseAsV2YamlWorks()
$ref: '#/definitions/customType'
examples:
text/plain: Blabla
myextension: myextensionvalue
headers:
X-Rate-Limit-Limit:
description: The number of allowed requests in the current period
Expand Down