oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Why are discriminators not supported for AllOf ?

Open LelouBil opened this issue 3 years ago • 4 comments

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.

LelouBil avatar Jul 07 '22 08:07 LelouBil

+1

Antonboom avatar Aug 05 '22 20:08 Antonboom

+1

ssamsonov-IS360 avatar Oct 20 '22 17:10 ssamsonov-IS360

+1

re-cheid avatar Mar 23 '23 10:03 re-cheid

+1

MateusFrFreitas avatar Mar 29 '23 19:03 MateusFrFreitas