@@ -16,51 +16,46 @@ namespace Microsoft.OpenApi.Extensions
1616 public static class OpenApiReferencableExtensions
1717 {
1818 /// <summary>
19- /// TODO: tmpDbg comment
19+ /// Resolves a JSON Pointer with respect to an element, returning the referenced element.
2020 /// </summary>
2121 /// <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>
2323 /// <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 )
2525 {
26- if ( jsonPointer == "/" )
26+ if ( ! pointer . Tokens . Any ( ) )
2727 {
2828 return element ;
2929 }
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 ) ;
3732 try
3833 {
3934 if ( element . GetType ( ) == typeof ( OpenApiHeader ) )
4035 {
41- return ResolveReferenceOnHeaderElement ( ( OpenApiHeader ) element , propertyName , mapKey , jsonPointer ) ;
36+ return ResolveReferenceOnHeaderElement ( ( OpenApiHeader ) element , propertyName , mapKey , pointer ) ;
4237 }
4338 if ( element . GetType ( ) == typeof ( OpenApiParameter ) )
4439 {
45- return ResolveReferenceOnParameterElement ( ( OpenApiParameter ) element , propertyName , mapKey , jsonPointer ) ;
40+ return ResolveReferenceOnParameterElement ( ( OpenApiParameter ) element , propertyName , mapKey , pointer ) ;
4641 }
4742 if ( element . GetType ( ) == typeof ( OpenApiResponse ) )
4843 {
49- return ResolveReferenceOnResponseElement ( ( OpenApiResponse ) element , propertyName , mapKey , jsonPointer ) ;
44+ return ResolveReferenceOnResponseElement ( ( OpenApiResponse ) element , propertyName , mapKey , pointer ) ;
5045 }
5146 }
5247 catch ( KeyNotFoundException )
5348 {
54- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
49+ throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , pointer ) ) ;
5550 }
56- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
51+ throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , pointer ) ) ;
5752 }
5853
5954 private static IOpenApiReferenceable ResolveReferenceOnHeaderElement (
6055 OpenApiHeader headerElement ,
6156 string propertyName ,
6257 string mapKey ,
63- string jsonPointer )
58+ JsonPointer pointer )
6459 {
6560 switch ( propertyName )
6661 {
@@ -69,15 +64,15 @@ private static IOpenApiReferenceable ResolveReferenceOnHeaderElement(
6964 case OpenApiConstants . Examples when mapKey != null :
7065 return headerElement . Examples [ mapKey ] ;
7166 default :
72- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
67+ throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , pointer ) ) ;
7368 }
7469 }
7570
7671 private static IOpenApiReferenceable ResolveReferenceOnParameterElement (
7772 OpenApiParameter parameterElement ,
7873 string propertyName ,
7974 string mapKey ,
80- string jsonPointer )
75+ JsonPointer pointer )
8176 {
8277 switch ( propertyName )
8378 {
@@ -86,15 +81,15 @@ private static IOpenApiReferenceable ResolveReferenceOnParameterElement(
8681 case OpenApiConstants . Examples when mapKey != null :
8782 return parameterElement . Examples [ mapKey ] ;
8883 default :
89- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
84+ throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , pointer ) ) ;
9085 }
9186 }
9287
9388 private static IOpenApiReferenceable ResolveReferenceOnResponseElement (
9489 OpenApiResponse responseElement ,
9590 string propertyName ,
9691 string mapKey ,
97- string jsonPointer )
92+ JsonPointer pointer )
9893 {
9994 switch ( propertyName )
10095 {
@@ -103,7 +98,7 @@ private static IOpenApiReferenceable ResolveReferenceOnResponseElement(
10398 case OpenApiConstants . Links when mapKey != null :
10499 return responseElement . Links [ mapKey ] ;
105100 default :
106- throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , jsonPointer ) ) ;
101+ throw new OpenApiException ( string . Format ( SRResource . InvalidReferenceId , pointer ) ) ;
107102 }
108103 }
109104 }
0 commit comments