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

#[serde(flatten)] breaks deserialization #114

Open
kyle-mccarthy opened this issue Sep 25, 2024 · 2 comments
Open

#[serde(flatten)] breaks deserialization #114

kyle-mccarthy opened this issue Sep 25, 2024 · 2 comments

Comments

@kyle-mccarthy
Copy link

kyle-mccarthy commented Sep 25, 2024

Describe the bug

If a struct contains a field annotated with #[serde(flatten)], it cannot be deserialized by sonic.

To Reproduce

Cargo.toml

[package]
name = "repro"
version = "0.1.0"
edition = "2021"


[dependencies]
sonic-rs = "0.3.13"
serde = { version = "1.0", features = ["derive"] }

main.rs

fn main() {
    #[derive(serde::Deserialize)]
    struct Repro {
        expected_field: i32,
        #[serde(flatten)]
        others: std::collections::HashMap<String, sonic_rs::Value>,
    }

    let json = r#"
    {
        "expected_field": 1,
        "unexpected_field": 2
    }
    "#;

    let _repro: Repro = sonic_rs::from_str(json).unwrap();
}

Expected behavior

Deserialization to succeed and for others to contain the "unexpected_field".

Actual behavior

thread 'main' panicked at src/main.rs:16:50:
called Result::unwrap() on an Err value: invalid type: newtype struct, expected a valid json at line 5 column 4

sonic-rs version:

latest (0.3.13)

Environment:

macos

@liuq19
Copy link
Collaborator

liuq19 commented Sep 25, 2024

thanks, we will investigate that

@liuq19
Copy link
Collaborator

liuq19 commented Sep 29, 2024

the problem is similar as serde-rs/serde#1183 and serde-rs/json#1099

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

2 participants