OpenAPI / Swagger 3.0:默认鉴别器值

Cha*_*aos 6 polymorphism swagger openapi swagger-codegen openapi-generator

您如何为每个子类设置默认鉴别器?

例如,采用以下模式:

components:
  schemas:
    Pet:
      type: object
      required:
      - petType
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType
    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
Run Code Online (Sandbox Code Playgroud)

上述模式的代码生成器将创建一个客户端,其中的petType值必须由程序员显式设置。为什么不能默认设置Cat对象?petTypeCat

我尝试使用defaultvalue 让它工作。但是,生成的代码包含隐藏的属性(子级和父级上的相同属性)。

components:
  schemas:
    Pet:
      type: object
      required:
      - petType
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType
    Cat:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Cat`
        properties:
          name:
            type: string
          petType:
            type: string
            default: 'Cat'
    Dog:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Dog`
        properties:
          bark:
            type: string
          petType:
            type: string
            default: 'Dog'
Run Code Online (Sandbox Code Playgroud)

petType从父母那里移除财产也感觉不对,因为从技术上讲,它更像是父母的财产而不是孩子的财产。

components:
  schemas:
    Pet:
      type: object
      required:
      - petType
      discriminator:
        propertyName: petType
    Cat:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Cat`
        properties:
          name:
            type: string
          petType:
            type: string
            default: 'Cat'
    Dog:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Dog`
        properties:
          bark:
            type: string
          petType:
            type: string
            default: 'Dog'
Run Code Online (Sandbox Code Playgroud)

你有解决这个问题的方法吗?

Tac*_*oco 0

一旦mapping指定 a ,默认值可能是隐式的 ????

discriminator:
        propertyName: petType
        mapping:
          dog: Dog
          cat: Cat
Run Code Online (Sandbox Code Playgroud)