11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT license.
33
4- using System ;
54using System . Collections . Generic ;
65using System . Linq ;
76using Microsoft . OpenApi . Exceptions ;
@@ -25,20 +24,29 @@ public static class OpenApiReferencableExtensions
2524 public static IOpenApiReferenceable ResolveReference ( this IOpenApiReferenceable element , string jsonPointer )
2625 {
2726 if ( jsonPointer == "/" )
27+ {
2828 return element ;
29+ }
30+ if ( string . IsNullOrEmpty ( jsonPointer ) )
31+ {
32+ throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
33+ }
34+ var jsonPointerTokens = jsonPointer . Split ( '/' ) ;
35+ var propertyName = jsonPointerTokens . ElementAtOrDefault ( 1 ) ;
36+ var mapKey = jsonPointerTokens . ElementAtOrDefault ( 2 ) ;
2937 try
3038 {
3139 if ( element . GetType ( ) == typeof ( OpenApiHeader ) )
3240 {
33- return ResolveReferenceOnHeaderElement ( ( OpenApiHeader ) element , jsonPointer ) ;
41+ return ResolveReferenceOnHeaderElement ( ( OpenApiHeader ) element , propertyName , mapKey , jsonPointer ) ;
3442 }
3543 if ( element . GetType ( ) == typeof ( OpenApiParameter ) )
3644 {
37- return ResolveReferenceOnParameterElement ( ( OpenApiParameter ) element , jsonPointer ) ;
45+ return ResolveReferenceOnParameterElement ( ( OpenApiParameter ) element , propertyName , mapKey , jsonPointer ) ;
3846 }
3947 if ( element . GetType ( ) == typeof ( OpenApiResponse ) )
4048 {
41- return ResolveReferenceOnResponseElement ( ( OpenApiResponse ) element , jsonPointer ) ;
49+ return ResolveReferenceOnResponseElement ( ( OpenApiResponse ) element , propertyName , mapKey , jsonPointer ) ;
4250 }
4351 }
4452 catch ( KeyNotFoundException )
@@ -48,78 +56,52 @@ public static IOpenApiReferenceable ResolveReference(this IOpenApiReferenceable
4856 throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
4957 }
5058
51- private static IOpenApiReferenceable ResolveReferenceOnHeaderElement ( OpenApiHeader headerElement , string jsonPointer )
59+ private static IOpenApiReferenceable ResolveReferenceOnHeaderElement (
60+ OpenApiHeader headerElement ,
61+ string propertyName ,
62+ string mapKey ,
63+ string jsonPointer )
5264 {
53- if ( string . IsNullOrEmpty ( jsonPointer ) )
54- {
55- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
56- }
57- var jsonPointerTokens = jsonPointer . Split ( '/' ) ;
58- var propertyName = jsonPointerTokens . ElementAtOrDefault ( 1 ) ;
5965 switch ( propertyName )
6066 {
6167 case OpenApiConstants . Schema :
6268 return headerElement . Schema ;
63- case OpenApiConstants . Examples :
64- {
65- if ( jsonPointerTokens . Length < 3 )
66- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
67- var mapKey = jsonPointerTokens . ElementAtOrDefault ( 2 ) ;
68- return headerElement . Examples [ mapKey ] ;
69- }
69+ case OpenApiConstants . Examples when mapKey != null :
70+ return headerElement . Examples [ mapKey ] ;
7071 default :
7172 throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
7273 }
7374 }
7475
75- private static IOpenApiReferenceable ResolveReferenceOnParameterElement ( OpenApiParameter parameterElement , string jsonPointer )
76+ private static IOpenApiReferenceable ResolveReferenceOnParameterElement (
77+ OpenApiParameter parameterElement ,
78+ string propertyName ,
79+ string mapKey ,
80+ string jsonPointer )
7681 {
77- if ( string . IsNullOrEmpty ( jsonPointer ) )
78- {
79- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
80- }
81- var jsonPointerTokens = jsonPointer . Split ( '/' ) ;
82- var propertyName = jsonPointerTokens . ElementAtOrDefault ( 1 ) ;
8382 switch ( propertyName )
8483 {
8584 case OpenApiConstants . Schema :
8685 return parameterElement . Schema ;
87- case OpenApiConstants . Examples :
88- {
89- if ( jsonPointerTokens . Length < 3 )
90- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
91- var mapKey = jsonPointerTokens . ElementAtOrDefault ( 2 ) ;
92- return parameterElement . Examples [ mapKey ] ;
93- }
86+ case OpenApiConstants . Examples when mapKey != null :
87+ return parameterElement . Examples [ mapKey ] ;
9488 default :
9589 throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
9690 }
9791 }
9892
99- private static IOpenApiReferenceable ResolveReferenceOnResponseElement ( OpenApiResponse responseElement , string jsonPointer )
93+ private static IOpenApiReferenceable ResolveReferenceOnResponseElement (
94+ OpenApiResponse responseElement ,
95+ string propertyName ,
96+ string mapKey ,
97+ string jsonPointer )
10098 {
101- if ( string . IsNullOrEmpty ( jsonPointer ) )
102- {
103- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
104- }
105- var jsonPointerTokens = jsonPointer . Split ( '/' ) ;
106- var propertyName = jsonPointerTokens . ElementAtOrDefault ( 1 ) ;
10799 switch ( propertyName )
108100 {
109- case OpenApiConstants . Headers :
110- {
111- if ( jsonPointerTokens . Length < 3 )
112- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
113- var mapKey = jsonPointerTokens . ElementAtOrDefault ( 2 ) ;
114- return responseElement . Headers [ mapKey ] ;
115- }
116- case OpenApiConstants . Links :
117- {
118- if ( jsonPointerTokens . Length < 3 )
119- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
120- var mapKey = jsonPointerTokens . ElementAtOrDefault ( 2 ) ;
121- return responseElement . Links [ mapKey ] ;
122- }
101+ case OpenApiConstants . Headers when mapKey != null :
102+ return responseElement . Headers [ mapKey ] ;
103+ case OpenApiConstants . Links when mapKey != null :
104+ return responseElement . Links [ mapKey ] ;
123105 default :
124106 throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
125107 }
0 commit comments