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

Box<RawValue> and #[serde(flatten)] don't work together for deserialization #883

Closed
tbu- opened this issue May 1, 2022 · 2 comments
Closed

Comments

@tbu-
Copy link

tbu- commented May 1, 2022

It seems that if any struct field in the path to a Box<RawValue> is marked with #[serde(flatten)], deserialization always fails.

In the example below, Def can deserialize a certain JSON string, but the struct Abc containing a single #[serde(flatten)] Def field fails.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a7a182c5711834ea12a847374eb08de5

> cargo run
[…]
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("invalid type: newtype struct, expected any valid JSON value", line: 1, column: 11)', src/main.rs:19:57
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
#![allow(dead_code)]

use serde::Deserialize;
use std::error::Error;

#[derive(Deserialize)]
struct Abc {
    #[serde(flatten)]
    pub def: Def,
}

#[derive(Deserialize)]
struct Def {
    pub ghi: Box<serde_json::value::RawValue>,
}

fn main() -> Result<(), Box<dyn Error>> {
    let _: Def = serde_json::from_str(r#"{"ghi": {}}"#).unwrap();
    let _: Abc = serde_json::from_str(r#"{"ghi": {}}"#).unwrap();
    Ok(())
}
[package]
name = "abc"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0.126", features = ["derive"] }
serde_json = { version = "1.0.80", features = [
  "float_roundtrip",
  "preserve_order",
  "raw_value",
] }

Might have a similar root cause as #497, #545, #599 or #779.

@Thomasdezeeuw
Copy link

I think this is a duplicate of #1051.

@dtolnay dtolnay closed this as completed Dec 21, 2023
@tbu-
Copy link
Author

tbu- commented Dec 23, 2023

This is not a duplicate of #1051. Can this be re-opened?

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

3 participants