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

serde rename with '@' as first letter lead to broken serialization #532

Open
apatrushev opened this issue Mar 21, 2024 · 3 comments
Open

Comments

@apatrushev
Copy link

apatrushev commented Mar 21, 2024

This code breaks with InvalidIdentifier("@sender") error:

use serde::{Deserialize, Serialize};


#[derive(Debug, Deserialize, Serialize)]
struct Event {
    #[serde(rename = "@sender")]
    sender: String,
}


fn main() {
    let event = Event { sender: "test".to_string() };
    println!("{}", ron::to_string(&event).unwrap())
}

Result:

$ cargo run
   Compiling ron-bug v0.1.0 ([stripped])
    Finished dev [unoptimized + debuginfo] target(s) in 0.52s
     Running `target/debug/ron-bug`
thread 'main' panicked at src/main.rs:13:43:
called `Result::unwrap()` on an `Err` value: InvalidIdentifier("@sender")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Expected result:

(r#@sender:"test")

Looks like it is a regression: everything worked as expected in 0.7.1 but stopped working in 0.8.0

@apatrushev
Copy link
Author

Additional notes: deserialization did not work in 0.7.1 (and early versions) too.

@juntyr
Copy link
Member

juntyr commented Mar 21, 2024

I'll start with my technical answer:

This breakage is expected. Before v0.8.0, RON's grammar (https://github.com/ron-rs/ron/blob/master/docs/grammar.md#identifier) did not allow @sender as an identifier (just like it's not a valid Rust identifier), it was just never checked. In v0.8.0 such checks were added since you could otherwise do funny stuffy with identifiers, e.g. pack RON inside them which then cannot roundtrip. Now RON checks identifier at serialisation and deserialisation to ensure only valid ones can be used.

How big of a problem is this more strict parsing? If the breakage is with a large userbase we could work around it (e.g. by adding an unchecked identifier feature which turns off the checks but leaves you open to breaking serialise-deserialise roundtrips), otherwise I'd rather stick to the checks since they stick to the grammar that existed already before.

@apatrushev
Copy link
Author

apatrushev commented Mar 21, 2024

Thank you for detailed answer. I just noticed this inconsistence because I used insta+ron (insta pins 0.7.1 and use it only as serializer) and it worked fine till I tried to deserialize the data to use in tests (not only in assertion/check). So I decided that it was a regression. I personally opted out from serde/rename to serde/alias in my application so it is not a biggest problem for me and hopefully this change can be used in most cases.

I think that it will be enough to mention serde/rename limitation in the list of other limitations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants