Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for processing discriminator property used with allOf property #383

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mladjanm
Copy link

@mladjanm mladjanm commented Mar 29, 2021

This pull request introduces support for handling polymorphic schemas in the Open API 3 Specification responses that use the discriminator property with the allOf property to describe polymorphism.

Here is an example situation that this pull request resolves:

components:
  schemas:
    Pet:
      type: object
      discriminator:
        propertyName: petType
      properties:
        name:
          type: string
        petType:
          type: string
          enum:
          - Cat
          - Dog
      required:
      - name
      - petType
    Cat:
      description: A representation of a cat
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        properties:
          huntingSkill:
            type: string
            description: The measured skill for hunting
            enum:
            - clueless
            - lazy
            - adventurous
            - aggressive
        required:
        - huntingSkill
    Dog:
      description: A representation of a dog
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        properties:
          packSize:
            type: integer
            format: int32
            description: the size of the pack the dog is from
            default: 0
            minimum: 0
        required:
        - packSize

In this example an union called Pet would be created and it would be composed of two types: Dog and Cat.

Fixes #361

…perty

Co-authored-by: Ilija Miljkovic <ilija.miljkovic@asseco-see.rs>
Signed-off-by: Mlađan Mihajlović <32955957+mladjanm@users.noreply.github.com>
@mladjanm mladjanm force-pushed the master branch 3 times, most recently from d653ffb to ea9469b Compare March 29, 2021 10:05
Signed-off-by: Mlađan Mihajlović <32955957+mladjanm@users.noreply.github.com>
@Alan-Cha
Copy link
Collaborator

Alan-Cha commented Jun 1, 2021

@mladjanm Wow! This seems like an awesome change! Thank you so much for this PR! I will take a look at this and try to get it in asap.

@Warxcell
Copy link

Warxcell commented Apr 7, 2022

👍

@vigie
Copy link

vigie commented Apr 25, 2022

Wouldn't a more natural GQL implementation of this be to turn Pet into an interface and have Cat and Dog be two types that implement Pet?

By using union, we're losing some of the information that allOf is giving us.

@TrivialNinja
Copy link

Do we have any plan on having this support?

@Alan-Cha
Copy link
Collaborator

@vigie Yes, on the surface level, that makes sense to me. We just have not implemented this kind of feature. I suppose if all member schemas reference one or more other schemas, then we can create interfaces from all those referenced schemas. I just wonder if there is any kind overlap between union and interface that we need to keep in mind.

@TrivialNinja I’m planning on reviewing new and old PRs again this week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use discriminator and mapping object for resolveType in GraphQL union types
5 participants