From ad500b58de1c26637728411d4620d725deb174f0 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Wed, 21 Dec 2022 14:05:26 +0900 Subject: [PATCH] Allow path parameter as enum strings --- src/buildV3.ts | 1 + src/builderUtils/getDirName.ts | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/buildV3.ts b/src/buildV3.ts index dd8cd824..852c57c5 100644 --- a/src/buildV3.ts +++ b/src/buildV3.ts @@ -60,6 +60,7 @@ export default (openapi: OpenAPIV3.Document) => { [] as OpenAPIV3.ParameterObject[] ) ], + openapi, false ) ), diff --git a/src/builderUtils/getDirName.ts b/src/builderUtils/getDirName.ts index 30ec741a..c56e287a 100644 --- a/src/builderUtils/getDirName.ts +++ b/src/builderUtils/getDirName.ts @@ -1,17 +1,24 @@ import { OpenAPIV3 } from 'openapi-types' -import { getPropertyName, schema2value } from './converters' +import { getPropertyName, isRefObject, schema2value } from './converters' +import { resolveSchemasRef } from './resolvers' -export default (text: string, params: OpenAPIV3.ParameterObject[], required: boolean) => { +export default (text: string, params: OpenAPIV3.ParameterObject[], openapi: OpenAPIV3.Document, required: boolean) => { if (text === '*') return '_any' if (!/^{/.test(text)) { return text } const valName = text.slice(1, -1) - const schemaVal = schema2value( - params.find(p => p.in === 'path' && p.name === valName)?.schema, - required - ) + + let schema = params.find(p => p.in === 'path' && p.name === valName)?.schema + if (schema && isRefObject(schema)) { + const referencedSchema = resolveSchemasRef(openapi, schema.$ref) + if (referencedSchema.type === 'string' || referencedSchema.type === 'number') { + schema = referencedSchema + } + } + + const schemaVal = schema2value(schema, required) return `_${getPropertyName(valName)}${ schemaVal && typeof schemaVal.value === 'string' ? `@${schemaVal.value}` : ''