It looks like typescript enum types are not supported yet
in rsponse definition i set ConfigModel as type response like this
// model definition
export enum EnvConfig {
PROD,
DEV
}
export class ConfigModel {
@IsString()
@ApiModelProperty()
domain: string;
@IsInt()
@ApiModelProperty()
port: number;
@ApiModelProperty()
env: EnvConfig;
}
// controller
@Post('api')
@ApiResponse({status: 200, type: ConfigModel})
api(@Body() data: ConfigModel): ConfigModel {
return data;
}
in the swagger-ui docs it's represented as number:
ConfigModel {
domain: string
port: number
env: number
}
it's not supported or I did something wrong ?
I'm willing to help and do PR with this feature if anyone will point me in right direction
btw: grate job with the nestjs, i think it's gonna by the best choice for node developers
Did you find an alternative while waiting for the feature ?
Edit: Oh I see a PR already :-P
https://github.com/nestjs/swagger/pull/44
I don't think this can be achieved because enums are compiled to objects.
At runtime, your EnvConfig looks like:
{
"PROD": 0,
"DEV": 1,
0: "PROD",
1: "DEV"
}
_(Have a look there to know why https://www.typescriptlang.org/docs/handbook/enums.html)_
It is pretty risky to try generating a documentation from this...
However, we could implement the support of a enum: string[] | number[] | (string|number)[] property for the @ApiModelProperty() decorator.
Things would look like this:
// model definition
export enum EnvConfig {
PROD,
DEV
}
export class ConfigModel {
@IsString()
@ApiModelProperty()
domain: string;
@IsInt()
@ApiModelProperty()
port: number;
@ApiModelProperty({ enum: [ 'PROD', 'ENV' ] })
// Or
// @ApiModelProperty({
// enum: Object.keys(EnvConfig).filter(k => Number.isNaN(parseInt(k)))
// })
env: string;
}
// controller
@Post('api')
@ApiResponse({status: 200, type: ConfigModel})
api(@Body() data: ConfigModel): ConfigModel {
return data;
}
This would be documented as a proper Swagger enum:
ConfigModel {
domain: string
port: number
env: string
enum: [ PROD, DEV ]
}
This probably isn't what you was expecting for, but, IMO, this is the best we can safely do.
I just submitted a PR for this. Please have a look at it and give me any impression ;)
As far as I know, Enum type can be generated and can be in its own definitions. I've been using lukeatry/tsoa package (https://github.com/lukeautry/tsoa) and its Swagger Generator works beautifully with enums. I am an Angular guy so I love Nest's approach and I think it's potentially the best and go-to framework to develop WebAPI with Node. Maybe take a look at TSOA and find some direction.
Here's how I set my Enum:
export enum UserRole {
Admin = 'Admin' as any,
User = 'User' as any
}
Here's how the SwaggerSpec file looks like:
UserRole: {
enum: [
"Admin",
"User"
],
type: "string"
},
UserVm: {
properties: {
createdOn: {
type: "string",
format: "date-time"
},
updatedOn: {
type: "string",
format: "date-time"
},
_id: {
type: "string"
},
username: {
type: "string"
},
password: {
type: "string"
},
role: {
$ref: "#/definitions/UserRole"
}
},
type: "object"
}
PR #53 has been merged. I'll publish it soon! 馃檪
Great news that this is already added! You could mention this feature in the docs though, since I just came all the way here searching how to use enums in my API :)
Now we have the plugin running in the compile time, could the plugin help us telling the enum values to Swagger? @fwoelffel
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
PR #53 has been merged. I'll publish it soon! 馃檪