swagger icon indicating copy to clipboard operation
swagger copied to clipboard

`example` & `default` properties not behavie as expected

Open maturanomx opened this issue 2 years ago • 0 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current behavior

The Swagger page is taking default values from the example property in the declaration.

Screenshot 2023-06-15 at 20 24 49

This is especially an issue when the property is an array due to there is no way to really not sent the query param:

Screenshot 2023-06-15 at 20 28 39

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-aprkdl?file=src%2Fapp.controller.ts

Steps to reproduce

Declare an optional property as in:

  // input text will have `something` as value
  @ApiPropertyOptional({
    default: 'default is ignored',
    description: 'some string',
    example: 'something',
  })
  q?: string;

  // No option selected but the request will include `?options=one`
  @ApiPropertyOptional({
    description: 'some options',
    enum: OPTIONS,
    example: OPTIONS[0],
    isArray: true,
  })
  options?: (typeof OPTIONS)[number];

Expected behavior

Default values in SwaggerUI should not be taken from the example property but from the default if there is any

Package version

6.3.0

NestJS version

9.0.0

Node.js version

v18.16.0

In which operating systems have you tested?

  • [X] macOS
  • [ ] Windows
  • [ ] Linux

Other

No response

maturanomx avatar Jun 16 '23 02:06 maturanomx