RReverser / serde-xml-rs

xml-rs based deserializer for Serde (compatible with 1.0+)
https://crates.io/crates/serde-xml-rs
MIT License
269 stars 90 forks source link

Ampersand does not get serialized properly #175

Open JorritSalverda opened 2 years ago

JorritSalverda commented 2 years ago

I have the following struct I'm trying to serialize:

use serde_xml_rs::{from_str, to_string};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
#[serde(rename = "request")]
pub struct TokenRequest {
  pub client: String,
  pub username: String,
  pub password: String,
}

However when I run the following test with an & in a value it should be encoded as &. Unfortunately to_string fails to do so and just keeps &.

    #[test]
    fn serialize_access_token_request() {

      let token_request = TokenRequest {
        client: "a".to_string(),
        username: "b".to_string(),
        password: "c&d".to_string(),
      };

      // act
      let request = to_string(&token_request).unwrap();

      assert_eq!(request, r#"<request><client>a</client><username>b</username><password>c&amp;d</password></request>"#);
    }

It fails as follows:

---- timewax_client::tests::serialize_access_token_request stdout ----
thread 'timewax_client::tests::serialize_access_token_request' panicked at 'assertion failed: `(left == right)`
  left: `"<request><client>a</client><username>b</username><password>c&d</password></request>"`,
 right: `"<request><client>a</client><username>b</username><password>c&amp;d</password></request>"`', src/timewax_client.rs:137:7
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

My Cargo.toml has the following dependencies (and some others that aren't relevant to this issue I would say):

[dependencies]
serde = "1.0.136"
serde-xml-rs = "0.5.1"

Off-topic, shouldn't the serialized string also start with <?xml version="1.0" encoding="utf-8"?>?