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

Enum schema missing information on multiple specifications #2182

Open
3 of 4 tasks
DJCrossman opened this issue Dec 3, 2022 · 0 comments
Open
3 of 4 tasks

Enum schema missing information on multiple specifications #2182

DJCrossman opened this issue Dec 3, 2022 · 0 comments

Comments

@DJCrossman
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

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

  1. nest new project-name

  2. cd project-name

  3. npm install @nestjs/swagger

  4. nest g module cats

  5. nest g class cats/cat --flat

  6. 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;
    }
  7. nest g controller cats

  8. 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 [];
      }
    }
  9. 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();
  10. npm run start:dev

  11. 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?

  • macOS
  • Windows
  • Linux

Other

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant