【问题标题】:Can I have a polymorphic response in Nestjs OpenAPI?我可以在 Nestjs OpenAPI 中进行多态响应吗?
【发布时间】:2020-07-08 09:29:42
【问题描述】:

使用 Nestjs OpenAPI 模块,我可以使用如下装饰器指定响应类型:

@Controller('app')
class MyController {
  @Get()
  @ApiResponse({ status: 200, description: 'Success', type: MyDto })
  getThing() {
    // code here
  }
}

但是,文档没有指定如何使用多态响应类型(或在 OpenAPI 术语中为 oneOf 响应类型)。例如,在下面的示例中,我希望我的响应类型为 oneOf: [MyDto, MyOtherDto]

@Controller('app')
class MyController {
  @Get()
  @ApiResponse({ status: 200, description: 'Success', type: MyDto }) // What do do for type here?
  getThing(): MyDto | MyOtherDto {
    // code here
  }
}

我该怎么做?

【问题讨论】:

    标签: nestjs openapi


    【解决方案1】:

    好的,所以不确定这是否是最好/最简单的方法,但可以为 @ApiResponse() 装饰器提供“原始模式定义”,如下所示:

    @Controller('app')
    class MyController {
      @Get()
      @ApiResponse({
        status: 200,
        description: 'Success',
        schema: {
          oneOf: [
            { $ref: getSchemaPath(MyDto) },
            { $ref: getSchemaPath(MyOtherDto) },
          ]
        }
      })
      getThing(): MyDto | MyOtherDto {
        // code here
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-08
      • 2019-09-19
      • 1970-01-01
      • 2015-05-01
      • 2018-09-22
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      相关资源
      最近更新 更多