0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[備忘録]nestjs/swaggerにてApiPropertyのtypeで指定したクラスのモデルがうまく生成されない

Posted at

状況

↓のように@ApiPropertyOptional({ type: User })で指定したところ、

project.model.ts
export class Project extends Model {
  ・・・
  @ApiPropertyOptional({ type: [User] })
  @BelongsToMany(() => User, () => ProjectMember)
  members?: User[];

SwaggerUIでProjectモデルを展開すると、↓のようにmembersにUserが反映されずstringとなってしまいました

image.png

typeにクラスでなくGetter関数を指定する

こちらで解決策が提示されていました
循環参照により型がうまいこと読めていなかったようです。

type: () => [User]のように無名関数で型を返すようにしてあげるだけです

project.model.ts
  @ApiPropertyOptional({ type: () => [User] })
  @BelongsToMany(() => User, () => ProjectMember)
  members?: User[];

SwaggerUIで再度確認すると、無事にUserクラスのモデルが反映されました

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?