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

Does not work with flatten #9

Open
nkovacs opened this issue Mar 29, 2021 · 2 comments
Open

Does not work with flatten #9

nkovacs opened this issue Mar 29, 2021 · 2 comments

Comments

@nkovacs
Copy link

nkovacs commented Mar 29, 2021

#[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
Copy link

xamgore commented Sep 1, 2023

+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 serde-rs/serde#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
Copy link

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

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

3 participants