swagger icon indicating copy to clipboard operation
swagger copied to clipboard

@Body() decorator with param broke swagger, swagger plugin auto @ApiProperty() doesn't work

Open PoOwAa opened this issue 4 years ago • 21 comments

I'm submitting a...


[ x ] Regression 
[ x ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I faced 2 issues when I use NestCLI to start my application with swagger plugin in nest-cli.json:

  1. @Body('param') is not part of the request, I have to write wrapper DTO or use schema
  2. The auto @ApiProperty() doesn't work. I have to use @ApiProperty in all of my DTO files.

I tried to reproduce nest-example-cats-swagger in my own repository, but it doesn't work like the Docs mentions here

Expected behavior

  1. When I use @Body('param'), swagger maybe could wrap the type inside that parameter name.
  @Post('noDoc/param/scheme')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        user: { // the `user` word comes from @Body('user')
          properties: { // these properties are coming from `UserDto`
            id: { type: 'number' },
            username: { type: 'string' },
            email: { type: 'string' },
          },
        },
      },
    },
  })
  async noDocParamScheme(@Body('user') user: UserDto) {
    return user;
  }
  1. Swagger plugin should properly create @ApiProperty

Minimal reproduction of the problem with instructions

You can check it in my test-repository: https://github.com/PoOwAa/nestjs-swagger-body-param-bug

What is the motivation / use case for changing the behavior?

At least @Body() without params should work like in the documentation.

Environment


[System Information]
OS Version     : Linux 5.3
NodeJS Version : v12.16.1
NPM Version    : 6.14.4

[Nest CLI]
Nest CLI Version : 7.1.2

[Nest Platform Information]
platform-express version : 7.0.0
swagger version          : 4.5.1
common version           : 7.0.0
core version             : 7.0.0

PoOwAa avatar Apr 09 '20 19:04 PoOwAa

I noticed today that SOME of my DTO schemas are not described completely in Swagger (they were a few days ago). I went through all the settings in docs again, updated dep., I have my plugin configured correctly. I checked that the swagger description works again when I add ApiProperty(). I will try to add more info later.

[System Information]
OS Version     : Windows 10
NodeJS Version : v12.16.1
NPM Version    : 6.14.4

[Nest CLI]
Nest CLI Version : 7.1.1

[Nest Platform Information]
platform-express version : 7.0.8
swagger version          : 4.5.3
common version           : 7.0.8
config version           : 0.4.0
core version             : 7.0.8

I tried to nest update from these versions today to solve the issue..

    "@nestjs/common": "^7.0.7",
    "@nestjs/config": "0.4.0",
    "@nestjs/core": "^7.0.7",
    "@nestjs/platform-express": "^7.0.7",
    "@nestjs/swagger": "^4.5.1",

dkocich avatar Apr 21 '20 17:04 dkocich

I also have same issue

tranquangvu avatar Apr 28 '20 06:04 tranquangvu

I'm encountering the same behavior as well.

adrian-fundmore avatar Apr 28 '20 07:04 adrian-fundmore

I also have this issue.

zveljkovic avatar May 06 '20 21:05 zveljkovic

I also have same issue

k-efimov avatar May 29 '20 14:05 k-efimov

I also have same issue

east2dd avatar Jun 10 '20 22:06 east2dd

I also have same issue

mikeharty avatar Aug 16 '20 02:08 mikeharty

having the same issue, swagger-cli does not work as expected.

vbrdnk avatar Sep 24 '20 10:09 vbrdnk

Exact same problem here, need to use @ApiProperty() directly for it to work.

MikaelBrenner avatar Nov 25 '20 17:11 MikaelBrenner

I also have same issue

s97712 avatar Dec 07 '20 12:12 s97712

I also have same issue

nvthuong1996 avatar Dec 17 '20 11:12 nvthuong1996

I also have same issue

vuongkien avatar Dec 28 '20 10:12 vuongkien

Any news on this? I have the same issue.

mojo2012 avatar Aug 11 '21 13:08 mojo2012

I want a little bit to promote this issue, as my company is also catching it.

barkandrii avatar May 11 '22 10:05 barkandrii

I had the same issue and could resolve it by including ExtraModels to swagger main configuration: docs

Akhouad avatar May 26 '22 15:05 Akhouad

@Akhouad Docs mention that you need to use it for models, which are not referenced by any controllers. But my controller is using the DTO, and I can create manually with ApiProperty decorator, but nest-cli promises, that it can find out automagically without manual description.

Did you add the ApiExtraModels decorator to a DTO, which is already referenced in a controller?

PoOwAa avatar May 26 '22 15:05 PoOwAa

@PoOwAa , i basically had this issue after trying to create a new decorator for a paginated response, following the steps in this docs page and the error I had was complaining about PaginatedDto schema which couldn't be resolved, so I added the extraModels in swagger main configuration with PaginatedDto, it looked like this:

const document = SwaggerModule.createDocument(app, config, {
  extraModels: [PaginatedDto]
});

Akhouad avatar May 26 '22 16:05 Akhouad

Any update on this issue?

I am facing the same thing. As described this happened when setting the property attribute like @Body(‘property’)

yossipapi avatar Jul 04 '22 16:07 yossipapi

@PoOwAa , i basically had this issue after trying to create a new decorator for a paginated response, following the steps in this docs page and the error I had was complaining about PaginatedDto schema which couldn't be resolved, so I added the extraModels in swagger main configuration with PaginatedDto, it looked like this:

const document = SwaggerModule.createDocument(app, config, {
  extraModels: [PaginatedDto]
});

It's works for me!

Tsugami avatar Sep 20 '22 02:09 Tsugami

same issue. It doesn't work without adding the @ApiPropterty() decorator. IT does give error for circular dependency if we add the model in API extra models.

ankibalyan avatar Jun 09 '23 06:06 ankibalyan

I was following along with the docs on using a Generic schema + using the CLI Plugin. This was giving me the circular dep error that @ankibalyan mentioned above. I was able to fix this by manually setting an @ApiProperty for the generic property, so PaginatedDto would look like this:


export class PaginatedDto<TData = unknown> {
  total: number;

  limit: number;

  offset: number;

  @ApiProperty({ type: () => Array })
  results: TData[];
}

incutonez avatar Feb 15 '24 01:02 incutonez