From ee02d1f40c47cd8a8790287d3ccbef881a6a85f1 Mon Sep 17 00:00:00 2001 From: Yevgenii Mikhnytskyi Date: Fri, 16 Jul 2021 17:10:27 +0300 Subject: [PATCH 1/2] 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 e21e83c80..0954d7365 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 e21e83c80..0954d7365 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 ef641b449..4195e55bc 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 3fa53d22f..3fde117f7 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 { From 2fb2324b04f239d3f9718bf9f84afcc48ec91219 Mon Sep 17 00:00:00 2001 From: Joris Goovaerts Date: Thu, 9 Sep 2021 10:25:37 +0200 Subject: [PATCH 2/2] Cleanup unnecessary changes --- src/openApi/v2/parser/getOperationParameterName.ts | 3 +-- src/openApi/v3/parser/getOperationParameterName.ts | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/openApi/v2/parser/getOperationParameterName.ts b/src/openApi/v2/parser/getOperationParameterName.ts index 0954d7365..e21e83c80 100644 --- a/src/openApi/v2/parser/getOperationParameterName.ts +++ b/src/openApi/v2/parser/getOperationParameterName.ts @@ -1,7 +1,6 @@ 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 0954d7365..e21e83c80 100644 --- a/src/openApi/v3/parser/getOperationParameterName.ts +++ b/src/openApi/v3/parser/getOperationParameterName.ts @@ -1,7 +1,6 @@ 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.