如何在 swagger codegen 中处理多个 response/return 类型(204 为空,400 为非空等)?

How to handle multiple response/return types (empty for 204, non-empty for 400 etc) in swagger codegen?

我正在使用 openapi 版本 3.0.2。

我有以下描述我的回复的规范:

responses:
    '201':
        description:  
            Created
    '400':
        description: Bad request
        content:
            application/json:
                schema:
                    $ref: '#/components/schemas/Error'
    '404':
        description: The resource could not be found.
    '500':
        description: The request failed due to an unexpected server error.

对于大多数响应代码,我不return任何响应主体,但对于 400 响应 代码,我想 return 错误对象:

Error:
    type: object
    properties:
        code:
            type: string
        message:
            type: string
    required:
        - code
        - message

当我为此端点生成 Java 服务器代码时,方法的 return 类型 是 ResponseEntity<Void>,意味着我不能 return 错误对象?

似乎与这些问题类似:

https://groups.google.com/forum/#!topic/swagger-swaggersocket/ygVjA2m5gY0

https://github.com/swagger-api/swagger-codegen/issues/7743

https://github.com/swagger-api/swagger-codegen/issues/4398

我不知道这个问题是否已经修复,或者是否有任何解决方法?

我使用 here 描述的以下方法来解决此问题:

@Override
public ResponseEntity<Void> deleteThing(...)
{
        try {
                myService.deleteThing(...);
                return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (MyException e) {
                    throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage());
        }
        catch (MyOtherException e) {
                    throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
        }
}