mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
- Also add path escaping
This commit is contained in:
parent
f77b0b7558
commit
17d3c74afa
@ -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<T>(openApi: OpenApi, item: T & OpenApiReference): T {
|
||||
if (item.$ref) {
|
||||
// Fetch the paths to the definitions, this converts:
|
||||
@ -13,9 +16,10 @@ export function getRef<T>(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}"`);
|
||||
}
|
||||
|
||||
@ -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<T>(openApi: OpenApi, item: T & OpenApiReference): T {
|
||||
if (item.$ref) {
|
||||
@ -16,10 +16,10 @@ export function getRef<T>(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}"`);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user