zefchain / serde-reflection

Rust libraries and tools to help with interoperability and testing of serialization formats based on Serde.
Apache License 2.0
139 stars 26 forks source link

[Bug] serde_name::trace_name does't work on structs that use #[serde(flatten)] #35

Closed tsbernar closed 9 months ago

tsbernar commented 1 year ago

šŸ› Bug

Adding #[serde(flatten)] makes trace_name fail to find the name.

To reproduce

Code snippet to reproduce


use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct MyStruct {
    a: u64,
    b: HashMap<String, serde_json::Value>,
}
#[derive(Serialize, Deserialize)]
struct MyStructFlat {
    a: u64,
    #[serde(flatten)]
    b: HashMap<String, serde_json::Value>,
}

assert_eq!(serde_name::trace_name::<MyStruct>(), Some("MyStruct"));
assert_eq!(serde_name::trace_name::<MyStructFlat>(), None);

Stack trace/error message

n/a

Expected Behavior

Expected behavior is to find the struct name correctly.

System information

Please complete the following information:

Additional context

Add any other context about the problem here.

banool commented 1 year ago

Hey @tsbernar did you manage to fix this somehow?

tsbernar commented 1 year ago

No, Iā€™m still facing this issue

ma2bd commented 9 months ago

serde(flatten) is not supported.

If you work with JSON, maybe this library is closer to what you need? https://graham.cool/schemars/

If you work with both JSON and a binary format tracked by serde-reflection, you can branch the definition of serde::{Serialize,Deserialize} for your struct using is_human_readable, like this: https://github.com/linera-io/linera-protocol/blob/2b46124fff612b25cd8453ec8573ce48e37b214f/linera-version/src/serde_pretty/mod.rs#L20

ma2bd commented 9 months ago

@banool What change in serde 1.0.53 caused the issue?