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

Wrong serialization of Vec<Enum> #184

Open
aknarts opened this issue Sep 9, 2022 · 2 comments
Open

Wrong serialization of Vec<Enum> #184

aknarts opened this issue Sep 9, 2022 · 2 comments
Assignees

Comments

@aknarts
Copy link

aknarts commented Sep 9, 2022

I am trying to serialize this

#[derive(Serialize)]
pub struct Test {
    pub val: Vec<TestEnum>,
}

#[derive(Serialize)]
pub enum TestEnum {
    #[serde(rename_all = "camelCase")]
    FirstEnum {
        content: String
    },
    #[serde(rename_all = "camelCase")]
    SecondEnum {
        cont: String
    },
}

let mut test = Test { val: vec![] };
test.val.push(TestEnum::FirstEnum { content: "Test1".to_string() });
test.val.push(TestEnum::SecondEnum { cont: "Test2".to_string() });
println!("{}", serde_xml_rs::to_string(&test).unwrap());

Sadly this ends up being

<?xml version="1.0" encoding="UTF-8"?>
<Test>
    <val>
        <FirstEnum>
            <content>Test1</content>
        </FirstEnum>
    </val>
    <content>
        <SecondEnum>
            <cont>Test2</cont>
        </SecondEnum>
    </content>
</Test>

I think the expected output would be

<?xml version="1.0" encoding="UTF-8"?>
<Test>
    <val>
        <FirstEnum>
            <content>Test1</content>
        </FirstEnum>
        <SecondEnum>
            <cont>Test2</cont>
        </SecondEnum>
    </val>
</Test>

Or am I doing something wrong here?

@aknarts
Copy link
Author

aknarts commented Sep 9, 2022

I did identify where the wrong tag comes in https://github.com/RReverser/serde-xml-rs/blob/master/src/ser/seq.rs#L26-L29 which closes the </val> after the first enum and then the next call to build_start_tag() opens <content> because that is the curent_tag

And that is it from me, somebody way more knowledgeable than me in this matter has got to figure this one out. Trying to look into how serde_json does it makes my head hurt.

@aknarts
Copy link
Author

aknarts commented Sep 9, 2022

Not sure if this helps

[2022-09-09T20:17:47Z DEBUG serde_xml_rs::ser] Struct Test
[2022-09-09T20:17:47Z DEBUG serde_xml_rs::ser::map] field val
[2022-09-09T20:17:47Z DEBUG serde_xml_rs::ser] Sequence
[2022-09-09T20:17:47Z DEBUG serde_xml_rs::ser] Struct variant FirstEnum
[2022-09-09T20:17:47Z DEBUG serde_xml_rs::ser::map] field content
[2022-09-09T20:17:47Z DEBUG serde_xml_rs::ser::map] end field
[2022-09-09T20:17:47Z DEBUG serde_xml_rs::ser] Struct variant SecondEnum
[2022-09-09T20:17:47Z DEBUG serde_xml_rs::ser::map] field cont
[2022-09-09T20:17:47Z DEBUG serde_xml_rs::ser::map] end field
[2022-09-09T20:17:47Z DEBUG serde_xml_rs::ser::map] end field

@punkstarman punkstarman self-assigned this Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants