Skip to content

Commit eda97ad

Browse files
committed
Load "x-examples" as Examples; store and retrieve from temp storage and append it to the media type object
1 parent cfa49e9 commit eda97ad

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

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

Lines changed: 48 additions & 7 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;
@@ -28,6 +28,10 @@ internal static partial class OpenApiV2Deserializer
2828
"examples",
2929
LoadExamples
3030
},
31+
{
32+
"x-examples",
33+
LoadExamplesExtension
34+
},
3135
{
3236
"schema",
3337
(o, n) => n.Context.SetTempStorage(TempStorageKeys.ResponseSchema, LoadSchema(n), o)
@@ -37,7 +41,7 @@ internal static partial class OpenApiV2Deserializer
3741
private static readonly PatternFieldMap<OpenApiResponse> _responsePatternFields =
3842
new()
3943
{
40-
{s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, LoadExtension(p, n))}
44+
{s => s.StartsWith("x-") && !s.Equals("x-examples"), (o, p, n) => o.AddExtension(p, LoadExtension(p, n))}
4145
};
4246

4347
private static readonly AnyFieldMap<OpenApiMediaType> _mediaTypeAnyFields =
@@ -69,6 +73,7 @@ private static void ProcessProduces(MapNode mapNode, OpenApiResponse response, P
6973
?? context.DefaultContentType ?? new List<string> { "application/octet-stream" };
7074

7175
var schema = context.GetFromTempStorage<OpenApiSchema>(TempStorageKeys.ResponseSchema, response);
76+
var examples = context.GetFromTempStorage<Dictionary<string, OpenApiExample>>(TempStorageKeys.Examples, response);
7277

7378
foreach (var produce in produces)
7479
{
@@ -84,20 +89,58 @@ private static void ProcessProduces(MapNode mapNode, OpenApiResponse response, P
8489
{
8590
var mediaType = new OpenApiMediaType
8691
{
87-
Schema = schema
92+
Schema = schema,
93+
Examples = examples
8894
};
8995

9096
response.Content.Add(produce, mediaType);
9197
}
9298
}
9399

94100
context.SetTempStorage(TempStorageKeys.ResponseSchema, null, response);
101+
context.SetTempStorage(TempStorageKeys.Examples, null, response);
95102
context.SetTempStorage(TempStorageKeys.ResponseProducesSet, true, response);
96103
}
97104

105+
private static void LoadExamplesExtension(OpenApiResponse response, ParseNode node)
106+
{
107+
var mapNode = node.CheckMapNode("x-examples");
108+
var examples = new Dictionary<string, OpenApiExample>();
109+
110+
foreach (var examplesNode in mapNode)
111+
{
112+
// Load the media type node as an OpenApiExample object
113+
var example = new OpenApiExample();
114+
var exampleNode = examplesNode.Value.CheckMapNode(examplesNode.Name);
115+
foreach (var valueNode in exampleNode)
116+
{
117+
switch (valueNode.Name)
118+
{
119+
case "summary":
120+
example.Summary = valueNode.Value.GetScalarValue();
121+
break;
122+
case "description":
123+
example.Description = valueNode.Value.GetScalarValue();
124+
break;
125+
case "value":
126+
example.Value = valueNode.Value.CreateAny();
127+
break;
128+
case "externalValue":
129+
example.ExternalValue = valueNode.Value.GetScalarValue();
130+
break;
131+
}
132+
}
133+
134+
examples.Add(examplesNode.Name, example);
135+
}
136+
137+
node.Context.SetTempStorage(TempStorageKeys.Examples, examples, response);
138+
}
139+
98140
private static void LoadExamples(OpenApiResponse response, ParseNode node)
99141
{
100142
var mapNode = node.CheckMapNode("examples");
143+
101144
foreach (var mediaTypeNode in mapNode)
102145
{
103146
LoadExample(response, mediaTypeNode.Name, mediaTypeNode.Value);
@@ -108,10 +151,7 @@ private static void LoadExample(OpenApiResponse response, string mediaType, Pars
108151
{
109152
var exampleNode = node.CreateAny();
110153

111-
if (response.Content == null)
112-
{
113-
response.Content = new Dictionary<string, OpenApiMediaType>();
114-
}
154+
response.Content ??= new Dictionary<string, OpenApiMediaType>();
115155

116156
OpenApiMediaType mediaTypeObject;
117157
if (response.Content.TryGetValue(mediaType, out var value))
@@ -141,6 +181,7 @@ public static OpenApiResponse LoadResponse(ParseNode node)
141181
}
142182

143183
var response = new OpenApiResponse();
184+
144185
foreach (var property in mapNode)
145186
{
146187
property.ParseField(response, _responseFixedFields, _responsePatternFields);

0 commit comments

Comments
 (0)