Skip to content

Commit

Permalink
Merge pull request #2734 from lucas-gregoire/fix/param-enums
Browse files Browse the repository at this point in the history
fix: `enumName` is ignored in @Api[Param|Query|Header]
  • Loading branch information
kamilmysliwiec authored Jan 15, 2024
2 parents 91d2035 + 3a869a3 commit b7f3f5b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 22 deletions.
56 changes: 54 additions & 2 deletions e2e/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand Down
9 changes: 8 additions & 1 deletion e2e/src/cats/cats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
18 changes: 4 additions & 14 deletions lib/decorators/api-param.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParameterObject, 'in' | 'schema'>;
Expand All @@ -29,24 +29,14 @@ const defaultParamOptions: ApiParamOptions = {
};

export function ApiParam(options: ApiParamOptions): MethodDecorator {
const param: Record<string, any> = {
const param: ApiParamMetadata & Record<string, any> = {
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);
Expand Down
9 changes: 4 additions & 5 deletions lib/services/schema-object-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export class SchemaObjectFactory {
undefined
) as [Type<any>, boolean];
}
if (!isBodyParameter(param) && param.enumName) {
return this.createEnumParam(param, schemas);
}
if (this.isPrimitiveType(param.type)) {
return param;
}
Expand Down Expand Up @@ -89,14 +92,10 @@ export class SchemaObjectFactory {
return flatten(parameterObjects);
}

createQueryOrParamSchema(
private createQueryOrParamSchema(
param: ParamWithTypeMetadata,
schemas: Record<string, SchemaObject>
) {
if (param.enumName) {
return this.createEnumParam(param, schemas);
}

if (isDateCtor(param.type as Function)) {
return {
format: 'date-time',
Expand Down

0 comments on commit b7f3f5b

Please sign in to comment.