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)] not working with deserialization #714

Closed
meettitan opened this issue Feb 21, 2024 · 3 comments
Closed

#[serde(flatten)] not working with deserialization #714

meettitan opened this issue Feb 21, 2024 · 3 comments
Labels
bug duplicate serde Issues related to mapping from Rust types to XML

Comments

@meettitan
Copy link

I am trying to factor out some commonly used keys in an xml api I am using, using #[serde(flatten)]. When using #[serde(flatten)] I am getting

Custom("invalid type: map, expected a string")

I would like to avoid manually flattening struct definitions if possible. Hopefully I am just misunderstanding.

Minimum reproducible example:

//git rev #88aa477
use quick_xml::de::from_str;
use serde::Deserialize;

#[derive(Debug, Deserialize, PartialEq)]
struct Outer {
    #[serde(flatten)]
    foo: Foo,
    #[serde(flatten)]
    bar: Bar,
}

#[derive(Debug, Deserialize, PartialEq)]
struct OuterFlat {
    a: String,
    b: String,
    c: u32,
    d: u32,
}

#[derive(Debug, Deserialize, PartialEq)]
struct Foo {
    a: String,
    b: String,
}

#[derive(Debug, Deserialize, PartialEq)]
struct Bar {
    c: u32,
    d: u32,
}

const XML: &str = "<Outer><a>foo</a><b>bar</b><c>1</c><d>2</d></Outer>";

#[test]
fn test() {
    let de = from_str::<Outer>(XML);
    let expected = Outer {
        foo: Foo {
            a: "foo".to_string(),
            b: "bar".to_string(),
        },
        bar: Bar {
            c: 1,
            d: 2,
        },
    };
    assert_eq!(expected, de.unwrap()) //Custom("invalid type: map, expected a string")
}

#[test]
fn test_flat() {
    let de = from_str::<OuterFlat>(XML);
    let expected = OuterFlat {
        a: "foo".to_string(),
        b: "bar".to_string(),
        c: 1,
        d: 2,
    };
    assert_eq!(expected, de.unwrap()) //success
}
@Mingun
Copy link
Collaborator

Mingun commented Feb 21, 2024

That is problem with serde flatten feature, that is not well-designed. Unfortunately, we cannot do anything at quick-xml side. The fix of serde-rs/serde#1183 is required.

@Mingun Mingun closed this as not planned Won't fix, can't repro, duplicate, stale Feb 21, 2024
@Mingun Mingun added bug duplicate serde Issues related to mapping from Rust types to XML labels Feb 21, 2024
@meettitan
Copy link
Author

@Mingun, Do you have a suggested workaround for factoring out multiple redundant keys? Maybe a macro that defines my structs and inherits all redundant keys?

@Mingun
Copy link
Collaborator

Mingun commented Feb 21, 2024

No, I have no ready-to-use recipes. A custom macro that defines actual struct and it's serialized representation is the best choice for the moment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug duplicate serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

No branches or pull requests

2 participants