Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve oneOf / discriminator support #1613

Open
Redmega opened this issue Jan 22, 2025 · 2 comments
Open

Improve oneOf / discriminator support #1613

Redmega opened this issue Jan 22, 2025 · 2 comments
Labels
bug 🔥 Something isn't working feature 🚀 New feature or request

Comments

@Redmega
Copy link

Redmega commented Jan 22, 2025

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?

@Redmega Redmega added the feature 🚀 New feature or request label Jan 22, 2025
@mrlubos
Copy link
Member

mrlubos commented Jan 22, 2025

Hi @Redmega, the first sentence is great to hear! The rest not so much 😀 there's an existing issue for discriminators, is yours the same? #1561

I see you've also added a log from transformers, is your issue about multiple problems?

@Redmega
Copy link
Author

Redmega commented Jan 22, 2025

I saw that but I wasn't sure if it was the same issue as my code generates the same with or without the discriminators, and it's about primitives instead of refs. I do not get the union code from #1561 in any configuration.

If you think they're related I can post there and close this one :)

@mrlubos mrlubos added the bug 🔥 Something isn't working label Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🔥 Something isn't working feature 🚀 New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants