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
Using flatten
and rename="$value"
on adjacent fields causes error
#326
Comments
@askepen, actually, your example is working since merging #490 after few changes:
#[test]
fn issue326() {
#[derive(Debug, Deserialize, PartialEq)]
struct ParentStruct {
#[serde(flatten)]
flat_struct: FlatStruct,
#[serde(rename = "ChildStruct")]
children: Vec<ChildStruct>,
}
#[derive(Debug, Deserialize, PartialEq)]
struct FlatStruct {
#[serde(rename = "@a")]
a: String,
#[serde(rename = "@b")]
b: String,
}
#[derive(Debug, Deserialize, PartialEq)]
struct ChildStruct {
#[serde(rename = "@c")]
c: String,
#[serde(rename = "@d")]
d: String,
}
assert_eq!(
from_str::<ParentStruct>(
r#"
<ParentStruct a="1" b="2">
<ChildStruct c="3" d="4"/>
<ChildStruct c="5" d="6"/>
</ParentStruct>
"#
)
.unwrap(),
ParentStruct {
flat_struct: FlatStruct {
a: "1".to_string(),
b: "2".to_string(),
},
children: vec![
ChildStruct {
c: "3".to_string(),
d: "4".to_string(),
},
ChildStruct {
c: "5".to_string(),
d: "6".to_string(),
},
],
}
);
} But I leave this bug open, because you spot a problem, that really exists: #[test]
fn issue326_enum() {
#[derive(Debug, Deserialize, PartialEq)]
struct ParentStruct {
#[serde(flatten)]
flat_struct: FlatStruct,
#[serde(rename = "$value")]
children: Vec<Enum>,
}
#[derive(Debug, Deserialize, PartialEq)]
struct FlatStruct {
#[serde(rename = "@a")]
a: String,
#[serde(rename = "@b")]
b: String,
}
#[derive(Debug, Deserialize, PartialEq)]
enum Enum {
ChildStruct {
#[serde(rename = "@c")]
c: String,
#[serde(rename = "@d")]
d: String,
}
}
// Result:
// thread 'issue326_enum' panicked at 'called `Result::unwrap()` on an `Err` value: Custom("missing field `$value`")',
assert_eq!(
from_str::<ParentStruct>(
r#"
<ParentStruct a="1" b="2">
<ChildStruct c="3" d="4"/>
<ChildStruct c="5" d="6"/>
</ParentStruct>
"#
)
.unwrap(),
ParentStruct {
flat_struct: FlatStruct {
a: "1".to_string(),
b: "2".to_string(),
},
children: vec![
Enum::ChildStruct {
c: "3".to_string(),
d: "4".to_string(),
},
Enum::ChildStruct {
c: "5".to_string(),
d: "6".to_string(),
},
],
}
);
} Problem disappeared after commenting out the |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have an issue similar to this issue in serde-xml-rs.
I have the following implementation, where i use
flatten
andrename="$value"
on fields in the same struct:I expect to get a struct similar to:
But I get the following error:
Using
flatten
orrename="$value"
in isolation works perfectly, but the combination of the two seems to cause the error.Is this a bug, or am I somehow using these macros wrong?
The text was updated successfully, but these errors were encountered: