Skip to content

Why are discriminators not supported for AllOf ? #666

@LelouBil

Description

@LelouBil

Hello,
I have an OpenAPI Schema that uses inheritance like this.

ViewConnectionNoRelationship:
      type: object
      additionalProperties: false
      allOf:
        - $ref: "#/components/schemas/ViewConnectionBase"
        - type: object
          additionalProperties: false
          properties:
            name:
              type: string
            documentation:
              type: string
          required:
            - name
            - documentation

ViewConnectionRelationship:
      type: object
      additionalProperties: false
      allOf:
        - $ref: "#/components/schemas/ViewConnectionBase"
        - type: object
          additionalProperties: false
          properties:
            relationship_id:
              type: string
          required:
            - relationship_id

With two schemas inheriting from a top schema.

ViewConnectionBase also has a discriminator like this :

discriminator:
        propertyName: connection_type
        mapping:
          connection: "#/components/schemas/ViewConnectionNoRelationship"
          relationship: "#/components/schemas/ViewConnectionRelationship"

I searched online and found this is how one should use discriminators for inheritance (based on https://swagger.io/specification/#discriminator-object, the AllOf example).

And for half of my use-case, generating a java client library, it works and generates correctly the inheritance ONLY if I use the discriminator like this. However, for oapi-codegen it doesn't work, and just refuses to merge schemas with discriminators involved, why so ?

https://github.com/deepmap/oapi-codegen/blob/b11a594aa540a0c4ebd25f247228e226bed15f9f/pkg/codegen/merge_schemas.go#L189-L192

At the very least, the library could just ignore the discriminator, since it can't do polymorphism via inheritance in Golang. If I correctly understand what I found, at first the library was using struct embeddings, but it was then later changed to schema merging.

As of right now (and from what I found), the discriminator on AllOf and OneOf/AnyOf is more of a "hint" for the deserializer to find the correct schema on the first try, so simply ignoring it for now in the case of AllOf should not be a problem.

Currently, the library doesn't even read the discriminator for OneOf and just gives it back to the developer to use, along with methods that error if the JSON schema is not matching, so I don't see why it couldn't just be ignored for AllOf.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:*of`oneOf`/`anyOf`/`allOf`

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions