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

ApiProperty with enum undefined changes type #3229

Open
2 of 4 tasks
nxht opened this issue Jan 8, 2025 · 3 comments
Open
2 of 4 tasks

ApiProperty with enum undefined changes type #3229

nxht opened this issue Jan 8, 2025 · 3 comments

Comments

@nxht
Copy link

nxht commented Jan 8, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Query parameter name shown as number despite the dto type says string

Minimum reproduction code

https://github.com/nxht/test-nestjs

Steps to reproduce

  1. pnpm install
  2. pnpm start:dev
  3. Open http://localhost:3000/docs#/App/AppController_getHello

Expected behavior

Query parameter name should be string

Package version

8.1.0

NestJS version

10.4.15

Node.js version

20.9.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

DTO tested:

export class TestDto {
  @ApiProperty({
    type: 'string',
    enum: undefined,
  })
  name: string;
}

When the enum: undefined is removed, it works normally.

@kamilmysliwiec
Copy link
Member

What's the point of setting enum to undefined?

@nxht
Copy link
Author

nxht commented Jan 9, 2025

What's the point of setting enum to undefined?

Currently using custom decorator to wrap the ApiProperty.
During the step there's change on the key name from enumType => enum.
I didn't explicitly set the enum to be undefined. But if the enumType is missing, it'll pass undefined to the enum, causing the issue.

Simple example would be like

import { ApiProperty } from '@nestjs/swagger';

const CustomDecorator = ({ enumType }: { enumType?: object } = {}) => {
  return (object: object, propertyName: string): void =>
    ApiProperty({ type: 'string', enum: enumType })(object, propertyName);
};

export class TestDto {
  @CustomDecorator()
  name: string;
}

I do know this can be solved by checking enumType before setting it to ApiProperty and I fixed my case this way. But I don't think it's an expected behaviour.

@kamilmysliwiec
Copy link
Member

Got it. Sounds like this could be easily fixed by adding another condition to the if statement here https://github.dev/nestjs/swagger/blob/e19d30ce2d807001519c29f30030a88c04d7e5c3/lib/decorators/api-property.decorator.ts#L67 Would you like to create a PR for this?

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

2 participants