1- // Copyright (c) Microsoft Corporation. All rights reserved.
1+ // Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT license.
33
4+ using System ;
45using System . Collections . Generic ;
56using Json . Schema ;
67using Microsoft . OpenApi . Extensions ;
@@ -29,6 +30,10 @@ internal static partial class OpenApiV2Deserializer
2930 "examples" ,
3031 LoadExamples
3132 } ,
33+ {
34+ "x-examples" ,
35+ LoadExamplesExtension
36+ } ,
3237 {
3338 "schema" ,
3439 ( o , n ) => n . Context . SetTempStorage ( TempStorageKeys . ResponseSchema , LoadSchema ( n ) , o )
@@ -38,7 +43,7 @@ internal static partial class OpenApiV2Deserializer
3843 private static readonly PatternFieldMap < OpenApiResponse > _responsePatternFields =
3944 new ( )
4045 {
41- { s => s . StartsWith ( "x-" ) , ( o , p , n ) => o . AddExtension ( p , LoadExtension ( p , n ) ) }
46+ { s => s . StartsWith ( "x-" ) && ! s . Equals ( OpenApiConstants . ExamplesExtension ) , ( o , p , n ) => o . AddExtension ( p , LoadExtension ( p , n ) ) }
4247 } ;
4348
4449 private static readonly AnyFieldMap < OpenApiMediaType > _mediaTypeAnyFields =
@@ -70,6 +75,8 @@ private static void ProcessProduces(MapNode mapNode, OpenApiResponse response, P
7075 ?? context . DefaultContentType ?? new List < string > { "application/octet-stream" } ;
7176
7277 var schema = context . GetFromTempStorage < JsonSchema > ( TempStorageKeys . ResponseSchema , response ) ;
78+ var examples = context . GetFromTempStorage < Dictionary < string , OpenApiExample > > ( TempStorageKeys . Examples , response )
79+ ?? new Dictionary < string , OpenApiExample > ( ) ;
7380
7481 foreach ( var produce in produces )
7582 {
@@ -85,20 +92,58 @@ private static void ProcessProduces(MapNode mapNode, OpenApiResponse response, P
8592 {
8693 var mediaType = new OpenApiMediaType
8794 {
88- Schema = schema
95+ Schema = schema ,
96+ Examples = examples
8997 } ;
9098
9199 response . Content . Add ( produce , mediaType ) ;
92100 }
93101 }
94102
95103 context . SetTempStorage ( TempStorageKeys . ResponseSchema , null , response ) ;
104+ context . SetTempStorage ( TempStorageKeys . Examples , null , response ) ;
96105 context . SetTempStorage ( TempStorageKeys . ResponseProducesSet , true , response ) ;
97106 }
98107
108+ private static void LoadExamplesExtension ( OpenApiResponse response , ParseNode node )
109+ {
110+ var mapNode = node . CheckMapNode ( OpenApiConstants . ExamplesExtension ) ;
111+ var examples = new Dictionary < string , OpenApiExample > ( ) ;
112+
113+ foreach ( var examplesNode in mapNode )
114+ {
115+ // Load the media type node as an OpenApiExample object
116+ var example = new OpenApiExample ( ) ;
117+ var exampleNode = examplesNode . Value . CheckMapNode ( examplesNode . Name ) ;
118+ foreach ( var valueNode in exampleNode )
119+ {
120+ switch ( valueNode . Name . ToLowerInvariant ( ) )
121+ {
122+ case "summary" :
123+ example . Summary = valueNode . Value . GetScalarValue ( ) ;
124+ break ;
125+ case "description" :
126+ example . Description = valueNode . Value . GetScalarValue ( ) ;
127+ break ;
128+ case "value" :
129+ example . Value = valueNode . Value . CreateAny ( ) ;
130+ break ;
131+ case "externalValue" :
132+ example . ExternalValue = valueNode . Value . GetScalarValue ( ) ;
133+ break ;
134+ }
135+ }
136+
137+ examples . Add ( examplesNode . Name , example ) ;
138+ }
139+
140+ node . Context . SetTempStorage ( TempStorageKeys . Examples , examples , response ) ;
141+ }
142+
99143 private static void LoadExamples ( OpenApiResponse response , ParseNode node )
100144 {
101145 var mapNode = node . CheckMapNode ( "examples" ) ;
146+
102147 foreach ( var mediaTypeNode in mapNode )
103148 {
104149 LoadExample ( response , mediaTypeNode . Name , mediaTypeNode . Value ) ;
@@ -109,10 +154,7 @@ private static void LoadExample(OpenApiResponse response, string mediaType, Pars
109154 {
110155 var exampleNode = node . CreateAny ( ) ;
111156
112- if ( response . Content == null )
113- {
114- response . Content = new Dictionary < string , OpenApiMediaType > ( ) ;
115- }
157+ response . Content ??= new Dictionary < string , OpenApiMediaType > ( ) ;
116158
117159 OpenApiMediaType mediaTypeObject ;
118160 if ( response . Content . TryGetValue ( mediaType , out var value ) )
@@ -142,6 +184,7 @@ public static OpenApiResponse LoadResponse(ParseNode node)
142184 }
143185
144186 var response = new OpenApiResponse ( ) ;
187+
145188 foreach ( var property in mapNode )
146189 {
147190 property . ParseField ( response , _responseFixedFields , _responsePatternFields ) ;
0 commit comments