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

deserialize() returns Err when element has attribute i:nil="true"

Open synlestidae opened this issue 7 years ago • 0 comments

I have the following struct.

#[derive(Serialize, Deserialize, Debug)]
pub struct InitiateTransactionResponse {
    #[serde(rename="Error", default)]
    pub errors: Vec<InitiateTransactionError>,
    #[serde(rename="TransactionStatusCode", default)]
    pub transaction_status_code: Option<TransactionStatusCode>,
    #[serde(rename="Transaction", default)]
    pub transaction: Vec<PoliTransactionResponse>
}

It should deserialise from this XML (which is returned from an API).

<?xml version="1.0" encoding="utf-8"?>
<InitiateTransactionResponse
xmlns="http://schemas.datacontract.org/2004/07/Centricom.POLi.Services.MerchantAPI.Contract
s" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Errors
xmlns:dco="http://schemas.datacontract.org/2004/07/Centricom.POLi.Services.MerchantAPI.DCO"
>
<dco:Error>
<dco:Code>1003</dco:Code>
<dco:Field />
<dco:Message>POLi is unable to continue with this payment. Please contact the
Merchant for assistance.</dco:Message> </dco:Error>
  </Errors>
  <TransactionStatusCode i:nil="true" />
  <Transaction i:nil="true"
xmlns:dco="http://schemas.datacontract.org/2004/07/Centricom.POLi.Services.MerchantAPI.DCO"
/>
</InitiateTransactionResponse>

But my deserialising test panics with this error:

thread 'tests::initiate_transaction_tests::test_response_error_deserializes' panicked at 'called `Result::unwrap()` on an `Err` value: Expected token &XmlEvent::Characters(ref name) |
&XmlEvent::StartElement { name: OwnedName { local_name: ref name, .. }, .. }, found EndElement({http://schemas.datacontract.org/2004/07/Centricom.POLi.Services.MerchantAPI.Contract
s}TransactionStatusCode)', libcore/result.rs:945:5

When I remove the Transaction and TransactionStatusCode elements as so (both have i:nil attributes)

<?xml version="1.0" encoding="utf-8"?>
<InitiateTransactionResponse
xmlns="http://schemas.datacontract.org/2004/07/Centricom.POLi.Services.MerchantAPI.Contract
s" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Errors
xmlns:dco="http://schemas.datacontract.org/2004/07/Centricom.POLi.Services.MerchantAPI.DCO"
>
<dco:Error>
<dco:Code>1003</dco:Code>
<dco:Field />
<dco:Message>POLi is unable to continue with this payment. Please contact the
Merchant for assistance.</dco:Message> </dco:Error>
  </Errors>
</InitiateTransactionResponse>

then it deserialises and my tests pass. Shouldn't serde-xml-rs handle the case where i:nil=true is set? I expect that transaction_status_code is None and transaction is an empty Vec. Is that good reasoning?

synlestidae avatar Jul 11 '18 22:07 synlestidae