Skip to content

Commit 6f5e63c

Browse files
authored
Merge pull request #460 from ozziepeeps/458
Fixes #458
2 parents 8851cf3 + 542bddc commit 6f5e63c

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/Microsoft.OpenApi/Models/OpenApiResponse.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
124124
// description
125125
writer.WriteRequiredProperty(OpenApiConstants.Description, Description);
126126

127+
var extensionsClone = new Dictionary<string, IOpenApiExtension>(Extensions);
128+
127129
if (Content != null)
128130
{
129131
var mediatype = Content.FirstOrDefault();
@@ -152,14 +154,23 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
152154

153155
writer.WriteEndObject();
154156
}
157+
158+
writer.WriteExtensions(mediatype.Value.Extensions, OpenApiSpecVersion.OpenApi2_0);
159+
160+
foreach (var key in mediatype.Value.Extensions.Keys)
161+
{
162+
// The extension will already have been serialized as part of the call above,
163+
// so remove it from the cloned collection so we don't write it again.
164+
extensionsClone.Remove(key);
165+
}
155166
}
156167
}
157168

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

161172
// extension
162-
writer.WriteExtensions(Extensions, OpenApiSpecVersion.OpenApi2_0);
173+
writer.WriteExtensions(extensionsClone, OpenApiSpecVersion.OpenApi2_0);
163174

164175
writer.WriteEndObject();
165176
}

test/Microsoft.OpenApi.Tests/Models/OpenApiResponseTests.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System.Collections.Generic;
45
using System.Globalization;
56
using System.IO;
67
using FluentAssertions;
78
using Microsoft.OpenApi.Any;
89
using Microsoft.OpenApi.Extensions;
10+
using Microsoft.OpenApi.Interfaces;
911
using Microsoft.OpenApi.Models;
1012
using Microsoft.OpenApi.Writers;
1113
using Xunit;
@@ -33,7 +35,11 @@ public class OpenApiResponseTests
3335
Reference = new OpenApiReference {Type = ReferenceType.Schema, Id = "customType"}
3436
}
3537
},
36-
Example = new OpenApiString("Blabla")
38+
Example = new OpenApiString("Blabla"),
39+
Extensions = new Dictionary<string, IOpenApiExtension>
40+
{
41+
["myextension"] = new OpenApiString("myextensionvalue"),
42+
},
3743
}
3844
},
3945
Headers =
@@ -158,7 +164,8 @@ public void SerializeAdvancedResponseAsV3JsonWorks()
158164
""$ref"": ""#/components/schemas/customType""
159165
}
160166
},
161-
""example"": ""Blabla""
167+
""example"": ""Blabla"",
168+
""myextension"": ""myextensionvalue""
162169
}
163170
}
164171
}";
@@ -193,7 +200,8 @@ public void SerializeAdvancedResponseAsV3YamlWorks()
193200
type: array
194201
items:
195202
$ref: '#/components/schemas/customType'
196-
example: Blabla";
203+
example: Blabla
204+
myextension: myextensionvalue";
197205

198206
// Act
199207
var actual = AdvancedResponse.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);
@@ -219,6 +227,7 @@ public void SerializeAdvancedResponseAsV2JsonWorks()
219227
""examples"": {
220228
""text/plain"": ""Blabla""
221229
},
230+
""myextension"": ""myextensionvalue"",
222231
""headers"": {
223232
""X-Rate-Limit-Limit"": {
224233
""description"": ""The number of allowed requests in the current period"",
@@ -252,6 +261,7 @@ public void SerializeAdvancedResponseAsV2YamlWorks()
252261
$ref: '#/definitions/customType'
253262
examples:
254263
text/plain: Blabla
264+
myextension: myextensionvalue
255265
headers:
256266
X-Rate-Limit-Limit:
257267
description: The number of allowed requests in the current period

0 commit comments

Comments
 (0)