Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enumName is ignored in @Api[Param|Query|Header] #2734

Merged
merged 3 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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