path-to-error icon indicating copy to clipboard operation
path-to-error copied to clipboard

Does not work with flatten

Open nkovacs opened this issue 4 years ago • 2 comments

#[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, ".");
}

nkovacs avatar Mar 29 '21 13:03 nkovacs

+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.

xamgore avatar Sep 01 '23 13:09 xamgore

@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 "."

ignaciostellino avatar Aug 19 '24 07:08 ignaciostellino