Skip to content

Commit 17d3c74

Browse files
committed
- Also add path escaping
1 parent f77b0b7 commit 17d3c74

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/openApi/v2/parser/getRef.ts

Lines changed: 7 additions & 3 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 ESCAPED_REF_SLASH = /~1/g;
5+
const ESCAPED_REF_TILDE = /~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:
@@ -13,9 +16,10 @@ export function getRef<T>(openApi: OpenApi, item: T & OpenApiReference): T {
1316
// Try to find the reference by walking down the path,
1417
// if we cannot find it, then we throw an error.
1518
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];
1923
} else {
2024
throw new Error(`Could not find reference: "${item.$ref}"`);
2125
}

src/openApi/v3/parser/getRef.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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;
4+
const ESCAPED_REF_SLASH = /~1/g;
5+
const ESCAPED_REF_TILDE = /~0/g;
66

77
export function getRef<T>(openApi: OpenApi, item: T & OpenApiReference): T {
88
if (item.$ref) {
@@ -16,10 +16,10 @@ export function getRef<T>(openApi: OpenApi, item: T & OpenApiReference): T {
1616
// Try to find the reference by walking down the path,
1717
// if we cannot find it, then we throw an error.
1818
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];
2323
} else {
2424
throw new Error(`Could not find reference: "${item.$ref}"`);
2525
}

0 commit comments

Comments
 (0)