Skip to content

Commit 845f314

Browse files
committed
Refactor and clean up code
1 parent dc319ec commit 845f314

File tree

3 files changed

+45
-77
lines changed

3 files changed

+45
-77
lines changed

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

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,47 +73,35 @@ private static void ProcessProduces(MapNode mapNode, OpenApiResponse response, P
7373
}
7474

7575
var produces = context.GetFromTempStorage<List<string>>(TempStorageKeys.OperationProduces)
76-
?? context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalProduces);
76+
?? context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalProduces)
77+
?? new List<string> { "application/octet-stream" };
7778

78-
if (produces != null)
79+
var schema = context.GetFromTempStorage<OpenApiSchema>(TempStorageKeys.ResponseSchema, response);
80+
81+
foreach (var produce in produces)
7982
{
80-
var schema = context.GetFromTempStorage<OpenApiSchema>(TempStorageKeys.ResponseSchema, response);
8183

82-
if (produces.Count == 0 && schema != null)
84+
if (response.Content.TryGetValue(produce, out var produceValue))
8385
{
84-
var mediaType = new OpenApiMediaType
86+
if (schema != null)
8587
{
86-
Schema = schema
87-
};
88-
89-
response.Content.Add("application/octet-stream", mediaType);
88+
produceValue.Schema = schema;
89+
ProcessAnyFields(mapNode, produceValue, _mediaTypeAnyFields);
90+
}
9091
}
91-
92-
foreach (var produce in produces)
92+
else
9393
{
94-
95-
if (response.Content.TryGetValue(produce, out var produceValue))
96-
{
97-
if (schema != null)
98-
{
99-
produceValue.Schema = schema;
100-
ProcessAnyFields(mapNode, produceValue, _mediaTypeAnyFields);
101-
}
102-
}
103-
else
94+
var mediaType = new OpenApiMediaType
10495
{
105-
var mediaType = new OpenApiMediaType
106-
{
107-
Schema = schema
108-
};
96+
Schema = schema
97+
};
10998

110-
response.Content.Add(produce, mediaType);
111-
}
99+
response.Content.Add(produce, mediaType);
112100
}
113-
114-
context.SetTempStorage(TempStorageKeys.ResponseSchema, null, response);
115-
context.SetTempStorage(TempStorageKeys.ResponseProducesSet, true, response);
116101
}
102+
103+
context.SetTempStorage(TempStorageKeys.ResponseSchema, null, response);
104+
context.SetTempStorage(TempStorageKeys.ResponseProducesSet, true, response);
117105
}
118106

119107
private static void LoadExamples(OpenApiResponse response, ParseNode node)

src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ private void ResolveTags(IList<OpenApiTag> tags)
256256

257257
private T ResolveReference<T>(OpenApiReference reference) where T : class, IOpenApiReferenceable, new()
258258
{
259-
if (string.IsNullOrEmpty(reference.ExternalResource))
259+
if (string.IsNullOrEmpty(reference?.ExternalResource))
260260
{
261261
try
262262
{

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

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

44
using System.Collections.Generic;
@@ -163,45 +163,25 @@ public void ShouldParseProducesInAnyOrder()
163163
var reader = new OpenApiStreamReader();
164164
var doc = reader.Read(stream, out var diagnostic);
165165

166-
var successSchema = new OpenApiSchema()
166+
var okSchema = new OpenApiSchema()
167167
{
168-
Type = "array",
169168
Reference = new OpenApiReference
170169
{
171170
Type = ReferenceType.Schema,
172171
Id = "Item",
173172
HostDocument = doc
174173
},
175-
Items = new OpenApiSchema()
174+
Properties = new Dictionary<string, OpenApiSchema>()
176175
{
177-
Reference = new OpenApiReference()
178-
{
179-
Type = ReferenceType.Schema,
180-
Id = "Item",
181-
HostDocument = doc
176+
{ "id", new OpenApiSchema()
177+
{
178+
Type = "string",
179+
Description = "Item identifier."
180+
}
182181
}
183182
}
184183
};
185184

186-
var okSchema = new OpenApiSchema()
187-
{
188-
Reference = new OpenApiReference
189-
{
190-
Type = ReferenceType.Schema,
191-
Id = "Item",
192-
HostDocument = doc
193-
},
194-
Properties = new Dictionary<string, OpenApiSchema>()
195-
{
196-
{ "id", new OpenApiSchema()
197-
{
198-
Type = "string",
199-
Description = "Item identifier."
200-
}
201-
}
202-
}
203-
};
204-
205185
var errorSchema = new OpenApiSchema()
206186
{
207187
Reference = new OpenApiReference
@@ -211,24 +191,24 @@ public void ShouldParseProducesInAnyOrder()
211191
HostDocument = doc
212192
},
213193
Properties = new Dictionary<string, OpenApiSchema>()
214-
{
215-
{ "code", new OpenApiSchema()
216-
{
217-
Type = "integer",
218-
Format = "int32"
219-
}
220-
},
221-
{ "message", new OpenApiSchema()
222-
{
223-
Type = "string"
224-
}
225-
},
226-
{ "fields", new OpenApiSchema()
227-
{
228-
Type = "string"
229-
}
230-
}
231-
}
194+
{
195+
{ "code", new OpenApiSchema()
196+
{
197+
Type = "integer",
198+
Format = "int32"
199+
}
200+
},
201+
{ "message", new OpenApiSchema()
202+
{
203+
Type = "string"
204+
}
205+
},
206+
{ "fields", new OpenApiSchema()
207+
{
208+
Type = "string"
209+
}
210+
}
211+
}
232212
};
233213

234214
var okMediaType = new OpenApiMediaType

0 commit comments

Comments
 (0)