'OpenAPI / Swagger 3.0: Default discriminator value

How do you set a default discriminator for each child class?

For example, take this schema:

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

Code-generators for the above schema will create a client where the petType value must be explicitly set by the programmer. Why can't the Cat object have the petType be set to Cat by default?

I tried using default value to get it to work. However, the resulting code includes shadowed properties (same property on the child and parent).

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'

Removing the property petType from the parent also doesn't feel right since technically it's more of a property of the parent than the child.

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'

Do you have any solutions to this problem?



Solution 1:[1]

The default value is maybe implicit as soon as a mapping is specified ????

discriminator:
        propertyName: petType
        mapping:
          dog: Dog
          cat: Cat

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TacheDeChoco