serde-xml-rs icon indicating copy to clipboard operation
serde-xml-rs copied to clipboard

Option type fails on self-closing tag

Open scribe opened this issue 4 years ago • 1 comments

I'm very new to Rust so if this is an obvious mistake I'm sorry in advance.

I have the following struct

    #[derive(Debug, Deserialize, PartialEq)]
    #[serde(rename = "Data", rename_all = "PascalCase")]
    struct CreateBucketResponse {
        creation_date: String,
        data_policy_id: String,
        id: String,
        last_preferred_chunk_size_in_bytes: Option<i64>,
        name: String,
        user_id: String
    }

and it works great in almost every case, but my other system passed me the following data.

<Data>
   <CreationDate>2015-07-22T14:22:49.224Z</CreationDate>
   <DataPolicyId>22bdea46-4b7f-4f70-873c-ff953fa97b3b </DataPolicyId>
   <Id>1acb4e56-ae93-49a0-b288-db93f0a70ea4</Id>
   <LastPreferredChunkSizeInBytes/>
   <Name>new_bucket</Name>
   <UserId>c0a80d43-c1ae-4ee1-87d5-12ca632e8205</UserId>
</Data>

Which generates the following error:

called `Result::unwrap()` on an `Err` value: UnexpectedToken { token: "EndElement", found: "Characters" } thread 'create_bucket::create_bucket::tests::deserialize_create_bucket_response' panicked at 'called `Result::unwrap()` on an `Err` value: UnexpectedToken { token: "EndElement", found: "Characters" }', src/create_bucket.rs:34:72 stack backtrace:

It seems I'm receiving valid XML, is this a parsing issue?

scribe avatar Jun 21 '21 02:06 scribe

Yes, this is a parsing issue. It is even so that the XML generated for a struct field that has the value None is serialized as an empty/self-closing element will not be deserializable again. The only work-around I could find for now, and this will not always work for every application is by placing #[serde(skip_serializing_if = "Option::is_none")] on those field so they are not present in the serialized XML at all.

paulvt avatar Apr 14 '22 11:04 paulvt