Skip to content

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

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    RTKQ-CodegenIssues related to the @rtk-query/codegen-openapi package.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions