多级鉴别器OpenAPI

时间:2018-08-13 23:14:07

标签: swagger openapi

问候,感谢您的宝贵时间。 我正在使用OpenAPI做一些ReDoc文档,但是找不到正确进行两级继承的方法。这是我所拥有的:

components:
  schemas:
    Pet:
      type: object
      required:
      - pet_type
      properties:
        pet_type:
          type: string
      discriminator:
        propertyName: pet_type
        mapping:
          dogs: Dog
          cats: Cat

    Cat:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Cat`
        properties:
          name:
            type: string
    Dog:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Dog`
        properties:
          bark:
            type: string
          size:
            type: string
        discriminator:
          propertyName: size
          mapping:
             large: '#/components/schemas/LargeDog'
             medium: '#/components/schemas/MediumDog'
    SmallDogs:
      allOf:
      - $ref: '#/components/schemas/Dog'
      - type: object
    LargeDogs:
      allOf:
      - $ref: '#/components/schemas/Dog'
      - type: object

谢谢。

1 个答案:

答案 0 :(得分:0)

您所做的事情很奇怪,或者您的示例未能完全揭示问题的实质。对于狗大小,您无需使用鉴别器。下面的示例

components:
  schemas:
    Pet:
      type: object
      required:
      - pet_type
      properties:
        pet_type:
          type: string
      discriminator:
        propertyName: pet_type
        mapping:
          dogs: Dog
          cats: Cat
    Cat:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Cat`
        properties:
          name:
            type: string
    Dog:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Dog`
        properties:
          bark:
            type: string
          size:
            type: string
            enum:
            - large
            - medium