1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
4
- using System ;
5
4
using System . Collections . Generic ;
6
5
using System . Linq ;
7
6
using Microsoft . OpenApi . Exceptions ;
@@ -25,20 +24,29 @@ public static class OpenApiReferencableExtensions
25
24
public static IOpenApiReferenceable ResolveReference ( this IOpenApiReferenceable element , string jsonPointer )
26
25
{
27
26
if ( jsonPointer == "/" )
27
+ {
28
28
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 ) ;
29
37
try
30
38
{
31
39
if ( element . GetType ( ) == typeof ( OpenApiHeader ) )
32
40
{
33
- return ResolveReferenceOnHeaderElement ( ( OpenApiHeader ) element , jsonPointer ) ;
41
+ return ResolveReferenceOnHeaderElement ( ( OpenApiHeader ) element , propertyName , mapKey , jsonPointer ) ;
34
42
}
35
43
if ( element . GetType ( ) == typeof ( OpenApiParameter ) )
36
44
{
37
- return ResolveReferenceOnParameterElement ( ( OpenApiParameter ) element , jsonPointer ) ;
45
+ return ResolveReferenceOnParameterElement ( ( OpenApiParameter ) element , propertyName , mapKey , jsonPointer ) ;
38
46
}
39
47
if ( element . GetType ( ) == typeof ( OpenApiResponse ) )
40
48
{
41
- return ResolveReferenceOnResponseElement ( ( OpenApiResponse ) element , jsonPointer ) ;
49
+ return ResolveReferenceOnResponseElement ( ( OpenApiResponse ) element , propertyName , mapKey , jsonPointer ) ;
42
50
}
43
51
}
44
52
catch ( KeyNotFoundException )
@@ -48,78 +56,52 @@ public static IOpenApiReferenceable ResolveReference(this IOpenApiReferenceable
48
56
throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
49
57
}
50
58
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 )
52
64
{
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 ) ;
59
65
switch ( propertyName )
60
66
{
61
67
case OpenApiConstants . Schema :
62
68
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 ] ;
70
71
default :
71
72
throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
72
73
}
73
74
}
74
75
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 )
76
81
{
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 ) ;
83
82
switch ( propertyName )
84
83
{
85
84
case OpenApiConstants . Schema :
86
85
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 ] ;
94
88
default :
95
89
throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
96
90
}
97
91
}
98
92
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 )
100
98
{
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 ) ;
107
99
switch ( propertyName )
108
100
{
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 ] ;
123
105
default :
124
106
throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
125
107
}
0 commit comments