Skip to content

Commit 0cffb60

Browse files
committed
Fix getRef on encoded path, issue: ferdikoomen#791
1 parent f212119 commit 0cffb60

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

src/openApi/v2/parser/getOperationParameterName.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import camelCase from 'camelcase';
22

3-
const reservedWords = /^(arguments|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|eval|export|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)$/g;
3+
const reservedWords =
4+
/^(arguments|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|eval|export|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)$/g;
45

56
/**
67
* Replaces any invalid characters from a parameter name.

src/openApi/v3/parser/getOperationParameterName.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import camelCase from 'camelcase';
22

3-
const reservedWords = /^(arguments|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|eval|export|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)$/g;
3+
const reservedWords =
4+
/^(arguments|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|eval|export|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|let|new|null|package|private|protected|public|return|static|super|switch|this|throw|true|try|typeof|var|void|while|with|yield)$/g;
45

56
/**
67
* Replaces any invalid characters from a parameter name.

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)