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

Add rename_all for union types #347

Closed
banool opened this issue Jul 26, 2022 · 1 comment
Closed

Add rename_all for union types #347

banool opened this issue Jul 26, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@banool
Copy link
Contributor

banool commented Jul 26, 2022

I've been making a lot of issues lately sorry hahahh.

Imagine I have a struct like this:

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Union)]
#[serde(tag = "type", rename_all = "snake_case")]
#[oai(one_of, discriminator_name = "type")]
pub enum Transaction {
    PendingTransaction(PendingTransaction),
    StateCheckpointTransaction(StateCheckpointTransaction),
}

As it is now, the type value in the response will for example be PendingTransaction. However, if you try to deserialize that response using this struct + serde, it fails, because it expects the type field to be in snake case.

I have to have this unfortunately since I'm migrating a codebase to Poem and I still need the old serialization to work.

Would it be possible to add rename_all to unions to uniformly rename the mapping?

So instead of this:

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Union)]
#[serde(tag = "type", rename_all = "snake_case")]
#[oai(one_of, discriminator_name = "type")]
pub enum Transaction {
    #[oai(mapping = "pending_transaction")]
    PendingTransaction(PendingTransaction),
    #[oai(mapping = "state_checkpoint_transaction")]
    StateCheckpointTransaction(StateCheckpointTransaction),
}

I just do this:

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Union)]
#[serde(tag = "type", rename_all = "snake_case")]
#[oai(one_of, discriminator_name = "type", rename_all = "snake_case")]
pub enum Transaction {
    PendingTransaction(PendingTransaction),
    StateCheckpointTransaction(StateCheckpointTransaction),
}

Thanks a lot!!

@banool banool added the enhancement New feature or request label Jul 26, 2022
sunli829 added a commit that referenced this issue Jul 27, 2022
@banool
Copy link
Contributor Author

banool commented Jul 27, 2022

Awesome thanks a lot!!

@banool banool closed this as completed Jul 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant