openapi-to-graphql icon indicating copy to clipboard operation
openapi-to-graphql copied to clipboard

Add support for processing discriminator property used with allOf property

Open mladjanm opened this issue 3 years ago • 5 comments

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

mladjanm avatar Mar 29 '21 09:03 mladjanm

@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.

Alan-Cha avatar Jun 01 '21 15:06 Alan-Cha

:+1:

Warxcell avatar Apr 07 '22 14:04 Warxcell

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.

vigie avatar Apr 25 '22 18:04 vigie

Do we have any plan on having this support?

TrivialNinja avatar Apr 29 '22 02:04 TrivialNinja

@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.

Alan-Cha avatar Apr 30 '22 04:04 Alan-Cha