This commit is contained in:
Yevgenii Mikhnytskyi 2021-07-16 17:10:27 +03:00
parent f212119432
commit 0cffb60812
4 changed files with 32 additions and 2 deletions

View File

@ -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.

View File

@ -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.

View File

@ -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',
});
});
});

View File

@ -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<T>(openApi: OpenApi, item: T & OpenApiReference): T {
if (item.$ref) {
// Fetch the paths to the definitions, this converts:
@ -14,6 +17,7 @@ export function getRef<T>(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 {