Reputation: 383
I have a controller with a POST operation to save ReasonCode. Here is what I have in my controller:
import {
CustomAuthGuard,
ReasonCodeService,
ReasonCode,
} from 'cnv-ms-core';
export class ReasonCodeController {
constructor(private readonly reasonCodeService: ReasonCodeService) {}
}
@Post('reason-codes')
@ApiOperation({ summary: 'Save Reason Code' })
@ApiBody({
//type: ReasonCode,
description: 'Reason Code',
required: true,
isArray: false,
})
@ApiResponse({
status: 201,
description: 'Reason Code is Saved Successfully.'
})
async saveReasonCode(
@Body() newReasonCode: ReasonCode,
): Promise<ReasonCode | null> {
return this.reasonCodeService.saveReasonCode(newReasonCode);
}
Here is my interface object:
export interface ReasonCode {
name: string;
description: string;
type: ReasonCodeTypeEnum;
isVisible: boolean;
createdAt?: Date;
updatedAt?: Date;
}
As you can see in the above snippet of my controller, I commented out the 'type' in the '@ApiBody' decorator. I wanted to add this, but when I uncomment I see the error 'ReasonCode only refers to a type, but is being used as a value here' and vs code offers the quick fix to add ReasonCode to imports. But, I already have ReasonCode in imports. How can I add this in the @ApiBody to see it in swagger-ui.
Thanks for the help.
Upvotes: 1
Views: 20422
Reputation: 629
Instead of using an interface to add a type to the body you should use a class to do that and then just add the @ApiProperty() decorator to each property of the class, it's a cleaner solution because you can avoid the @ApiBody() Decorator.
Also, when using a class you can take advantage of the class validator decorators to validate the body before using it.
ApiProperty is exported from @nestjs/swagger
More info here: https://docs.nestjs.com/openapi/types-and-parameters#types-and-parameters
Also check this info about class-validator: https://docs.nestjs.com/pipes#class-validator
Upvotes: 2
Reputation: 6695
changing export interface ReasonCode
to export class ReasonCode
might solve this
Upvotes: 0