Swagger: Define custom name in ApiModelProperty

Created on 9 Jan 2019  路  8Comments  路  Source: nestjs/swagger

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

export class FooDto {
  @ApiModelProperty()
  readonly timeRange: TimeRange;
}

image

Expected behavior

export class FooDto {
  @ApiModelProperty({ name: 'time_range' })
  readonly timeRange: TimeRange;
}

image

What is the motivation / use case for changing the behavior?

We have API style in underscore case, but JS coding style is camel case and we can't use in DTO classes underscore variables.

Environment


Nest version: 5.3.7

For Tooling issues:
- Node version: v10.15.0
- Platform: Mac

Most helpful comment

Is there any news about this feature ? I think it's valid use case with class-transformer

All 8 comments

+1

But how would you access timeRange then? Your DTO should be consistent with definitions shown via Swagger UI.

E.g. if you use class-transformer to transform fields, then your DTO differs from your JS Class.

I am using class-transformer to transform the MongoDB related field _id to a better named property id. Unfortunately, if I attach @ApiModelProperty() it still gets exposed as _id.

export class SomeModel {
  @ApiModelProperty()
  @IsString()
  @Expose({
    name: "id",
    toPlainOnly: true
  })
  public _id: string;
}

I agree with you @kamilmysliwiec... The DTO should be consistent with the SwaggerUI, unfortunately, in my case, the Typescript Class used by NestJS differs from the DTO.

For the time being I added another private field and annotated that one:

  @ApiModelProperty()
  private id: string; // internal use for swagger

Is there any news about this feature ? I think it's valid use case with class-transformer

Yeah using class-transformer makes using swagger difficult

Added in the next version (to install run npm i @nestjs/swagger@next). Note: remember to update @nestjs/common, @nestjs/core and typescript as well to ensure that you're using the latest versions.

Steps to migrate: https://github.com/nestjs/swagger/pull/355#issuecomment-547925879

4.0.0 has been published

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

djedlajn picture djedlajn  路  4Comments

alisherks picture alisherks  路  4Comments

itslenny picture itslenny  路  3Comments

ivashog picture ivashog  路  3Comments

ph55 picture ph55  路  3Comments