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

Is it as expect that Option attribute serialize into empty string instead of non-attribute. #692

Open
Vonfry opened this issue Dec 3, 2023 · 1 comment
Labels
documentation Issues about improvements or bugs in documentation serde Issues related to mapping from Rust types to XML

Comments

@Vonfry
Copy link

Vonfry commented Dec 3, 2023

A simple example here.

use serde::Serialize;
use quick_xml::se::to_string;

#[derive(Serialize)]
struct Foo {
    #[serde(rename = "@foo")]
    foo: Option<usize>
}

#[test]
fn test_foo() {
    let foo = Foo { foo: None };

    let foo_xml = to_string(&foo).unwrap();

    assert_eq!(foo_xml, "<Foo foo=\"\"/>");
    assert_eq!(foo_xml, "<Foo/>");
}

Is foo="" expected in quick-xml instead of dismissing this attribute?

@Mingun Mingun added the serde Issues related to mapping from Rust types to XML label Dec 3, 2023
@Mingun
Copy link
Collaborator

Mingun commented Dec 3, 2023

Yes, that is expected. This is the same behavior as in serde_json (yes, it is asymmetric de vs. ser). If you want to skip field serialization, add #[serde(skip_serializing_if = "Option::is_none")]. The documentation should be updated, although

@Mingun Mingun added the documentation Issues about improvements or bugs in documentation label Dec 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Issues about improvements or bugs in documentation serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

No branches or pull requests

2 participants