Skip to content

Commit 4f056c6

Browse files
authored
Merge pull request ferdikoomen#793 from todesstoss/fix-get-ref-on-encoded-ref-path
Fix getRef on encoded path, issue: https://github.com/ferdikoomen/op…
2 parents bbaf970 + 0cffb60 commit 4f056c6

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/openApi/v3/parser/getRef.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,28 @@ describe('getRef', () => {
3434
type: 'integer',
3535
});
3636
});
37+
38+
it('should produce correct result for encoded ref path', () => {
39+
expect(
40+
getRef(
41+
{
42+
openapi: '3.0',
43+
info: {
44+
title: 'dummy',
45+
version: '1.0',
46+
},
47+
paths: {
48+
'/api/user/{id}': {
49+
description: 'This is an Example path',
50+
},
51+
},
52+
},
53+
{
54+
$ref: '#/paths/~1api~1user~1%7Bid%7D',
55+
}
56+
)
57+
).toEqual({
58+
description: 'This is an Example path',
59+
});
60+
});
3761
});

src/openApi/v3/parser/getRef.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { OpenApi } from '../interfaces/OpenApi';
22
import type { OpenApiReference } from '../interfaces/OpenApiReference';
33

4+
const escapedSlash = /~1/g;
5+
const escapedTilde = /~0/g;
6+
47
export function getRef<T>(openApi: OpenApi, item: T & OpenApiReference): T {
58
if (item.$ref) {
69
// Fetch the paths to the definitions, this converts:
@@ -14,6 +17,7 @@ export function getRef<T>(openApi: OpenApi, item: T & OpenApiReference): T {
1417
// if we cannot find it, then we throw an error.
1518
let result: any = openApi;
1619
paths.forEach((path: string): void => {
20+
path = decodeURIComponent(path.replace(escapedSlash, '/').replace(escapedTilde, '~'));
1721
if (result.hasOwnProperty(path)) {
1822
result = result[path];
1923
} else {

0 commit comments

Comments
 (0)