@@ -16,51 +16,46 @@ namespace Microsoft.OpenApi.Extensions
16
16
public static class OpenApiReferencableExtensions
17
17
{
18
18
/// <summary>
19
- /// TODO: tmpDbg comment
19
+ /// Resolves a JSON Pointer with respect to an element, returning the referenced element.
20
20
/// </summary>
21
21
/// <param name="element">The referencable Open API element on which to apply the JSON pointer</param>
22
- /// <param name="jsonPointer ">a JSON Pointer [RFC 6901](https://tools.ietf.org/html/rfc6901).</param>
22
+ /// <param name="pointer ">a JSON Pointer [RFC 6901](https://tools.ietf.org/html/rfc6901).</param>
23
23
/// <returns>The element pointed to by the JSON pointer.</returns>
24
- public static IOpenApiReferenceable ResolveReference ( this IOpenApiReferenceable element , string jsonPointer )
24
+ public static IOpenApiReferenceable ResolveReference ( this IOpenApiReferenceable element , JsonPointer pointer )
25
25
{
26
- if ( jsonPointer == "/" )
26
+ if ( ! pointer . Tokens . Any ( ) )
27
27
{
28
28
return element ;
29
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 ) ;
30
+ var propertyName = pointer . Tokens . FirstOrDefault ( ) ;
31
+ var mapKey = pointer . Tokens . ElementAtOrDefault ( 1 ) ;
37
32
try
38
33
{
39
34
if ( element . GetType ( ) == typeof ( OpenApiHeader ) )
40
35
{
41
- return ResolveReferenceOnHeaderElement ( ( OpenApiHeader ) element , propertyName , mapKey , jsonPointer ) ;
36
+ return ResolveReferenceOnHeaderElement ( ( OpenApiHeader ) element , propertyName , mapKey , pointer ) ;
42
37
}
43
38
if ( element . GetType ( ) == typeof ( OpenApiParameter ) )
44
39
{
45
- return ResolveReferenceOnParameterElement ( ( OpenApiParameter ) element , propertyName , mapKey , jsonPointer ) ;
40
+ return ResolveReferenceOnParameterElement ( ( OpenApiParameter ) element , propertyName , mapKey , pointer ) ;
46
41
}
47
42
if ( element . GetType ( ) == typeof ( OpenApiResponse ) )
48
43
{
49
- return ResolveReferenceOnResponseElement ( ( OpenApiResponse ) element , propertyName , mapKey , jsonPointer ) ;
44
+ return ResolveReferenceOnResponseElement ( ( OpenApiResponse ) element , propertyName , mapKey , pointer ) ;
50
45
}
51
46
}
52
47
catch ( KeyNotFoundException )
53
48
{
54
- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
49
+ throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , pointer ) ) ;
55
50
}
56
- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
51
+ throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , pointer ) ) ;
57
52
}
58
53
59
54
private static IOpenApiReferenceable ResolveReferenceOnHeaderElement (
60
55
OpenApiHeader headerElement ,
61
56
string propertyName ,
62
57
string mapKey ,
63
- string jsonPointer )
58
+ JsonPointer pointer )
64
59
{
65
60
switch ( propertyName )
66
61
{
@@ -69,15 +64,15 @@ private static IOpenApiReferenceable ResolveReferenceOnHeaderElement(
69
64
case OpenApiConstants . Examples when mapKey != null :
70
65
return headerElement . Examples [ mapKey ] ;
71
66
default :
72
- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
67
+ throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , pointer ) ) ;
73
68
}
74
69
}
75
70
76
71
private static IOpenApiReferenceable ResolveReferenceOnParameterElement (
77
72
OpenApiParameter parameterElement ,
78
73
string propertyName ,
79
74
string mapKey ,
80
- string jsonPointer )
75
+ JsonPointer pointer )
81
76
{
82
77
switch ( propertyName )
83
78
{
@@ -86,15 +81,15 @@ private static IOpenApiReferenceable ResolveReferenceOnParameterElement(
86
81
case OpenApiConstants . Examples when mapKey != null :
87
82
return parameterElement . Examples [ mapKey ] ;
88
83
default :
89
- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
84
+ throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , pointer ) ) ;
90
85
}
91
86
}
92
87
93
88
private static IOpenApiReferenceable ResolveReferenceOnResponseElement (
94
89
OpenApiResponse responseElement ,
95
90
string propertyName ,
96
91
string mapKey ,
97
- string jsonPointer )
92
+ JsonPointer pointer )
98
93
{
99
94
switch ( propertyName )
100
95
{
@@ -103,7 +98,7 @@ private static IOpenApiReferenceable ResolveReferenceOnResponseElement(
103
98
case OpenApiConstants . Links when mapKey != null :
104
99
return responseElement . Links [ mapKey ] ;
105
100
default :
106
- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
101
+ throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , pointer ) ) ;
107
102
}
108
103
}
109
104
}
0 commit comments