Swagger: ApiModelProperty groups

Created on 25 Nov 2017  路  14Comments  路  Source: nestjs/swagger

Hi,

thank you with your contribution of nest and swagger.

I have a question, is there a possibility that the properties with the decorator @ApiModelProperty support groups?

Similar to class-validator groups https://github.com/typestack/class-validator#validation-groups.

export class MyClass {

    @ApiModelProperty({ groups: ["edit"] })
    id: string;

    @ApiModelProperty({ groups: ["edit", "create"] })
    name: string;

    @ApiModelProperty({ groups: ["edit", "create"] })
    description: string;

}

in controller

@ApiImplicitBody({ required: true, type: MyClass, name: "name", groups:["create"]  })
//or
@ApiResponse({ status: 200, description: "OK", type: MyClass, groups:["edit"] })


or is there an alternative to not show all the attributes in a response or body? Currently I am creating additional classes.

Thanks

feature request

Most helpful comment

In the latest release (4.5.0), we added PartialType, OmitType, and PickType functions which can mimic the behavior of built-in TypeScript types (respectively Partial, Omit, and Pick).

Example:

class CreateUserDto {
  @ApiProperty()
  email: string;

  @ApiProperty()
  password: string;

  @ApiProperty()
  firstName: string
}

and to create the UpdateUserDto class based on the CreateUserDto, but with all properties marked as optional, use the following code:

export class UpdateUserDto extends PartialType(CreateUserDto) {}

If you want to exclude, let's say, the email property (because, for instance, email has to be unique and cannot be modified), compose OmitType and PartialType functions, as follows:

export class UpdateUserDto extends PartialType(OmitType(CreateUserDto, ['email'])) {}

All 14 comments

Is there any plan to implement this? I think it should be really useful to avoid repetition.

Its a very useful functionality. Hope will add in future.

I would be interested in this feature too !
I don't know what the better way to transform my model depending on the controller method. Interceptor ? Manual transformer ?

What did you end up going with @otroboe ?

@aequasi I did custom transformers, not the best, but the simplest I guess :-/

Any way we can get this moving @kamilmysliwiec ? This is a super handy feature that i'd love to have available here.

edit: To add, without this, data is duplicated all over the place

+1

+1

+1

ping ?

Is there any solution to this issue other than creating a separate DTO?

+1

In the latest release (4.5.0), we added PartialType, OmitType, and PickType functions which can mimic the behavior of built-in TypeScript types (respectively Partial, Omit, and Pick).

Example:

class CreateUserDto {
  @ApiProperty()
  email: string;

  @ApiProperty()
  password: string;

  @ApiProperty()
  firstName: string
}

and to create the UpdateUserDto class based on the CreateUserDto, but with all properties marked as optional, use the following code:

export class UpdateUserDto extends PartialType(CreateUserDto) {}

If you want to exclude, let's say, the email property (because, for instance, email has to be unique and cannot be modified), compose OmitType and PartialType functions, as follows:

export class UpdateUserDto extends PartialType(OmitType(CreateUserDto, ['email'])) {}

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

Diluka picture Diluka  路  4Comments

patilrevansidh picture patilrevansidh  路  4Comments

malbertSC picture malbertSC  路  5Comments

dennisameling picture dennisameling  路  4Comments

vh13294 picture vh13294  路  4Comments