nestjs / swagger

OpenAPI (Swagger) module for Nest framework (node.js) :earth_americas:

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parameter not marked as optional, when using optional type

paulwer opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I provide an additional parameter to the ApiProperty Decorator, to achieve the right behavior.

  @ApiProperty({ required: false }) // isOptional is not reconized by swagger in this case?!
  @IsOptional()
  @IsObject()
  meta?: {
    [key: string]: any;
  }

Minimum reproduction code

none? but I can provide, if realy needed

Steps to reproduce

  1. create a DTO with a property with dynamic keys.
  2. view output of swagger-file

Expected behavior

should detect, that the parameter is optional.

Package version

7.1.16

NestJS version

10.2.10

Node.js version

18.16.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project).

https://stackblitz.com/edit/nestjs-typescript-starter-hevk9d?file=src%2Ftest%2Fdto%2Fcreate-test.dto.ts

I found, that this is not related to meta?: { [key: string]: any; } types, and instead is present for all params. Is this a wanted behavior?

This has been discussed several times in the past.

If you want @nestjs/swagger to automatically detect optional attributes, use CLI plugin (see docs).
Otherwise, we don't take into account ? as there's no metadata we could rely on (after the transpilation). We do ignore class-validator decorators too (as this is a separate package).