nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.91k stars 7.55k forks source link

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

Closed maturanomx closed 1 year ago

maturanomx commented 1 year ago

Is there an existing issue for this?

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

Other package

@nestjs/swagger

NestJS version

9.0.0

Packages versions

  "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/swagger": "^6.3.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0"
  },

Node.js version

v18.16.0

In which operating systems have you tested?

Other

No response

maturanomx commented 1 year ago

Wrong repo; sorry. I open it in the right repository: https://github.com/nestjs/swagger/issues/2479

Probably their CONTRIBUTION file points to this repository and I didn't notice it