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

How to implement Union with value of the enum is of the same type #742

Open
newholder opened this issue Feb 6, 2024 · 0 comments
Open
Labels
question Further information is requested

Comments

@newholder
Copy link

So, I have this struct that contains this enum

#[derive(Debug, Serialize, Deserialize, Object)]
pub struct Assay {
    pub id: ValidId,
    pub value: AssayValue,
}

#[derive(Debug, Serialize, Deserialize, Union)]
pub enum AssayValue {
    Ordinal(String),
    Numerical(String),
}

when i return it as a response type, it will flatten it to just a string. Here's an example response:

from this

Assay {
    id: ValidId(
        "TB_ACID_FAST",
    ),
    value: Numerical(
        "Scanty",
    ),
}

to this:

{
        "id": "TB_ACID_FAST",
        "value": "Scanty"
}

so, when I post it as a payload to endpoint that accepts it and then debugprint the resulting struct, it doesn't respect the enum. Here's the debug print:

    Assay {
        id: ValidId(
            "TB_ACID_FAST",
        ),
        value: Ordinal(
            "Scanty",
        ),
    }

my workaround is currently just making two different struct that describes the same thing, one for value, one just for scheme and implement ways to convert between two of them, with the schema one containing an intermediate struct with fieldname that match enum variant. Is there a way to make it respect the enum?

@newholder newholder added the question Further information is requested label Feb 6, 2024
@newholder newholder changed the title How to implement Union with valu of the enum is of the same type How to implement Union with value of the enum is of the same type Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant