From 17d3c74afa4008b470d93c179cede1963e605007 Mon Sep 17 00:00:00 2001 From: Ferdi Koomen Date: Mon, 27 Sep 2021 18:53:58 +0200 Subject: [PATCH] - Also add path escaping --- src/openApi/v2/parser/getRef.ts | 10 +++++++--- src/openApi/v3/parser/getRef.ts | 12 ++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/openApi/v2/parser/getRef.ts b/src/openApi/v2/parser/getRef.ts index cb9fe24f..c2390a50 100644 --- a/src/openApi/v2/parser/getRef.ts +++ b/src/openApi/v2/parser/getRef.ts @@ -1,6 +1,9 @@ import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiReference } from '../interfaces/OpenApiReference'; +const ESCAPED_REF_SLASH = /~1/g; +const ESCAPED_REF_TILDE = /~0/g; + export function getRef(openApi: OpenApi, item: T & OpenApiReference): T { if (item.$ref) { // Fetch the paths to the definitions, this converts: @@ -13,9 +16,10 @@ export function getRef(openApi: OpenApi, item: T & OpenApiReference): T { // Try to find the reference by walking down the path, // if we cannot find it, then we throw an error. let result: any = openApi; - paths.forEach((path: string): void => { - if (result.hasOwnProperty(path)) { - result = result[path]; + paths.forEach(path => { + const decodedPath = decodeURIComponent(path.replace(ESCAPED_REF_SLASH, '/').replace(ESCAPED_REF_TILDE, '~')); + if (result.hasOwnProperty(decodedPath)) { + result = result[decodedPath]; } else { throw new Error(`Could not find reference: "${item.$ref}"`); } diff --git a/src/openApi/v3/parser/getRef.ts b/src/openApi/v3/parser/getRef.ts index 3fde117f..9888e185 100644 --- a/src/openApi/v3/parser/getRef.ts +++ b/src/openApi/v3/parser/getRef.ts @@ -1,8 +1,8 @@ import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiReference } from '../interfaces/OpenApiReference'; -const escapedSlash = /~1/g; -const escapedTilde = /~0/g; +const ESCAPED_REF_SLASH = /~1/g; +const ESCAPED_REF_TILDE = /~0/g; export function getRef(openApi: OpenApi, item: T & OpenApiReference): T { if (item.$ref) { @@ -16,10 +16,10 @@ export function getRef(openApi: OpenApi, item: T & OpenApiReference): T { // Try to find the reference by walking down the path, // if we cannot find it, then we throw an error. let result: any = openApi; - paths.forEach((path: string): void => { - path = decodeURIComponent(path.replace(escapedSlash, '/').replace(escapedTilde, '~')); - if (result.hasOwnProperty(path)) { - result = result[path]; + paths.forEach(path => { + const decodedPath = decodeURIComponent(path.replace(ESCAPED_REF_SLASH, '/').replace(ESCAPED_REF_TILDE, '~')); + if (result.hasOwnProperty(decodedPath)) { + result = result[decodedPath]; } else { throw new Error(`Could not find reference: "${item.$ref}"`); }