Skip to content

[Bug]: Polymorphism with Alias leads to missing discriminator #6942

Open
@sfroehlichj

Description

@sfroehlichj

Describe the bug

Using Polymorphism and additional some kind of union (union or Alias) with all or a subset of these models leads to a missing discriminator mapping. The first Element is always missing in the discriminator of the parent Class.

In Detail:
If you need a some kind of union, which is only a subset of models, which are inherited from a base model and use this subset at least once somewhere else (e.g. another Model or as Response) the union itself is correctly created. But the Parent Model of the models looses the first model which is referenced in an alias or an union.

The linked playground show the example from the docs with an additional alias (MyPet). If MyPet is used somewhere Pet looses the first Element from the MyPet Alias (Cat). This leads to the following discriminator mapping (without Cat):

    Pet:
      type: object
      required:
        - kind
        - name
      properties:
        kind:
          $ref: '#/components/schemas/PetKind'
        name:
          type: string
        weight:
          type: number
          format: float
      discriminator:
        propertyName: kind
        mapping:
          dog: '#/components/schemas/Dog'

If MyPet would be defined reversed ( Dog | Cat ) only Cat would be rendered in mapping.
The same problem occurs if using union instead:

union MyPet {
  cat: Cat,
  dog: Dog,
}

Reproduction

Can be reproduced in Playground:

https://typespec.io/playground/?c=ZW51bSBQZXRLaW5kIHsKICBjYXQsCiAgZG9nLAp9CgpAZGlzY3JpbWluYXRvcigia2luZCIpCm1vZGVsxDjFNMQVOshGOwogIG5hbWU6IHN0cmluZ8QQd2VpZ2h0PzogZmxvYXQzMjsKfcdDQ2F0IGV4dGVuZHPWTy5jYXTEQ21lb3c6IGluzT5Eb2ffPmRv5QCBYmFya%2BoAkX0KCmFsaWFzIE15xDY95QCGfMROOwrHWlRlc8ZPdmFsOsYoxThvcCB0ZXN0KCk6xSM7&e=%40typespec%2Fopenapi3&options=%7B%7D

Checklist

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions