Kim Kern
Kim Kern

Reputation: 60357

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

Upvotes: 2

Views: 2231

Answers (2)

Yosvel Quintero
Yosvel Quintero

Reputation: 19070

You can import ApiParam from Swagger:

  • nestjs v8.2.6
  • @nestjs/swagger v5.2.0

Code:

import { ApiParam } from '@nestjs/swagger';

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

Upvotes: 1

Kim Kern
Kim Kern

Reputation: 60357

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();
}

Upvotes: 1

Related Questions