Search code examples
javascriptnode.jsswaggerswagger-uinestjs

How to set the data type for a route param in swagger?


I have a simple nest.js controller with a route param:

@Get(':id')
getHello(@Param('id', MyStringPipe) myString: MyString): string {
  return myString.toString();
}

The data type of the param is transformed from string to MyString with a simple pipe:

export class MyStringPipe implements PipeTransform {
  transform(value: string, metadata: ArgumentMetadata) {
    return new MyString(value);
  }
}

Now, when I want to try the route with swagger-ui, it rejects a string param:

enter image description here

How can I set the datatype of the param to string for swagger?


Try it out here:

Edit nest-swagger-ui-param-type


Solution

  • You can define the type of a param with the @ApiImplicitParam decorator:

    import { ApiImplicitParam } from '@nestjs/swagger';
    
    @Get(':id')
    @ApiImplicitParam({ name: 'id', type: String })
    getHello(@Param('id', MyStringPipe) myString: MyString): string {
      return myString.toString();
    }