【问题标题】:Optional parameter in NestJS SwaggerNestJS Swagger 中的可选参数
【发布时间】:2021-02-10 20:57:30
【问题描述】:

对于 API 服务器,我有一组模型,例如

Recording <-> Artist 

在 TypeORM 中具有 ManyToMany 关系。需要在双方模型中定义关系。

在某些路线中,我只显示录音,而在某些路线中,我还使用 leftJoinAndSelect 显示艺术家的录音。例如。 /api/artists 和 /api/artists_with_recording。

但是,生成的文档总是在 Artists 中显示 Recordings。

是否有一些简单的方法可以修改 swagger 输出?

我可以使用 swagger 标记制作不同的模型对象,但在更多上下文中使用更多对象可能会变得非常混乱。

【问题讨论】:

    标签: nestjs nestjs-swagger


    【解决方案1】:

    经过更多搜索,我找到了解决方案。我可以使用 NestJS 中的 OmitType 函数为文档创建简单的临时类。

    https://docs.nestjs.com/openapi/mapped-types#omit

    所以对于路线 /api/artists 我是这样做的

    @Entity()
    class Artist {
     ...
    }
    
    class ArtistWithoutRecording extends OmitType(Artist, ['recording'] as const)
    
    

    在控制器文档中,我包含了 ArtistWithoutRecording 类型。

      @Get('artists')
      @ApiOperation({
        summary: 'Get all artists',
        description: 'Some description'
      })
      @ApiOkResponse({ type: ArtistWithoutRecording })
      async getArtists() {
        return this.artistDao.findMany()
      }
    
    

    【讨论】:

      猜你喜欢
      • 2021-07-09
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      • 2020-10-03
      • 2021-04-19
      • 2021-11-09
      相关资源
      最近更新 更多