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

Help serializing empty attributes #252

Closed
thebeekeeper opened this issue Jan 11, 2021 · 2 comments · Fixed by #490
Closed

Help serializing empty attributes #252

thebeekeeper opened this issue Jan 11, 2021 · 2 comments · Fixed by #490
Assignees
Labels
bug serde Issues related to mapping from Rust types to XML

Comments

@thebeekeeper
Copy link

thebeekeeper commented Jan 11, 2021

I have an externally defined XML standard I'm trying to write to which includes requiring empty attribute values. As an example, I need to write the following:

<TIM A="" D="4">

Where my A attribute needs to be written with an empty value, and D needs a constant value. Is it possible to achieve this with the crate using the serde serialize feature? Ideally, I'd like to use an Option like this:

#[derive(Serialize, Debug)]
struct Tim {
   a: Option<String>,
   b: u8,
}

But when I do that, and set a to None, the A attribute isn't serialized at all. I'm not sure that the crate would be able to decide whether or not to serialize the attribute, so I'm guessing I'll need serialize_with. Also tried that, but haven't been able to get it to write out an empty attribute value. The closest I've gotten is writing out <TIM A=" " D="4"/> but in my case, the space in the A attribute value is invalid.

@clegaard
Copy link

I have experienced a similar issue to @thebeekeeper, the only difference is that it occurs during deserialization.
I would expect the <Foo val="" /> to be mapped to Some("") rather than None, but that does not seem to be the case.

#[derive(Debug, Deserialize, PartialEq)]
struct Foo {
    val: Option<String>,
}

#[test]
fn empty_string() {
    let s = r#""<Foo val=""/>""#;
    let foo: Foo = quick_xml::de::from_str(s).unwrap();
    assert!(foo.val.is_some());
}

@Mingun Mingun added question serde Issues related to mapping from Rust types to XML bug and removed question labels May 21, 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 added a commit to Mingun/quick-xml that referenced this issue Oct 26, 2022
Mingun added a commit to Mingun/quick-xml that referenced this issue Oct 26, 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>
@dsegovia90
Copy link

dsegovia90 commented Nov 4, 2022

Hi, sorry for commenting on this old issue. Just wondering if you found a solution @thebeekeeper for your example?

<TIM A="" D="4">

I'm in the need to send an empty attribute A="" to an API, and can't figure out how to do it. I tried using NoneAsEmptyString from the serde_with crate, but no luck:

#[derive(Debug, Serialize, Eq, PartialEq)]
#[serde_as]
struct {
  #[serde_as(as = "NoneAsEmptyString")]
  A: Option<String>,
  D: u8,
}

Also tried to do a custom Serialization, but serde removes the attribute if it's none.

TIA

JOSEPHGILBY pushed a commit to JOSEPHGILBY/quick-xml that referenced this issue Nov 5, 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
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 serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants