dtolnay / path-to-error

Find out path at which a deserialization error occurred
Apache License 2.0
320 stars 12 forks source link

Does not work with flatten #9

Open nkovacs opened 3 years ago

nkovacs commented 3 years ago
#[test]
fn test_flattened_struct() {
    #[derive(Deserialize, Debug)]
    struct Package {
        name: String,
        #[serde(flatten)]
        dependency: Dependency,
    }

    #[derive(Deserialize, Debug)]
    struct Dependency {
        version: String,
    }

    let j = r#"{
        "name": "demo",
        "version": 1
    }"#;

    // this doesn't work
    test::<Package>(j, "version");
    // this is the current result
    // test::<Package>(j, ".");
}
xamgore commented 1 year ago

+1 to this. Also, the returned error points to the end of file. Here 220 is the last }:

Error: invalid type: null, expected a string at line 220 column 1

It seems it's not the issue of path-to-error itself, as pure deserialisation returns the same result.

@jonasbb This is a duplicate of https://github.com/serde-rs/serde/issues/1183. With flatten the data gets deserialized into an intermediate buffer, advancing the location in the yaml file, but then fails to deserialize the buffer data into the struct.

ignaciostellino commented 2 months ago

@dtolnay This is really true I create an example using a struct that has flatten

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Default)]
pub struct TestStruct {
    #[serde(flatten)]
    test: Test<Bus>,
    id: Option<String>,
}

And when I use

let result: Result<TestStruct, _> = serde_path_to_error::deserialize(
                &mut serde_json::Deserializer::from_str(line.as_str()),
            );
             match result {
                Ok(response) => {
                    print!("{:?}", response);
                }
                Err(err) => {
                    panic!(
                        "Deserialization error at path: {}\nError: {}",
                        err.path(),
                        err
                    );
                }
            }

The path is "."