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 adds extra backslashs #321

Closed
Kerwood opened this issue Sep 13, 2021 · 7 comments
Closed

from_str adds extra backslashs #321

Kerwood opened this issue Sep 13, 2021 · 7 comments

Comments

@Kerwood
Copy link

Kerwood commented Sep 13, 2021

When deserializing from XML to a struct, quick-xml adds an extra backslash to backslashes, like its trying to escape them.

So <FolderPath>\Infrastructure\Network</FolderPath> becomes folder_path: "\\Infrastructure\\Network".
Is that a bug or am I missing something ?

@tafia
Copy link
Owner

tafia commented Sep 21, 2021

Can you provide an example?

@Kerwood
Copy link
Author

Kerwood commented Sep 21, 2021

Sure...

Dependencies

[dependencies]
serde = { version = "1.0", features = [ "derive" ] }
quick-xml = { version = "0.21", features = [ "serialize" ] }
csv = "1.1.6"

Code

extern crate quick_xml;
use quick_xml::de::from_str;
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Secret {
    #[serde(rename = "SecretName")]
    secret_name: String,
    #[serde(rename = "FolderPath")]
    folder_path: String,
}

fn main() {
    let xml = r#"<Secret>
            <SecretName>VerySecret</SecretName>
            <FolderPath>\Infrastructure\Network</FolderPath>
        </Secret>"#;

    let result: Secret = from_str(&xml).unwrap();
    println!("{:#?}", result);
}

Result

Secret {
    secret_name: "VerySecret",
    folder_path: "\\Infrastructure\\Network",
}

Expected

Secret {
    secret_name: "VerySecret",
    folder_path: "\Infrastructure\Network",
}

@BlueGreenMagick
Copy link
Contributor

That may be a quirk of debug display and the actual string has one backslash.

For example, run the following code:

#[derive(Debug)]
struct Object {
    name: String
}

fn main() {
    let obj = Object {name: r"\".to_string()};
    println!("{:#?}", obj);
}

output:

Object {
    name: "\\",
}

@tafia
Copy link
Owner

tafia commented Oct 26, 2021

I didn't see it, this definitely seems the case. Could you try comparing the actual result with r#"\Infrastructure\Network"# ?

@ghost
Copy link

ghost commented Nov 28, 2021

Is this also related to escaping happening in attribute values (which must AFAIK be quoted anyway)?

e.g.:
Original element:

<CONSTRAINT
    DESCRIPTION="Time alignable annotations within the parent annotation's time interval, gaps are allowed" 
    STEREOTYPE="Included_In"/>

The above deserialised then serialised (apostrophe in DESCRIPTION escaped as &apos;) :

<CONSTRAINT
    DESCRIPTION="Time alignable annotations within the parent annotation&apos;s time interval, gaps are allowed"     
    STEREOTYPE="Included_In"/>

Expected behaviour would be for this to happen only for (unquoted) element values/text, not attributes. The serialised xml in question does validate in either case however.

@Mingun
Copy link
Collaborator

Mingun commented May 21, 2022

The double slash in the output is the result of using Debug trait to output string, as @BlueGreenMagick noted.

The quoting in the XML attributes is excessive and can be avoided, because standard allows to not escape apostrophe if attribute value in double quotes

@Mingun
Copy link
Collaborator

Mingun commented May 25, 2022

Duplicate of #362

@Mingun Mingun marked this as a duplicate of #362 May 25, 2022
@Mingun Mingun closed this as not planned Won't fix, can't repro, duplicate, stale May 25, 2022
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

4 participants