Skip to content

Conversation

@michelhe
Copy link

Resolves #1530

This adds a new attribute #[serde(alias_all = "...")] to containers, which accepts the same rename rules as #[serde(rename_all = "...")]. When applied to a container, the alias will be added to all fields (or variants) similarly to how #[serde(alias = "...")] works on a single field/variant.

This attribute can be specified multiple times, with the use-case being accepting multiple case styles during de-serialization.

BTW this is my first contribution in this project, so I would appreciate if you take a closer look, and of course help with updating serde.rs documentation if you decide to accept this PR 😄

Comment on lines +1022 to +1026
#[serde(alias_all = "kebab-case")]
enum TestEnum {
VariantOne { field_one: i32 },
VariantTwo { field_two: String },
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this should be applied to variant names instead of field names inside variants? That is, convert to variant-one and variant-two

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your'e right, i made a typo in the test.
It actually works as you described, I pushed a fix to the tests.

michelhe added 2 commits July 24, 2025 21:39
This adds a new attribute `#[serde(alias_all = "...")]` to containers,
which accepts the same rename rules as `#[serde(rename_all = "...")]`.
When applied to a container, the alias will be added to all fields (or variants)
similarly to how `#[serde(alias = "...")]` works on a single field/variant.

This attribute can be specified multiple times, with the use-case being
accepting multiple case styles during de-serialization.

Resolves serde-rs#1530

Signed-off-by: Michel Heily <michelheily@gmail.com>
@michelhe
Copy link
Author

michelhe commented Jul 24, 2025

Just now realized that there was already an attempt at this in #1972 which was not accepted, missed that when I skimmed through the open issue.

I hope @dtolnay will have a change of heart here 🙏 this feature is really desired by many applications that use serde for loading configuration files.

I'm for once am tired of getting support tickets where the root cause is that the user used the wrong case style in their config files 😅

Our workaround is very not optimal - we first parse to serde_yaml::Value and then manually convert all keys to the correct style, and then convert back to yaml string, and deserialize to the Config struct from there.
This is dangerous because we lose type information when deserializing to serde_yaml::Value, which can lead to cases where strings in the orignal config files that happen to look like floating points (e.g a hexadecimal hash field) lose their meaning and cause deserialization errors when converted to the concrete Config struct.

@max-heller
Copy link

Our workaround is very not optimal - we first parse to serde_yaml::Value and then manually convert all keys to the correct style, and then convert back to yaml string, and deserialize to the Config struct from there.

FYI, serde_yaml::Value is itself a deserializer, so you should be able to get rid of the Value to string step and deserialize your config struct from the Value.

@michelhe michelhe requested a review from Mingun August 26, 2025 09:02
name: "VariantOne",
len: 1,
},
Token::Str("field-one"),
Copy link
Contributor

@Mingun Mingun Sep 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect, that alias_all for enums will influence only variant names. If you need to apply the same rules to struct variants, you should add attribute to struct variant. If I'm not wrong, it is how rename_all attribute work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Add alias_all attribute for containers similar to rename_all

3 participants