1
- // Copyright (c) Microsoft Corporation. All rights reserved.
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
4
4
using System . Collections . Generic ;
@@ -28,6 +28,10 @@ internal static partial class OpenApiV2Deserializer
28
28
"examples" ,
29
29
LoadExamples
30
30
} ,
31
+ {
32
+ "x-examples" ,
33
+ LoadExamplesExtension
34
+ } ,
31
35
{
32
36
"schema" ,
33
37
( o , n ) => n . Context . SetTempStorage ( TempStorageKeys . ResponseSchema , LoadSchema ( n ) , o )
@@ -37,7 +41,7 @@ internal static partial class OpenApiV2Deserializer
37
41
private static readonly PatternFieldMap < OpenApiResponse > _responsePatternFields =
38
42
new ( )
39
43
{
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 ) ) }
41
45
} ;
42
46
43
47
private static readonly AnyFieldMap < OpenApiMediaType > _mediaTypeAnyFields =
@@ -69,6 +73,7 @@ private static void ProcessProduces(MapNode mapNode, OpenApiResponse response, P
69
73
?? context . DefaultContentType ?? new List < string > { "application/octet-stream" } ;
70
74
71
75
var schema = context . GetFromTempStorage < OpenApiSchema > ( TempStorageKeys . ResponseSchema , response ) ;
76
+ var examples = context . GetFromTempStorage < Dictionary < string , OpenApiExample > > ( TempStorageKeys . Examples , response ) ;
72
77
73
78
foreach ( var produce in produces )
74
79
{
@@ -84,20 +89,58 @@ private static void ProcessProduces(MapNode mapNode, OpenApiResponse response, P
84
89
{
85
90
var mediaType = new OpenApiMediaType
86
91
{
87
- Schema = schema
92
+ Schema = schema ,
93
+ Examples = examples
88
94
} ;
89
95
90
96
response . Content . Add ( produce , mediaType ) ;
91
97
}
92
98
}
93
99
94
100
context . SetTempStorage ( TempStorageKeys . ResponseSchema , null , response ) ;
101
+ context . SetTempStorage ( TempStorageKeys . Examples , null , response ) ;
95
102
context . SetTempStorage ( TempStorageKeys . ResponseProducesSet , true , response ) ;
96
103
}
97
104
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
+
98
140
private static void LoadExamples ( OpenApiResponse response , ParseNode node )
99
141
{
100
142
var mapNode = node . CheckMapNode ( "examples" ) ;
143
+
101
144
foreach ( var mediaTypeNode in mapNode )
102
145
{
103
146
LoadExample ( response , mediaTypeNode . Name , mediaTypeNode . Value ) ;
@@ -108,10 +151,7 @@ private static void LoadExample(OpenApiResponse response, string mediaType, Pars
108
151
{
109
152
var exampleNode = node . CreateAny ( ) ;
110
153
111
- if ( response . Content == null )
112
- {
113
- response . Content = new Dictionary < string , OpenApiMediaType > ( ) ;
114
- }
154
+ response . Content ??= new Dictionary < string , OpenApiMediaType > ( ) ;
115
155
116
156
OpenApiMediaType mediaTypeObject ;
117
157
if ( response . Content . TryGetValue ( mediaType , out var value ) )
@@ -141,6 +181,7 @@ public static OpenApiResponse LoadResponse(ParseNode node)
141
181
}
142
182
143
183
var response = new OpenApiResponse ( ) ;
184
+
144
185
foreach ( var property in mapNode )
145
186
{
146
187
property . ParseField ( response , _responseFixedFields , _responsePatternFields ) ;
0 commit comments