Swagger 3参考。顶级组件中的组件

时间:2018-02-04 17:29:15

标签: swagger

VehicleBaseAttributes:
  type: object
  properties:
    id:
      type: integer
      format: int64
    model:
      type: string
    doors:
      type: integer
VehicleExtendedAttributes:
  type: object
  properties:
    $ref: '#/components/schemas/VehicleBaseAttributes'
    pricehistory:
      type: array
      items:
        title: PriceHistory
        type: object
        properties:
          priceWOT:
            type: number
          taxes:
            type: number
          additionalCosts:
            type: number
          price:
            type: number
          createdByUserId:
            type: string
          createdDate:
            type: string
    dealerHistory:
      type: array
      items:
        title: DealerHistory
        type: object
        properties:
          name:
            type: string
          phone:
            type: string
          createdByUserId:
            type: string
          createdDate:
            type: string

在上面的示例中,我打算定义一组基本属性,然后提供使用基本版本的扩展版本。

显然,在VehicleExtendedAttributes中,我不想将VehicleBaseAttributes嵌套在一个单独的属性中,而是将它们添加到顶层,从而产生如下输出:

  type: object
  properties:
    id:
      type: integer
      format: int64
    model:
      type: string
    doors:
      type: integer
    pricehistory:
      type: array
      items:
        title: PriceHistory
        type: object
        properties:
          priceWOT:
            type: number
          taxes:
            type: number
          additionalCosts:
            type: number
          price:
            type: number
          createdByUserId:
            type: string
          createdDate:
            type: string
    dealerHistory:
      type: array
      items:
        title: DealerHistory
        type: object
        properties:
          name:
            type: string
          phone:
            type: string
          createdByUserId:
            type: string
          createdDate:
            type: string

问题是导致错误:

Schema error at components.schemas['VehicleExtendedAttributes']
should have required property '$ref'
missingProperty: $ref
Jump to line 342
Schema error at components.schemas['VehicleExtendedAttributes']
should match exactly one schema in oneOf
Jump to line 342
Schema error at components.schemas['VehicleExtendedAttributes'].properties['$ref']
should be object
Jump to line 345

1 个答案:

答案 0 :(得分:2)

您需要allOf来进行模型合成。 allOf的值是一起构成单个模式的子模式列表($ref erenced或inline)。

VehicleExtendedAttributes:
  allOf:
    - $ref: '#/components/schemas/VehicleBaseAttributes'
    - type: object
      properties:
        pricehistory:
          type: array
          ...
        dealerHistory:
          type: array
          ...

更多信息: