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

0.31.0 breaks newtype serialization #708

Closed
realsama opened this issue Jan 23, 2024 · 2 comments
Closed

0.31.0 breaks newtype serialization #708

realsama opened this issue Jan 23, 2024 · 2 comments
Labels
duplicate serde Issues related to mapping from Rust types to XML

Comments

@realsama
Copy link

Hello there,

I made an upgrade from 0.30.0 -> 0.31.0 but this breaks my code. The below is a reproducible prototype.

use quick_xml::se::to_string;

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename = "document")]
pub struct Document {
    #[serde(rename = "@type")]
    pub _type: String,
    pub section: Option<Section>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Section {
    #[serde(rename = "@name")]
    pub name: String,
    #[serde(default)]
    pub child: Vec<SectionChild>,
}

#[derive(Serialize, Deserialize, Debug)]
pub enum SectionChild {
    #[serde(rename = "domain")]
    Domain(Domain),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Domain {
    #[serde(rename = "@name")]
    pub name: String,
}

pub fn main() {
    let doc = Document {
        _type: "document".to_string(),
        section: Some(Section {
            name: "section".to_string(),
            child: vec![SectionChild::Domain(Domain {
                name: "domain".to_string(),
            })],
        }),
    };

    let xml = to_string(&doc).unwrap();
    println!("{}", xml);
}

Running the above will produce an error.

called `Result::unwrap()` on an `Err` value: Unsupported("cannot serialize enum newtype variant `SectionChild::domain`")
@Mingun
Copy link
Collaborator

Mingun commented Jan 23, 2024

Yes, this is consequences of #630. Unfortunately, serge made a breaking change so we had to change our enum representation policy in incompatible way. You should add rename = "$value" to child member:

 #[derive(Serialize, Deserialize, Debug)]
 pub struct Section {
     #[serde(rename = "@name")]
     pub name: String,
-    #[serde(default)]
+    #[serde(default, rename = "$value")]
     pub child: Vec<SectionChild>,
 }

That representation in any case is more correct and also works in 0.30.

Your example in 0.31

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

Worked. Thank you for your response!

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

No branches or pull requests

2 participants