swagger icon indicating copy to clipboard operation
swagger copied to clipboard

Some of @ApiOperation decorator options are being ignored or overwritten

Open ivan-sennov opened this issue 1 year ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

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?

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

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.

ivan-sennov avatar Feb 28 '24 17:02 ivan-sennov

+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.

mastermatt avatar Mar 20 '24 00:03 mastermatt

same here :(

paulwer avatar Mar 20 '24 07:03 paulwer

related to https://github.com/nestjs/swagger/pull/1454 which is unfortunatelly locked and long abandoned

SmallhillCZ avatar Oct 17 '24 16:10 SmallhillCZ

Let's track this here https://github.com/nestjs/swagger/pull/1454

kamilmysliwiec avatar Oct 28 '24 13:10 kamilmysliwiec

#1454 is locked...

paulwer avatar Oct 28 '24 16:10 paulwer

and that makes sense since the PR was merged in, a new release has been published, and consequently this issue is no longer present in the latest version

kamilmysliwiec avatar Oct 28 '24 16:10 kamilmysliwiec