4
4
using System ;
5
5
using System . Collections . Generic ;
6
6
using System . Linq ;
7
+ using Microsoft . OpenApi . Exceptions ;
7
8
using Microsoft . OpenApi . Interfaces ;
8
9
using Microsoft . OpenApi . Models ;
10
+ using Microsoft . OpenApi . Properties ;
9
11
using Microsoft . OpenApi . Services ;
10
12
using Microsoft . OpenApi . Validations ;
11
13
@@ -26,27 +28,37 @@ public static IOpenApiReferenceable ResolveReference(this IOpenApiReferenceable
26
28
{
27
29
if ( jsonPointer == string . Empty )
28
30
return element ;
29
- if ( element . GetType ( ) == typeof ( OpenApiParameter ) )
31
+ try
30
32
{
31
- return ResolveReferenceOnParameterElement ( ( OpenApiParameter ) element , jsonPointer ) ;
33
+ if ( element . GetType ( ) == typeof ( OpenApiParameter ) )
34
+ {
35
+ return ResolveReferenceOnParameterElement ( ( OpenApiParameter ) element , jsonPointer ) ;
36
+ }
37
+ }
38
+ catch ( KeyNotFoundException )
39
+ {
40
+ throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
32
41
}
33
42
throw new NotImplementedException ( ) ;
34
43
}
35
44
36
45
private static IOpenApiReferenceable ResolveReferenceOnParameterElement ( OpenApiParameter parameterElement , string jsonPointer )
37
46
{
38
47
var jsonPointerTokens = jsonPointer . Split ( '/' ) ;
39
- switch ( jsonPointerTokens . First ( ) )
48
+ var propertyName = jsonPointerTokens . First ( ) ;
49
+ switch ( propertyName )
40
50
{
41
51
case OpenApiConstants . Schema :
42
52
return parameterElement . Schema ;
43
53
case OpenApiConstants . Examples :
44
54
{
45
- var mapKey = jsonPointerTokens . ElementAt ( 1 ) ;
55
+ var mapKey = jsonPointerTokens . ElementAtOrDefault ( 1 ) ;
56
+ if ( ! ( jsonPointerTokens . Length >= 2 && parameterElement . Examples . ContainsKey ( mapKey ) ) )
57
+ throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
46
58
return parameterElement . Examples [ mapKey ] ;
47
59
}
48
60
default :
49
- throw new NotImplementedException ( ) ;
61
+ throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
50
62
}
51
63
}
52
64
}
0 commit comments