Cryptic error UnexpectedStart if serde structs do not match XML

Open
#834 0 comments 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust

Research direction

Start by tracing how quick_xml:🇩🇪:from_str handles a serde struct field whose XML value begins with a nested element, then inspect the error conversion that produces UnexpectedStart. The reproduction uses XmlRoot.generalComment as a String while the XML contains a <text> child. Done means the mismatch returns a more informative error that helps identify the unexpected element; the issue names no test file, so locate or add coverage for this reproduction.

Written by the indexing model from the issue text.

Description

serde

Hi! I'm using quick_xml to deserialize to some serde structs. While I'm still developing the structs against the XML, it happens that I have mismatches between my structs and the XML. If that happens instead of getting an error like "Expected type X at location Y", I get a pretty cryptic error, specifically:

called `Result::unwrap()` on an `Err` value: UnexpectedStart([116, 101, 120, 116])

Which doesn't give me much information to start debugging, about where the issue is, or even what it is.

A real-life-inspired reproduction that creates this error is as follows:

    let xml = r#"
<?xml version='1.0' encoding='UTF-8'?>
    <outerTag>
        <generalComment>
              <text xml:lang="en" index="0">some text</text>
        </generalComment>
    </outerTag>
    "#;

    #[derive(Deserialize)]
    pub struct XmlRoot {
       // Oops you thought this was a string but it's not
        generalComment: String,
    }

    // Error: Unexpected start
    let result = quick_xml::de::from_str::<XmlRoot>(xml).unwrap();
Dominant language
Rust
Stars
1.6k
Forks
299
Avg merge
2d 4h
Merged PRs (30d)
9

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from tafia/quick-xml

All issues in tafia/quick-xml

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.