Coder Social home page Coder Social logo

Comments (11)

mrlubos avatar mrlubos commented on June 9, 2024

@v-morlock hey, are you able to replicate the same issue in @nicolas-chaulet/openapi-typescript-codegen?

from openapi-typescript-codegen.

v-morlock avatar v-morlock commented on June 9, 2024

Hi! Unfortunately yes

from openapi-typescript-codegen.

mrlubos avatar mrlubos commented on June 9, 2024

@v-morlock uh, I see what's happening. What's the output you'd expect? Roll all those options into a single enum, or multiple enums per each option?

from openapi-typescript-codegen.

v-morlock avatar v-morlock commented on June 9, 2024

Both would be fine to me - the value is representing a rust enum that looks like this:

#[derive(Serialize, Deserialize, Clone, schemars::JsonSchema, Eq, PartialEq, Debug)]
#[serde(tag = "type", content = "content")]
pub enum VendingItemState {
    Pending,
    Picked,
    Placed,
    Dispensed(chrono::DateTime<Utc>),
    Error(chrono::DateTime<Utc>, String),
}

so i think the direct mapping would be sth like below - one could make an argument that a separate enum isn't really necessary for enums with only a single value. But in practice, anything that gives the same end-result as the structure below would be fine to me. I don't think I'd ever really use the generated enum anyway. I think a single enum wouldn't work though because then it wouldn't be clear which options contain additional values and which don't.

export type VendingItemState = ({
    type: "Pending";
} | {
    type: "Picked";
} | {
    type: "Placed";
} | {
    type: "Dispensed;
    content: string;
} | {
    type: "Error";
    content: Array<any>;
});

from openapi-typescript-codegen.

v-morlock avatar v-morlock commented on June 9, 2024

Btw, do you have an idea why the "content" array is typed as "any"?

from openapi-typescript-codegen.

mrlubos avatar mrlubos commented on June 9, 2024

@v-morlock Got it, thanks! This is an interesting case, will have a look at it, but it will be available in the package I shared above as this one isn't really maintained

from openapi-typescript-codegen.

mrlubos avatar mrlubos commented on June 9, 2024

@v-morlock please try with the latest version of @nicolas-chaulet/openapi-typescript-codegen 🎉

That should give you exactly what you need

export type ModelWithOneOfEnum = {
    foo: 'Bar';
} | {
    foo: 'Baz';
} | {
    foo: 'Qux';
} | {
    content: string;
    foo: 'Quux';
} | {
    content: [string | string];
    foo: 'Corge';
};

from openapi-typescript-codegen.

mrlubos avatar mrlubos commented on June 9, 2024

@v-morlock I updated @nicolas-chaulet/openapi-typescript-codegen so it should now work out of the box. It is a huge change set, so I'd love if you could upgrade and let me know if anything breaks!

from openapi-typescript-codegen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.