-
Notifications
You must be signed in to change notification settings - Fork 480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enum type support #19
Comments
Did you find an alternative while waiting for the feature ? Edit: Oh I see a PR already :-P |
I don't think this can be achieved because enums are compiled to objects. {
"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 // 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:
This probably isn't what you was expecting for, but, IMO, this is the best we can safely do. |
As far as I know, Enum type can be generated and can be in its own 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. |
It looks like typescript enum types are not supported yet
in rsponse definition i set ConfigModel as type response like this
in the swagger-ui docs it's represented as 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
The text was updated successfully, but these errors were encountered: