OpenAPI 3.0 - 如何将一个数组中的项目合并/展开到另一个数组中?

问题描述 投票:0回答:1

在 OpenAPI 3.0 中,如果我有两个组件,它们是类似类型的数组,如下所示:

components:
  schemas:
    AquaticMammals:
      type: array
      items:
        anyOf:
          - $ref: '#/components/schemas/Dolphin'
          - $ref: '#/components/schemas/Otter'
          - $ref: '#/components/schemas/Seal'
          - $ref: '#/components/schemas/Beaver'
    LandMammals:
      type: array
      items:
        anyOf:
          - $ref: '#/components/schemas/Elephant'
          - $ref: '#/components/schemas/Bear'
          - $ref: '#/components/schemas/Monkey'
          - $ref: '#/components/schemas/Camel'

有没有一种很好的方法来创建一个包含两个列表中所有元素的数组,而不必在第三个组件中维护两个列表的副本?

我尝试了

anyOf
allOf
的一些变体,包括这个,但我最终得到的是一个数组的数组,而不是包含两个数组元素的单个数组。

components:
  schemas:
    Mammals:
      type: array
      items:
        anyOf:
          - allOf:
            - $ref: '#/components/schemas/AquaticMammals'
          - allOf:
            - $ref: '#/components/schemas/LandMammalsMammals'

本质上我正在寻找的是 OpenAPI 3.0 相当于 javascript 扩展运算符。

arrays swagger openapi
1个回答
0
投票

这应该有效:

components:
  schemas:
    Mammals:
      type: array
      items:
        anyOf:
          - $ref: '#/components/schemas/AquaticMammals/items'
          - $ref: '#/components/schemas/LandMammals/items'

或者,您可以为初始

anyOf
列表创建命名模式,这将使引用更容易。

components:
  schemas:
    AquaticMammal:
      anyOf:
       - $ref: '#/components/schemas/Dolphin'
       - $ref: '#/components/schemas/Otter'
       - $ref: '#/components/schemas/Seal'
       - $ref: '#/components/schemas/Beaver'
    AquaticMammals:
      type: array
      items:
        $ref: '#/components/schemas/AquaticMammal'

    LandMammal:
      anyOf:
        - $ref: '#/components/schemas/Elephant'
        - $ref: '#/components/schemas/Bear'
        - $ref: '#/components/schemas/Monkey'
        - $ref: '#/components/schemas/Camel'
    LandMammals:
      type: array
      items:
        $ref: '#/components/schemas/LandMammal'

    Mammals:
      type: array
      items:
        anyOf:
          - $ref: '#/components/schemas/AquaticMammal'
          - $ref: '#/components/schemas/LandMammal'
© www.soinside.com 2019 - 2024. All rights reserved.