serde-xml-rs icon indicating copy to clipboard operation
serde-xml-rs copied to clipboard

`Expected token XmlEvent::Characters(s), found StartElement` when trying to deserialize text and node children together

Open onelson opened this issue 7 years ago • 1 comments

Hi!

I'm trying to capture some data from an xml fragment that includes both a child node, and a text node, but the parser seems to be having trouble. Looks like capturing $value is breaking when there's any other node present.

The use case is virtually identical to #58 but I'm seeing a different backtrace.

extern crate serde;                                                            
#[macro_use]                                                                   
extern crate serde_derive;                                                     
extern crate serde_xml_rs;                                                     
                                                                               
#[derive(Debug, Deserialize, PartialEq, Default)]                              
pub struct Description {                                                                                            
    #[serde(rename = "ShortName", default)]                                    
    pub short_name: String,                                                    
    #[serde(rename = "$value")]                                                
    pub text: String,                                                          
}                                                                              
                                                                               
#[cfg(test)]                                                                   
mod tests {                                                                    
    use serde_xml_rs::deserialize;                                             
    use super::Description;                                                    
                                                                               
    #[test]                                                                    
    fn test_text_plus_node() {                                                 
        let xml = r#"                                                          
        <Description xml:lang="en">                                            
            <ShortName>Excelsior Desk Chair</ShortName>                          
            Leather Reclining Desk Chair with Padded Arms                        
        </Description>                                                           
        "#;                                                                       
                                                                                  
        let desc: Description = deserialize(xml.as_bytes()).unwrap();             
        assert_eq!("Leather Reclining Desk Chair with Padded Arms", desc.text);
        assert_eq!("Excelsior Desk Chair", desc.short_name);                      
    }                                                                             
} 

Running the test:

$ cargo test
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running target/debug/deps/xml_test-85159a57334a1842

running 1 test
test tests::test_text_plus_node ... FAILED

failures:

---- tests::test_text_plus_node stdout ----
	thread 'tests::test_text_plus_node' panicked at 'called `Result::unwrap()` on an `Err` value: Expected token XmlEvent::Characters(s), found StartElement(ShortName, {"": "", "xml": "http://www.w3.org/XML/1998/namespace", "xmlns": "http://www.w3.org/2000/xmlns/"})', /checkout/src/libcore/result.rs:916:5

Wondering if there's another way we should be marking this up as a struct, or if this is just a bug in the deserialization.

onelson avatar Feb 28 '18 23:02 onelson

Updating to the latest in the repo, I get the same "duplicate field `$value`" message as seen in #58

onelson avatar Mar 01 '18 01:03 onelson