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 { diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index d71a821b..59571869 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -165,11 +165,20 @@ async function getHeaders(options: ApiRequestOptions): Promise { const password = await resolve(options, OpenAPI.PASSWORD); const defaultHeaders = await resolve(options, OpenAPI.HEADERS); - const headers = new Headers({ + const filteredHeaders = Object.entries({ Accept: 'application/json', ...defaultHeaders, ...options.headers, - }); + }).reduce((acc, [headerKey, headerValue]) => { + if (typeof headerValue !== 'undefined') { + return { + ...acc, + [headerKey]: headerValue, + }; + } + return acc; + }, {}); + const headers = new Headers(filteredHeaders); if (isStringWithValue(token)) { headers.append('Authorization', \`Bearer \${token}\`); @@ -2520,11 +2529,20 @@ async function getHeaders(options: ApiRequestOptions): Promise { const password = await resolve(options, OpenAPI.PASSWORD); const defaultHeaders = await resolve(options, OpenAPI.HEADERS); - const headers = new Headers({ + const filteredHeaders = Object.entries({ Accept: 'application/json', ...defaultHeaders, ...options.headers, - }); + }).reduce((acc, [headerKey, headerValue]) => { + if (typeof headerValue !== 'undefined') { + return { + ...acc, + [headerKey]: headerValue, + }; + } + return acc; + }, {}); + const headers = new Headers(filteredHeaders); if (isStringWithValue(token)) { headers.append('Authorization', \`Bearer \${token}\`);