From 0cffb60812211989c172dc8c58b43e4589c86868 Mon Sep 17 00:00:00 2001 From: Yevgenii Mikhnytskyi Date: Fri, 16 Jul 2021 17:10:27 +0300 Subject: [PATCH] Fix getRef on encoded path, issue: https://github.com/ferdikoomen/openapi-typescript-codegen/issues/791 --- .../v2/parser/getOperationParameterName.ts | 3 ++- .../v3/parser/getOperationParameterName.ts | 3 ++- src/openApi/v3/parser/getRef.spec.ts | 24 +++++++++++++++++++ src/openApi/v3/parser/getRef.ts | 4 ++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/openApi/v2/parser/getOperationParameterName.ts b/src/openApi/v2/parser/getOperationParameterName.ts index e21e83c8..0954d736 100644 --- a/src/openApi/v2/parser/getOperationParameterName.ts +++ b/src/openApi/v2/parser/getOperationParameterName.ts @@ -1,6 +1,7 @@ import camelCase from 'camelcase'; -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; +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; /** * Replaces any invalid characters from a parameter name. diff --git a/src/openApi/v3/parser/getOperationParameterName.ts b/src/openApi/v3/parser/getOperationParameterName.ts index e21e83c8..0954d736 100644 --- a/src/openApi/v3/parser/getOperationParameterName.ts +++ b/src/openApi/v3/parser/getOperationParameterName.ts @@ -1,6 +1,7 @@ import camelCase from 'camelcase'; -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; +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; /** * Replaces any invalid characters from a parameter name. diff --git a/src/openApi/v3/parser/getRef.spec.ts b/src/openApi/v3/parser/getRef.spec.ts index ef641b44..4195e55b 100644 --- a/src/openApi/v3/parser/getRef.spec.ts +++ b/src/openApi/v3/parser/getRef.spec.ts @@ -34,4 +34,28 @@ describe('getRef', () => { type: 'integer', }); }); + + it('should produce correct result for encoded ref path', () => { + expect( + getRef( + { + openapi: '3.0', + info: { + title: 'dummy', + version: '1.0', + }, + paths: { + '/api/user/{id}': { + description: 'This is an Example path', + }, + }, + }, + { + $ref: '#/paths/~1api~1user~1%7Bid%7D', + } + ) + ).toEqual({ + description: 'This is an Example path', + }); + }); }); diff --git a/src/openApi/v3/parser/getRef.ts b/src/openApi/v3/parser/getRef.ts index 3fa53d22..3fde117f 100644 --- a/src/openApi/v3/parser/getRef.ts +++ b/src/openApi/v3/parser/getRef.ts @@ -1,6 +1,9 @@ import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiReference } from '../interfaces/OpenApiReference'; +const escapedSlash = /~1/g; +const escapedTilde = /~0/g; + export function getRef(openApi: OpenApi, item: T & OpenApiReference): T { if (item.$ref) { // Fetch the paths to the definitions, this converts: @@ -14,6 +17,7 @@ export function getRef(openApi: OpenApi, item: T & OpenApiReference): T { // 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]; } else {