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

Ampersand does not get serialized properly #175

Open
JorritSalverda opened this issue Apr 7, 2022 · 0 comments
Open

Ampersand does not get serialized properly #175

JorritSalverda opened this issue Apr 7, 2022 · 0 comments

Comments

@JorritSalverda
Copy link

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"?>?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant