Swagger: I require object list of a class in ApiModelProperty

Created on 24 Feb 2019  路  7Comments  路  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


Right now `@ApiResponseModelProperty({ ... })
I require object list of a class.

Expected behavior


@ApiResponseModelProperty I want to be able to get a list of my class

Minimal reproduction of the problem with instructions


i have 2 class: Articulo and UnidadMedida
export class UnidadMedida extends BaseModel { @prop() descripcion: string; @prop() abrev: string; }

Class: Articulo
export class Articulo extends BaseModel { @prop() nombre: string; @prop({ ref: UnidadMedida, required: true }) unidadmedida_id: Ref<UnidadMedida>; }

I have created a record class to show in the swagger, but I want to list the _id of my class unit measure

export class RegisterArticuloVm { @ApiModelProperty() nombre: string; @ApiModelProperty() unidadmedida_id: UnidadMedida (is my class UnidadMedida); }

image

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

I'm new using swagger, i don't understand as obtain list of my class with ApiResponse Model Property

PD: I Love NESTJS 鉂わ笍

Most helpful comment

Hey @Russ3ll-Franz, you need to setup UnidadMedia with ApiModelProperties as well to display in the model. Something like below might help, the @IsString(), @Type() & @ValidateNested() decorators is for validation so might not be needed for your use case, but validation is always handy to have.

import { Type } from 'class-transformer';

export class UnidadMedidaDto {
  @ApiModelProperty({
    required: true,
    type: String,
  })
  @IsString()
  _id: string;

@ApiModelProperty({
    required: true,
    type: String,
  })
  @IsString()
  nombre: string;
}

export class RegisterArticuloVm { 
@ApiModelProperty() nombre: string; 

@ApiModelProperty({
    description: 'My descriptionr',
    required: true,
    type: UnidadMedidaDto ,
  })
  @ValidateNested()
  @Type(() => UnidadMedidaDto)

 unidadMedida: UnidadMedidaDto; 
}

`

All 7 comments

Hey @Russ3ll-Franz, you need to setup UnidadMedia with ApiModelProperties as well to display in the model. Something like below might help, the @IsString(), @Type() & @ValidateNested() decorators is for validation so might not be needed for your use case, but validation is always handy to have.

import { Type } from 'class-transformer';

export class UnidadMedidaDto {
  @ApiModelProperty({
    required: true,
    type: String,
  })
  @IsString()
  _id: string;

@ApiModelProperty({
    required: true,
    type: String,
  })
  @IsString()
  nombre: string;
}

export class RegisterArticuloVm { 
@ApiModelProperty() nombre: string; 

@ApiModelProperty({
    description: 'My descriptionr',
    required: true,
    type: UnidadMedidaDto ,
  })
  @ValidateNested()
  @Type(() => UnidadMedidaDto)

 unidadMedida: UnidadMedidaDto; 
}

`

Thanks for helping @MumblesNZ

Would be great to have that scenario working for generics.
For example, having something like BaseRespone<T> and then using in the specific controller BaseResponse<Place>

Here is a working example for list of objects. Swagger version 2.9.2. All that is needed is for the dataType to define as "List" and it will render in the swagger documentation. Find attached the ProductAll list rendered in the attached picture.
Screen Shot 2019-07-31 at 03 59 08

@ApiModel
public class ProductGetAllDTO {
    @ApiModelProperty(example="20")
    private String count;
    @ApiModelProperty(dataType="List", value = "rows")
    private List<ProductAll> rows;
}

@tksilicon Thank you. What if I don't have a list, just one inner property?

@tksilicon - sorry for hijacking this thread, but I am wondering if you have any idea my related data is not populating in swagger.

(Note: I was able to get most relationships working, but the one class I have of PartAvailability is not being loaded into swagger)

@Entity()
export class Part {
  @PrimaryGeneratedColumn()
  @ApiModelProperty()
  id: number;

  @Column({ length: 100 })
  @ApiModelProperty()
  name: string;

  @Column({ type: 'text', enum: ['ONE-TIME', 'SUBSCRIPTION'] })
  @ApiModelProperty({ enum: ['ONE-TIME', 'SUBSCRIPTION'] })
  type?: 'ONE-TIME' | 'SUBSCRIPTION';

  @Column({ type: 'boolean', default: true })
  @ApiModelProperty()
  hasInventory: boolean;

  @Column({ type: 'double precision', default: 0 })
  @ApiModelProperty()
  price?: number;

  @Column({ type: 'boolean', default: true })
  @ApiModelProperty()
  active: boolean;

  @OneToOne(type => PartAvailability, partAvailability => partAvailability.part, { nullable: true })
  @JoinColumn()
  @ApiModelProperty({ type: PartAvailability })
  availability?: PartAvailability;

  @CreateDateColumn({ type: 'timestamp', default: new Date() })
  @ApiModelProperty({ type: String, format: 'date-time' })
  created: Date;

  @UpdateDateColumn({ type: 'timestamp', default: new Date() })
  @ApiModelProperty({ type: String, format: 'date-time' })
  updated: Date;
}
@Entity()
export class PartAvailability {
  @PrimaryGeneratedColumn()
  @ApiModelProperty()
  id: number;

  @Column({ type: 'integer' })
  @ApiModelProperty()
  totalQty: number;

  @Column({ type: 'integer' })
  @ApiModelProperty()
  availableQty: number;

  @Column({ type: 'integer', default: 0 })
  @ApiModelProperty()
  reservedQty: number;

  @OneToOne(type => Part, part => part.availability, { cascade: true })
  part: Part;

  @OneToMany(type => PartReservation, partReservation => partReservation.partAvailability)
  reservations: PartReservation[];

  @CreateDateColumn({ type: 'timestamp', default: new Date() })
  @ApiModelProperty({ type: String, format: 'date-time' })
  created: Date;

  @UpdateDateColumn({ type: 'timestamp', default: new Date() })
  @ApiModelProperty({ type: String, format: 'date-time' })
  updated: Date;
}

image

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