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

Route param does not appear in Swagger UI when handler's argument has non-primitive type

quallrum opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Let's assume we have a route param that we obtain in the route handler using @Param() decorator on the handler's argument. If that argument has non-primitive type (i.e. we use entity resolving pipe) then that param does not appear in the Swagger UI. But it works well if argument type has primitive type, even object.

That code will render id route param properly in Swagger UI:

import { Controller, Get, Param } from '@nestjs/common';
import { ApiParam } from '@nestjs/swagger';

@Controller('/test/:id')
@ApiParam({
    name: 'id',
})
export class TestController {
    @Get('/')
    public test(@Param('id') qwerty: string) {}
}

image

At the same time, the following example will not show any route params:

import { Controller, Get, Param } from '@nestjs/common';
import { ApiParam } from '@nestjs/swagger';
import { User } from './path/to/user.entity';

@Controller('/test/:id')
@ApiParam({
    name: 'id',
})
export class TestController {
    @Get('/')
    public test(@Param('id') qwerty: User) {}
}

image

Also works if i use object as argument type and does not when using any class

Minimum reproduction code

See examples and screenshots above

Steps to reproduce

No response

Expected behavior

It is expected for route params to appear in Swagger UI no matter what types they have in route handlers. A possible solution may be to prefer type from @ApiParam() decorator's options, if provided, over the argument type.

Package version

7.3.0

NestJS version

10.3.3

Node.js version

20.11

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

The project is running inside Docker on WSL Ubuntu using node:20.11-bullseye as base image

Other NestJS dependencies used:

{
  "dependencies": {
    "@golevelup/nestjs-rabbitmq": "^4.1.0",
    "@nestjs/cache-manager": "^2.1.1",
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.1.1",
    "@nestjs/core": "^10.0.0",
    "@nestjs/jwt": "^10.1.1",
    "@nestjs/microservices": "^10.2.7",
    "@nestjs/passport": "^10.0.2",
    "@nestjs/platform-express": "^10.0.0",
    "@nestjs/platform-socket.io": "^10.3.0",
    "@nestjs/swagger": "^7.3.0",
    "@nestjs/throttler": "^5.0.0",
    "@nestjs/typeorm": "^10.0.0",
    "@nestjs/websockets": "^10.3.0",
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.0.0"
  }
}