Search code examples
swaggertypeormnestjs

How to add the ApiModelProperty decorator to a ManyToOne relationship


I'm using Nest.js with the TypeORM and Swagger modules. So far documenting the API endpoints with Swagger works like a charm, but I'd like to include the attributes that define many to one relationships in the API documentation that is generated automatically.

This is the sample entity definition that I'm using:

@Entity()
export class Photo {
  @ApiModelPropertyOptional()    
  @PrimaryGeneratedColumn()
  readonly id: number;

  @ApiModelPropertyOptional()  
  @Column({ default: false })
  approved: boolean;

  @ApiModelProperty()  
  @Column()
  url: string;

  @ApiModelProperty()
  @ManyToOne(type => User, user => user.photos)
  owner: Photo;
}

Unfortunately the owner attribute doesn't like the ApiModelProperty decorator with an unhandled promise rejection.

Is it possible to add relations properties to documentation in Nest.js + Swagger and if it is so, what am I missing?

Thanks in advance


Solution

  • Maybe type of owner should be User?

    P. S. Have you tried to add metadata to ApiModelProperty?

    @ApiModelProperty({type: User})