File tree Expand file tree Collapse file tree 2 files changed +13
-9
lines changed Expand file tree Collapse file tree 2 files changed +13
-9
lines changed Original file line number Diff line number Diff line change 1
1
import type { OpenApi } from '../interfaces/OpenApi' ;
2
2
import type { OpenApiReference } from '../interfaces/OpenApiReference' ;
3
3
4
+ const ESCAPED_REF_SLASH = / ~ 1 / g;
5
+ const ESCAPED_REF_TILDE = / ~ 0 / g;
6
+
4
7
export function getRef < T > ( openApi : OpenApi , item : T & OpenApiReference ) : T {
5
8
if ( item . $ref ) {
6
9
// Fetch the paths to the definitions, this converts:
@@ -13,9 +16,10 @@ export function getRef<T>(openApi: OpenApi, item: T & OpenApiReference): T {
13
16
// Try to find the reference by walking down the path,
14
17
// if we cannot find it, then we throw an error.
15
18
let result : any = openApi ;
16
- paths . forEach ( ( path : string ) : void => {
17
- if ( result . hasOwnProperty ( path ) ) {
18
- result = result [ path ] ;
19
+ paths . forEach ( path => {
20
+ const decodedPath = decodeURIComponent ( path . replace ( ESCAPED_REF_SLASH , '/' ) . replace ( ESCAPED_REF_TILDE , '~' ) ) ;
21
+ if ( result . hasOwnProperty ( decodedPath ) ) {
22
+ result = result [ decodedPath ] ;
19
23
} else {
20
24
throw new Error ( `Could not find reference: "${ item . $ref } "` ) ;
21
25
}
Original file line number Diff line number Diff line change 1
1
import type { OpenApi } from '../interfaces/OpenApi' ;
2
2
import type { OpenApiReference } from '../interfaces/OpenApiReference' ;
3
3
4
- const escapedSlash = / ~ 1 / g;
5
- const escapedTilde = / ~ 0 / g;
4
+ const ESCAPED_REF_SLASH = / ~ 1 / g;
5
+ const ESCAPED_REF_TILDE = / ~ 0 / g;
6
6
7
7
export function getRef < T > ( openApi : OpenApi , item : T & OpenApiReference ) : T {
8
8
if ( item . $ref ) {
@@ -16,10 +16,10 @@ export function getRef<T>(openApi: OpenApi, item: T & OpenApiReference): T {
16
16
// Try to find the reference by walking down the path,
17
17
// if we cannot find it, then we throw an error.
18
18
let result : any = openApi ;
19
- paths . forEach ( ( path : string ) : void => {
20
- path = decodeURIComponent ( path . replace ( escapedSlash , '/' ) . replace ( escapedTilde , '~' ) ) ;
21
- if ( result . hasOwnProperty ( path ) ) {
22
- result = result [ path ] ;
19
+ paths . forEach ( path => {
20
+ const decodedPath = decodeURIComponent ( path . replace ( ESCAPED_REF_SLASH , '/' ) . replace ( ESCAPED_REF_TILDE , '~' ) ) ;
21
+ if ( result . hasOwnProperty ( decodedPath ) ) {
22
+ result = result [ decodedPath ] ;
23
23
} else {
24
24
throw new Error ( `Could not find reference: "${ item . $ref } "` ) ;
25
25
}
You can’t perform that action at this time.
0 commit comments