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

The error message on a struct with serde(rename = ...) includes the struct name without renaming #2859

Open
avandecreme opened this issue Nov 21, 2024 · 0 comments

Comments

@avandecreme
Copy link

Given the following code:

use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[serde(rename = "Renamed")]
struct Original {
    bar: String,
    baz: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        serde_json::from_str::<Original>("\"Bar\"").unwrap();
    }
}

The test fail with the following error:
Error("invalid type: string \"Bar\", expected struct Original", line: 1, column: 5)

I would expect the following:

Error("invalid type: string \"Bar\", expected struct Renamed", line: 1, column: 5)

Running cargo expand, I see that the rename macro has some effects:

                _serde::Deserializer::deserialize_struct(
                    __deserializer,
                    "Renamed",
                    FIELDS,
                    __Visitor {
                        marker: _serde::__private::PhantomData::<Original>,
                        lifetime: _serde::__private::PhantomData,
                    },
                )

but I also see

                    fn expecting(
                        &self,
                        __formatter: &mut _serde::__private::Formatter,
                    ) -> _serde::__private::fmt::Result {
                        _serde::__private::Formatter::write_str(
                            __formatter,
                            "struct Original",
                        )
                    }

So it seems it is the source of the issue.

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

No branches or pull requests

1 participant