nestjs / swagger

OpenAPI (Swagger) module for Nest framework (node.js) :earth_americas:
https://nestjs.com
MIT License
1.68k stars 469 forks source link

Some of @ApiOperation decorator options are being ignored or overwritten #2859

Open ivan-sennov opened 7 months ago

ivan-sennov commented 7 months ago

Is there an existing issue for this?

Current behavior

When @ApiOperation decorator is used, such options as "parameters", "tags" and "responses" are ignored and doesn't affect the resulting swagger json. You have to use @ApiResponse and other corresponding decorators to reach needed effect.

Code

@Controller('cats')
@ApiTags('cats')
class CatsController {
  @Get()
  @ApiOperation({
    // This works
    description: 'Find cats',
    summary: 'Short summary',
    operationId: 'findCats',

    // The next options are being ignored
    parameters: [{ name: 'color', in: 'query', schema: { type: 'string' } }],
    tags: ['search'],
    responses: {
      '2xx': {
        description: 'Cat',
        content: {
          // ...
        },
      },
    },
  })
  find(@Query query: Record<string, string>) {
    // ...
  }
}

Result

image

Minimum reproduction code

See example above

Steps to reproduce

No response

Expected behavior

When @ApiOperation decorator is used, all used options have the appropriate effect. If there are more specific decorators are used, such as @ApiResponse, they have a higher priority.

Package version

7.2.0

NestJS version

10.3.1

Node.js version

18.19.0

In which operating systems have you tested?

Other

Sometimes there is no properly typed models or interfaces are used and you need to manually describe the entire endpoint, including the query parameters and response types. In such case it may be inconvenient to use the different decorators and I would like to use the mentioned one to cover all properties of operation. Moreover, the type ApiOperationOptions which @ApiOperation accepts as an options allows passing such things in it but never applies them, which can be misleading.

During my investigation, I found the next code in the SwaggerExplorer class:

https://github.com/nestjs/swagger/blob/6633c3639f7f278c294f13504dcaa2d3bfb5b17f/lib/swagger-explorer.ts#L95-L104

Explorers are processed sequentially and, as a result, parameters and other mentioned fields which comes from ApiOperationExplorer are being overwritten by the next explorers here https://github.com/nestjs/swagger/blob/6633c3639f7f278c294f13504dcaa2d3bfb5b17f/lib/swagger-explorer.ts#L182

Thus, we can not manually set such properties with @ApiOperation decorator.

mastermatt commented 6 months ago

+1

When I discovered it wasn't possible to manually set a $ref for a header param via @ApiHeader, I was hoping I could get around it manually using @ApiOperation. Only to find params don't work at all in @ApiOperation.

paulwer commented 6 months ago

same here :(