diff --git a/e2e/api-spec.json b/e2e/api-spec.json index 99f19ab4a..2f2ef347a 100644 --- a/e2e/api-spec.json +++ b/e2e/api-spec.json @@ -837,12 +837,64 @@ "required": true, "in": "path", "schema": { - "type": "string", "enum": [ "A", "B", "C" - ] + ], + "type": "string" + } + }, + { + "name": "x-tenant-id", + "in": "header", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "" + } + }, + "tags": [ + "cats" + ], + "security": [ + { + "key2": [], + "key1": [] + }, + { + "bearer": [] + }, + { + "basic": [] + } + ] + } + }, + "/api/cats/with-enum-named/{type}": { + "get": { + "operationId": "CatsController_getWithEnumNamedParam", + "parameters": [ + { + "name": "header", + "in": "header", + "description": "Test", + "required": false, + "schema": { + "default": "test", + "type": "string" + } + }, + { + "name": "type", + "required": true, + "in": "path", + "schema": { + "$ref": "#/components/schemas/Letter" } }, { diff --git a/e2e/src/cats/cats.controller.ts b/e2e/src/cats/cats.controller.ts index 89300f136..6b2a79f76 100644 --- a/e2e/src/cats/cats.controller.ts +++ b/e2e/src/cats/cats.controller.ts @@ -96,10 +96,17 @@ export class CatsController { @ApiParam({ name: 'type', enum: LettersEnum, - enumName: 'Letter' }) getWithEnumParam(@Param('type') type: LettersEnum) {} + @Get('with-enum-named/:type') + @ApiParam({ + name: 'type', + enum: LettersEnum, + enumName: 'Letter' + }) + getWithEnumNamedParam(@Param('type') type: LettersEnum) {} + @Get('with-random-query') @ApiQuery({ name: 'type', diff --git a/lib/decorators/api-param.decorator.ts b/lib/decorators/api-param.decorator.ts index 4e1f48b4b..dc1706312 100644 --- a/lib/decorators/api-param.decorator.ts +++ b/lib/decorators/api-param.decorator.ts @@ -5,7 +5,7 @@ import { SchemaObject } from '../interfaces/open-api-spec.interface'; import { SwaggerEnumType } from '../types/swagger-enum.type'; -import { getEnumType, getEnumValues } from '../utils/enum.utils'; +import { addEnumSchema, getEnumType, getEnumValues, isEnumDefined } from '../utils/enum.utils'; import { createParamDecorator } from './helpers'; type ParameterOptions = Omit; @@ -29,24 +29,14 @@ const defaultParamOptions: ApiParamOptions = { }; export function ApiParam(options: ApiParamOptions): MethodDecorator { - const param: Record = { + const param: ApiParamMetadata & Record = { name: isNil(options.name) ? defaultParamOptions.name : options.name, in: 'path', ...omit(options, 'enum') }; - const apiParamMetadata = options as ApiParamMetadata; - if (apiParamMetadata.enum) { - param.schema = param.schema || ({} as SchemaObject); - - const paramSchema = param.schema as SchemaObject; - const enumValues = getEnumValues(apiParamMetadata.enum); - paramSchema.type = getEnumType(enumValues); - paramSchema.enum = enumValues; - - if (apiParamMetadata.enumName) { - param.enumName = apiParamMetadata.enumName; - } + if (isEnumDefined(options)) { + addEnumSchema(param, options); } return createParamDecorator(param, defaultParamOptions); diff --git a/lib/services/schema-object-factory.ts b/lib/services/schema-object-factory.ts index bf42e9d7a..9573248f6 100644 --- a/lib/services/schema-object-factory.ts +++ b/lib/services/schema-object-factory.ts @@ -51,6 +51,9 @@ export class SchemaObjectFactory { undefined ) as [Type, boolean]; } + if (!isBodyParameter(param) && param.enumName) { + return this.createEnumParam(param, schemas); + } if (this.isPrimitiveType(param.type)) { return param; } @@ -89,14 +92,10 @@ export class SchemaObjectFactory { return flatten(parameterObjects); } - createQueryOrParamSchema( + private createQueryOrParamSchema( param: ParamWithTypeMetadata, schemas: Record ) { - if (param.enumName) { - return this.createEnumParam(param, schemas); - } - if (isDateCtor(param.type as Function)) { return { format: 'date-time',