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

Serializing $value enum variant doesn't work #429

Closed
tyilo opened this issue Jul 19, 2022 · 1 comment · Fixed by #490
Closed

Serializing $value enum variant doesn't work #429

tyilo opened this issue Jul 19, 2022 · 1 comment · Fixed by #490
Assignees
Labels
bug help wanted serde Issues related to mapping from Rust types to XML

Comments

@tyilo
Copy link

tyilo commented Jul 19, 2022

Cargo.toml:

[package]
name = "quick-xml-test"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0.139", features = ["derive"] }
quick-xml = { version = "0.23.0", features = ["serialize" ] }

src/main.rs:

use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
enum State {
    A,
    B,
    C,
}

#[derive(Debug, Deserialize, Serialize)]
struct StateOuter {
    #[serde(rename = "$value")]
    state: State,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Root {
    state: StateOuter,
}

impl Root {
    pub fn new() -> Self {
        Self { state: StateOuter { state: State::B } }
    }
}

fn main() {
    let xml = "<root><state>B</state></root>";
    println!("Deserialized: {:?}", quick_xml::de::from_str::<Root>(&xml));

    let root = Root::new();
    println!("Serialized: {:?}", quick_xml::se::to_string(&root));
}

Output:

Deserialized: Ok(Root { state: StateOuter { state: B } })
Serialized: Ok("<Root><state><B/></state></Root>")

Expected output:

Deserialized: Ok(Root { state: StateOuter { state: B } })
Serialized: Ok("<Root><state>B</state></Root>")

As you can see deserialization works, but serialization does not.

@Mingun
Copy link
Collaborator

Mingun commented Jul 19, 2022

Efforts have not yet been put into the correct representation of enum, so many obvious (in fact, not all are obvious) things do not work. First of all, it's need to write consistent, composable rules to represent various serde enum representations. I've started this work in my doc branch, but have not yet time to finish it. You can help by working through this aspect of, if you wish.

@Mingun Mingun added bug help wanted serde Issues related to mapping from Rust types to XML labels Jul 19, 2022
@Mingun Mingun self-assigned this Sep 7, 2022
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 1, 2022
Fixes tafia#252 - ok
Fixes tafia#280 - ok
Fixes tafia#287 - ok
Fixes tafia#343 - ok
Fixes tafia#346 - not ok
Fixes tafia#361 - ok
Partially addresses tafia#368
Fixes tafia#429 - ok
Fixes tafia#430 - ok
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 1, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 24, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430

Fixes all tests
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 25, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430

Fixes all tests
@Mingun Mingun closed this as completed in 4f376b1 Oct 26, 2022
JOSEPHGILBY pushed a commit to JOSEPHGILBY/quick-xml that referenced this issue Nov 5, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430

Fixes all tests

Co-authored-by: Daniel Alley <dalley@redhat.com>
JOSEPHGILBY pushed a commit to JOSEPHGILBY/quick-xml that referenced this issue Nov 5, 2022
Fixes tafia#252
Fixes tafia#280
Fixes tafia#287
Fixes tafia#343
Fixes tafia#346
Fixes tafia#361
Partially addresses tafia#368
Fixes tafia#429
Fixes tafia#430

Fixes all tests

Co-authored-by: Daniel Alley <dalley@redhat.com>
Mingun added a commit to Mingun/quick-xml that referenced this issue Jan 8, 2023
dralley pushed a commit that referenced this issue Jan 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help wanted serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants