Skip to content

Commit 601ebf7

Browse files
Fix to properly assign schemas to content of all media types of all responses
1 parent c08eb18 commit 601ebf7

File tree

4 files changed

+114
-16
lines changed

4 files changed

+114
-16
lines changed

src/Microsoft.OpenApi.Readers/V2/OpenApiResponseDeserializer.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,35 +63,39 @@ internal static partial class OpenApiV2Deserializer
6363
private static void ProcessProduces(MapNode mapNode, OpenApiResponse response, ParsingContext context)
6464
{
6565
var produces = context.GetFromTempStorage<List<string>>(TempStorageKeys.OperationProduces) ??
66-
context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalProduces) ?? new List<string>();
66+
context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalProduces);
6767

6868
if (response.Content == null)
6969
{
7070
response.Content = new Dictionary<string, OpenApiMediaType>();
7171
}
7272

73-
foreach (var produce in produces)
73+
if (produces != null && produces.Count > 0)
7474
{
75-
var schema = context.GetFromTempStorage<OpenApiSchema>(TempStorageKeys.ResponseSchema, response);
76-
context.SetTempStorage(TempStorageKeys.ResponseSchema, null, response);
77-
78-
if (response.Content.ContainsKey(produce) && response.Content[produce] != null)
75+
foreach (var produce in produces)
7976
{
80-
if (schema != null)
77+
var schema = context.GetFromTempStorage<OpenApiSchema>(TempStorageKeys.ResponseSchema, response);
78+
79+
if (response.Content.ContainsKey(produce) && response.Content[produce] != null)
8180
{
82-
response.Content[produce].Schema = schema;
83-
ProcessAnyFields(mapNode, response.Content[produce], _mediaTypeAnyFields);
81+
if (schema != null)
82+
{
83+
response.Content[produce].Schema = schema;
84+
ProcessAnyFields(mapNode, response.Content[produce], _mediaTypeAnyFields);
85+
}
8486
}
85-
}
86-
else
87-
{
88-
var mediaType = new OpenApiMediaType
87+
else
8988
{
90-
Schema = schema
91-
};
89+
var mediaType = new OpenApiMediaType
90+
{
91+
Schema = schema
92+
};
9293

93-
response.Content.Add(produce, mediaType);
94+
response.Content.Add(produce, mediaType);
95+
}
9496
}
97+
98+
context.SetTempStorage(TempStorageKeys.ResponseSchema, null, response);
9599
}
96100
}
97101

test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
<SignAssembly>true</SignAssembly>
1313
<AssemblyOriginatorKeyFile>..\..\src\Microsoft.OpenApi.snk</AssemblyOriginatorKeyFile>
1414
</PropertyGroup>
15+
<ItemGroup>
16+
<None Remove="V2Tests\Samples\multipleProduces.json" />
17+
</ItemGroup>
1518
<ItemGroup>
1619
<EmbeddedResource Include="OpenApiReaderTests\Samples\unsupported.v1.yaml">
1720
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
@@ -97,6 +100,9 @@
97100
<EmbeddedResource Include="V2Tests\Samples\OpenApiSecurityScheme\oauth2PasswordSecurityScheme.yaml">
98101
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
99102
</EmbeddedResource>
103+
<EmbeddedResource Include="V2Tests\Samples\multipleProduces.json">
104+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
105+
</EmbeddedResource>
100106
<EmbeddedResource Include="V2Tests\Samples\twoResponses.json">
101107
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
102108
</EmbeddedResource>

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Globalization;
66
using System.IO;
7+
using System.Linq;
78
using System.Threading;
89
using FluentAssertions;
910
using Microsoft.OpenApi.Any;
@@ -238,5 +239,30 @@ public void ShouldParseProducesInAnyOrder()
238239
}
239240
}
240241
}
242+
243+
[Fact]
244+
public void ShouldAssignSchemaToAllResponses()
245+
{
246+
OpenApiDocument document;
247+
OpenApiDiagnostic diagnostic;
248+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "multipleProduces.json")))
249+
{
250+
document = new OpenApiStreamReader().Read(stream, out diagnostic);
251+
}
252+
253+
Assert.Equal(OpenApiSpecVersion.OpenApi2_0, diagnostic.SpecificationVersion);
254+
255+
var responses = document.Paths["/items"].Operations[OperationType.Get].Responses;
256+
foreach (var content in responses.Values.Select(r => r.Content))
257+
{
258+
var json = content["application/json"];
259+
Assert.NotNull(json);
260+
Assert.NotNull(json.Schema);
261+
262+
var xml = content["application/xml"];
263+
Assert.NotNull(xml);
264+
Assert.NotNull(xml.Schema);
265+
}
266+
}
241267
}
242268
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"swagger": "2.0",
3+
"info": {
4+
"title": "Multiple produces",
5+
"version": "1.0.0"
6+
},
7+
"schemes": [
8+
"https"
9+
],
10+
"basePath": "/",
11+
"paths": {
12+
"/items": {
13+
"get": {
14+
"produces": [
15+
"application/json",
16+
"application/xml"
17+
],
18+
"responses": {
19+
"200": {
20+
"description": "An OK response",
21+
"schema": {
22+
"type": "array",
23+
"items": {
24+
"$ref": "#/definitions/Item"
25+
}
26+
}
27+
},
28+
"default": {
29+
"description": "An error response",
30+
"schema": {
31+
"$ref": "#/definitions/Error"
32+
}
33+
}
34+
}
35+
}
36+
}
37+
},
38+
"definitions": {
39+
"Item": {
40+
"properties": {
41+
"id": {
42+
"type": "string",
43+
"description": "Item identifier."
44+
}
45+
}
46+
},
47+
"Error": {
48+
"properties": {
49+
"code": {
50+
"type": "integer",
51+
"format": "int32"
52+
},
53+
"message": {
54+
"type": "string"
55+
},
56+
"fields": {
57+
"type": "string"
58+
}
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)