Skip to content

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

@avandecreme

Description

@avandecreme

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions