Skip to content

Improve oneOf / discriminator support #1613

@Redmega

Description

@Redmega

Description

Hi folks! Love the project, the client feels much nicer to use than the "official" openapi typescript-fetch generator.

I'm not sure if this is a feature request or a bug report, but I'm marking it as a feature request just in case it was left out intentionally. It feels that way since I get a warning when generating the client:

❗️ Transformers warning: schema {"items":[{"type":"string"},{"type":"boolean"},{"type":"number"}],"logicalOperator":"or"} is too complex and won't be currently processed. This will likely produce an incomplete transformer which is not what you want. Please open an issue if you'd like this improved https://github.com/hey-api/openapi-ts/issues

I have an openapi schema that looks like this:

        field_type:
          type: string
          enum:
            - string
            - number
            - boolean
        setting_name:
          type: string
        setting_value:
          oneOf:
            - type: string
            - type: number
            - type: boolean
          discriminator:
            propertyName: field_type
            mapping:
              string: string
              number: number
              boolean: boolean

I would expect the type to be something like this:

export type Setting = { setting_name: string } & (
  | { field_type: "string"; setting_value: string }
  | { field_type: "number"; setting_value: number }
  | { field_type: "boolean"; setting_value: boolean }
);

so that we can perform type narrowing on switch(setting.field_type) for example.

Instead I get this:

export type Setting = {
  field_type: "string" | "number" | "boolean";
  setting_name: string;
  setting_value: string | number | boolean;
};

which forces me to do a lot of explicit type checking of the setting_value in practice.

Is there any way we can get better support for oneOf's with discriminators and/or with primitive types?

The client code ends up looking the same if I leave out the discriminator, so it seems like it might not even be getting read/applied?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🔥Broken or incorrect behavior.feature 🚀Feature request.important 📌This issue is important and will be addressed when capacity allows.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions