Skip to content
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

Closed
myflowpl opened this issue Dec 6, 2017 · 7 comments
Closed

enum type support #19

myflowpl opened this issue Dec 6, 2017 · 7 comments

Comments

@myflowpl
Copy link

myflowpl commented Dec 6, 2017

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

@otroboe
Copy link

otroboe commented Feb 9, 2018

Did you find an alternative while waiting for the feature ?

Edit: Oh I see a PR already :-P
#44

@fwoelffel
Copy link
Contributor

fwoelffel commented Feb 9, 2018

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

@nartc
Copy link
Contributor

nartc commented Mar 6, 2018

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"
}

@kamilmysliwiec
Copy link
Member

PR #53 has been merged. I'll publish it soon! 🙂

@quezak
Copy link

quezak commented Jun 18, 2018

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 :)

@Menci
Copy link

Menci commented Jan 31, 2020

Now we have the plugin running in the compile time, could the plugin help us telling the enum values to Swagger? @fwoelffel

@lock
Copy link

lock bot commented May 5, 2020

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.

@lock lock bot locked as resolved and limited conversation to collaborators May 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants