Skip to content

Are discriminators inherited with allOf? #2165

@spacether

Description

@spacether

Are discriminator inheritable?

Given the following SpecA:

components:
  schemas:
    Pet:
      type: object
      required:
        - petType
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType
    Reptile:
      allOf:
        - $ref: '#/components/schemas/Pet'
    Lizard:
      allOf:
        - $ref: '#/components/schemas/Reptile'
        - type: object
          properties:
            lovesRocks:
              type: boolean

Do Reptile and Lizard contain the discriminator defined in Pet?

If discriminators are inheritable, then what is the correct interpretation of receiving payloads of type Reptile from the below specs?
SpecB (discriminator missing from Snake + Lizard)

components:
  schemas:
    Pet:
      type: object
      required:
        - petType
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType
    Reptile:
      allOf:
        - $ref: '#/components/schemas/Pet'
      oneOf:
        - $ref: '#/components/schemas/Lizard'
        - $ref: '#/components/schemas/Snake'
    Lizard:
      type: object
      properties:
        lovesRocks:
          type: boolean
    Snake:
      type: object
      properties:
        hasLegs:
          type: boolean

SpecC: (discriminator present in Snake + Lizard)

components:
  schemas:
    Pet:
      type: object
      required:
        - petType
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType
    Reptile:
      allOf:
        - $ref: '#/components/schemas/Pet'
      oneOf:
        - $ref: '#/components/schemas/Lizard'
        - $ref: '#/components/schemas/Snake'
    Lizard:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            lovesRocks:
              type: boolean
    Snake:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            hasLegs:
              type: boolean

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions