Is there an existing issue for this?
Current behavior
Our team has been experiencing this issue since we upgraded from @nestjs/swagger@4.8.2, it is a weird one and took time to find. It appears that there is an issue with with multiple specifications. It fails to create the enum for specifications for the following specifications. It will show the enum only in the first specification when the enum is define with both ApiQuery and ApiProperty.
Minimum reproduction code
https://github.com/DJCrossman/nest-swagger-multi-spec-enums-bug
Steps to reproduce
-
nest new project-name
-
cd project-name
-
npm install @nestjs/swagger
-
nest g module cats
-
nest g class cats/cat --flat
-
Edit src/cats/cat.ts, to have CollarColorEnum and ApiProperty:
import { ApiProperty } from '@nestjs/swagger';
export enum CollarColorEnum {
'red' = 'red',
'green' = 'green',
'blue' = 'blue',
}
export type CollarColor = keyof typeof CollarColorEnum
export class Cat {
@ApiProperty({ enum: CollarColorEnum, enumName: 'CollarColor' })
collar_color: CollarColor;
}
-
nest g controller cats
-
Edit src\cat\cat.controller.ts, to have find endpoint:
import { Controller, Get } from '@nestjs/common';
import { ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Cat, CollarColorEnum } from './cat';
@ApiTags('cats')
@Controller('cat')
export class CatsController {
@Get()
@ApiQuery({ enum: CollarColorEnum, enumName: 'CollarColor' })
@ApiResponse({ status: 200, type: [Cat] })
find() {
return [];
}
}
-
Initialize Swagger with multiple specifications:
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module';
import { CatsModule } from './cats/cats.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const options = new DocumentBuilder()
.setTitle('Cats example')
.setDescription('The cats API description')
.setVersion('1.0')
.addTag('cats')
.build();
const catDocument = SwaggerModule.createDocument(app, options, {
include: [CatsModule],
});
SwaggerModule.setup('api/cats', app, catDocument);
const secondOptions = new DocumentBuilder()
.setTitle('Broken example')
.setDescription('The cats API with missing enum')
.setVersion('1.0')
.addTag('broken')
.build();
const document = SwaggerModule.createDocument(app, secondOptions, {
include: [CatsModule],
});
SwaggerModule.setup('api', app, document);
await app.listen(3000);
}
bootstrap();
-
npm run start:dev
-
Go to http://localhost:3000/api-json
Expected behavior
The enum schema CollarColor should be defined with enum values and not only appear as type string in the specification. It should be expected that both the http://localhost:3000/api-json specification and http://localhost:3000/api/cats-json should be identical.
Package version
6.1.3
NestJS version
9.1.5
Node.js version
v18.12.1
In which operating systems have you tested?
Other
No response
Is there an existing issue for this?
Current behavior
Our team has been experiencing this issue since we upgraded from
@nestjs/swagger@4.8.2, it is a weird one and took time to find. It appears that there is an issue with with multiple specifications. It fails to create the enum for specifications for the following specifications. It will show the enum only in the first specification when the enum is define with bothApiQueryandApiProperty.Minimum reproduction code
https://github.com/DJCrossman/nest-swagger-multi-spec-enums-bug
Steps to reproduce
nest new project-namecd project-namenpm install @nestjs/swaggernest g module catsnest g class cats/cat --flatEdit
src/cats/cat.ts, to haveCollarColorEnumandApiProperty:nest g controller catsEdit
src\cat\cat.controller.ts, to havefindendpoint:Initialize Swagger with multiple specifications:
npm run start:devGo to
http://localhost:3000/api-jsonExpected behavior
The enum schema
CollarColorshould be defined with enum values and not only appear as typestringin the specification. It should be expected that both thehttp://localhost:3000/api-jsonspecification andhttp://localhost:3000/api/cats-jsonshould be identical.Package version
6.1.3
NestJS version
9.1.5
Node.js version
v18.12.1
In which operating systems have you tested?
Other
No response