SWAGGER 2 从同一基本对象继承请求和响应对象

SWAGGER 2 Inheritance for Request and Response Objects from same Base Object

在我使用 Swagger 2.0 设计的 Spring API 中,我尝试使用 swagger 创建继承。我想创建一个基础对象,它将具有 Request 和 Response 对象的共同属性。我试着像下面的例子那样做:

CategoryResponse:
  allOf:
    - $ref: '#/definitions/Category'
    - type: object
      properties:
        id:
          type: integer
          example: '1'          
CategoryRequest:
  type: object
  allOf:
    - $ref: '#/definitions/Category'         
Category:
  discriminator: nameCategory
  type: object
  properties:
    nameCategory:
      type: string
      example: Games

问题是我在尝试 POST 或 PUT 新的 CategoryRequest 对象时收到错误请求错误。它甚至没有到达 API 控制器,所以我猜问题可能出在上面的模型定义中。我尝试了很多变体,但其中 none 有效。但是,当我尝试获取类别列表或通过 ID 获取一个类别时,我能够这样做(我的 CategoryResponse 正在运行并且扩展类别很好)。

是否有人知道使用公共基础模型的继承来创建此结构的正确方法,包括 Request 和 Response 对象?

提前致谢!

id 看起来像一个自动生成的只读 属性。在这种情况下,您不需要继承 - 您可以使用单个 Category 模式并将 id 标记为 readOnly: true.

Category:
  type: object
  properties:
    nameCategory:
      type: string
      example: Games
    id:
      type: integer
      readOnly: true  # <-----
      example: 1

来自OpenAPI Specification

readOnly

Declares the property as "read only". This means that it MAY be sent as part of a response but MUST NOT be sent as part of the request.