Reputation: 60357
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:
How can I set the datatype of the param to string
for swagger?
Try it out here:
Upvotes: 2
Views: 2231
Reputation: 19070
You can import ApiParam from Swagger:
nestjs
v8.2.6@nestjs/swagger
v5.2.0Code:
import { ApiParam } from '@nestjs/swagger';
@Get(':id')
@ApiParam({ name: 'id', type: String })
getHello(@Param('id', MyStringPipe) myString: MyString): string {
return myString.toString();
}
Upvotes: 1
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