swagger icon indicating copy to clipboard operation
swagger copied to clipboard

Define custom name in ApiModelProperty

Open vgladimir opened this issue 7 years ago • 8 comments

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

vgladimir avatar Jan 09 '19 08:01 vgladimir

+1

sysoft avatar Mar 20 '19 11:03 sysoft

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

kamilmysliwiec avatar Apr 15 '19 15:04 kamilmysliwiec

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

maxfriedmann avatar May 16 '19 15:05 maxfriedmann

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

abouroubi avatar Jun 29 '19 15:06 abouroubi

Yeah using class-transformer makes using swagger difficult

kaykaul avatar Oct 10 '19 08:10 kaykaul

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

kamilmysliwiec avatar Oct 30 '19 14:10 kamilmysliwiec

4.0.0 has been published

kamilmysliwiec avatar Dec 03 '19 10:12 kamilmysliwiec

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[bot] avatar Apr 25 '20 10:04 lock[bot]