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

from_str should ignore encoding in XML declaration #324

Closed
ZunTzu opened this issue Oct 1, 2021 · 1 comment · Fixed by #403
Closed

from_str should ignore encoding in XML declaration #324

ZunTzu opened this issue Oct 1, 2021 · 1 comment · Fixed by #403
Labels
encoding Issues related to support of various encodings of the XML documents enhancement help wanted serde Issues related to mapping from Rust types to XML

Comments

@ZunTzu
Copy link

ZunTzu commented Oct 1, 2021

XML can be viewed as a binary format, for instance when parsed using from_reader from a sequence of bytes.
However, when parsing from a &str, the content is already guaranteed to be valid UTF-8.
In that case the encoding hint in the XML declaration should be ignored.

Here is a test that fails (make sure the source code is saved as UTF-8).

#[test]
fn from_str_should_ignore_encoding() {
    let xml = r#"
        <?xml version="1.0" encoding="windows-1252" ?>
        <A a="€" />
    "#;

    #[derive(Debug, PartialEq, Deserialize)]
    struct A {
        a: String,
    }

    let a: A = quick_xml::de::from_str(xml).unwrap();
    assert_eq!(
        a,
        A {
            a: "€".to_string()
        }
    );
}

Test result:

---- foo::from_str_should_ignore_encoding stdout ----
thread 'foo::from_str_should_ignore_encoding' panicked at 'assertion failed: `(left == right)`
  left: `A { a: "€" }`,
 right: `A { a: "€" }`', src/foo.rs:415:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
@Mingun Mingun added enhancement help wanted serde Issues related to mapping from Rust types to XML encoding Issues related to support of various encodings of the XML documents labels May 21, 2022
@Mingun
Copy link
Collaborator

Mingun commented May 21, 2022

That would be a great addition. PR is welcome!

Mingun added a commit to Mingun/quick-xml that referenced this issue Jun 24, 2022
Mingun added a commit to Mingun/quick-xml that referenced this issue Jun 24, 2022
Mingun added a commit to Mingun/quick-xml that referenced this issue Jun 24, 2022
Mingun added a commit to Mingun/quick-xml that referenced this issue Jun 24, 2022
Co-authored-by: Daniel Alley <dalley@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
encoding Issues related to support of various encodings of the XML documents enhancement help wanted serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants