Skip to content

Openapi-codegen doesn’t handle discriminated union properly #3369

Closed
oazapfts/oazapfts
#671

Description

@anisometropie

In our OpenAPI specification, we have a type Allowance that can be either a EngineeringAllowance or StandardAllowance.

we use the property allowance_type as discriminator.

    Allowance:
      oneOf:
        - $ref: "#/components/schemas/EngineeringAllowance"
        - $ref: "#/components/schemas/StandardAllowance"
      discriminator:
        propertyName: allowance_type

each allowance type correspond to one allowance_type string, as given by the enum

    StandardAllowance:
      properties:
        allowance_type:
          type: string
          enum: ["standard"]

The types generated by rtk-query/codegen-openapi isn’t correct,

the type Allowance is empty. The discrimated union never match the given patterns allowance_type: 'EngineeringAllowance'; or allowance_type: 'StandardAllowance'; as it’s the wrong strings.

the strings do not match with the value given in the openAPI definition, it simply uses the name of the Schema.

It should use the provided allowance_type strings given in the allowance_type enum for each schema.

export type EngineeringAllowance = {
  allowance_type?: 'engineering';
  distribution?: 'MARECO' | 'LINEAR';
  capacity_speed_limit?: number;
} & RangeAllowance;
export type StandardAllowance = {
  allowance_type?: 'standard';
  default_value?: AllowanceValue;
  ranges?: RangeAllowance[];
  distribution?: 'MARECO' | 'LINEAR';
  capacity_speed_limit?: number;
};
export type Allowance =
  | ({
      allowance_type: 'EngineeringAllowance';
    } & EngineeringAllowance)
  | ({
      allowance_type: 'StandardAllowance';
    } & StandardAllowance);
    Allowance:
      oneOf:
        - $ref: "#/components/schemas/EngineeringAllowance"
        - $ref: "#/components/schemas/StandardAllowance"
      discriminator:
        propertyName: allowance_type

    StandardAllowance:
      properties:
        allowance_type:
          type: string
          enum: ["standard"]
        default_value:
          $ref: "#/components/schemas/AllowanceValue"
        ranges:
          type: array
          items:
            $ref: "#/components/schemas/RangeAllowance"
        distribution:
          type: string
          enum: ["MARECO", "LINEAR"]
        capacity_speed_limit:
          type: number
          format: double

    EngineeringAllowance:
      allOf:
        - type: object
          properties:
            allowance_type:
              type: string
              enum: ["engineering"]
            distribution:
              type: string
              enum: ["MARECO", "LINEAR"]
            capacity_speed_limit:
              type: number
              format: double
        - $ref: "#/components/schemas/RangeAllowance"

Activity

  1. changed the title [-]Openapi-codegen doesn’t handle discriminated this union properly[/-] [+]Openapi-codegen doesn’t handle discriminated union properly[/+] on Apr 18, 2023
  2. henkkasoft commented on Nov 25, 2024

    @henkkasoft

    Hope that the fix for this issue is merged and released soon

  3. added
    RTKQ-Codegen
    on Jan 3, 2025
  4. mrpelz commented on Dec 5, 2025

    @mrpelz

    Any timeline for incorporating the changes from oazapfts/oazapfts#671 into a @rtk-query/codegen-openapi release, i.e. upgrading @oazapfts/runtime to v1.0.4?

  5. markerikson commented on Dec 5, 2025

    @markerikson
    Collaborator

    @mrpelz : our current dep is "@oazapfts/runtime": "^1.0.3", , so there shouldn't be any action required on our end for you to make use of that - it should get installed when you use the codegen as the latest version.

  6. markerikson commented on Dec 7, 2025

    @markerikson
    Collaborator

    Okay, did some more investigation.

    Looks like:

    • That @oazapfts/runtime dep I mentioned is just part of our own dev deps
    • But, the actual fix you linked is part of oazapfts itself and came out in v6.3.0
    • We updated our oazapfts dep to 6.3.0 a while ago
    • Per Add test for #3369 #5154 this should work as-is right now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    RTKQ-Codegen

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions