Skip to content

Conversation

@cr8t
Copy link

@cr8t cr8t commented Sep 6, 2023

Adds a failing integration test for nested structures that contain repeating keys.

xml-rs appears to not be able to find an ending XML token in the nested inner structure when it contains an array type with multiple values.

Error from the test tests/nested-failures.rs:

$ cargo test --test nested-failures
    Updating crates.io index
    Finished test [unoptimized + debuginfo] target(s) in 2.28s
     Running tests/nested-failures.rs (target/debug/deps/nested_failures-966734fbe6baaf5e)

running 1 test
test nested_struct ... FAILED

failures:

---- nested_struct stdout ----
Error: UnexpectedToken { token: "XmlEvent::EndElement { name, .. }", found: "StartElement(value, {\"\": \"\", \"xml\": \"http://www.w3.org/XML/1998/namespace\", \"xmlns\": \"http://www.w3.org/2000/xmlns/\"})" }


failures:
    nested_struct

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass `--test nested-failures`

Adds a failing integration test for nested structures that contain
repeating keys.

`xml-rs` appears to not be able to find an ending XML token in the
nested inner structure when it contains an `array` type with multiple
`value`s.
@punkstarman
Copy link
Collaborator

The code you submitted doesn't work because the types don't correspond with the document. I'll be improving the documentation so that this confusion is less likely in the future.

The following adjustments to the code work.

use serde_xml_rs as xml;

#[repr(C)]
#[derive(serde::Deserialize, serde::Serialize)]
pub enum Value {
    #[serde(rename = "i32")]
    I32(i32),
    #[serde(rename = "struct")]
    Struct(Struct),
    #[serde(rename = "array")]
    Array(Array),
}

#[repr(C)]
#[derive(serde::Deserialize, serde::Serialize)]
pub struct Struct {
    #[serde(rename = "member")]
    members: Vec<Member>,
}

#[repr(C)]
#[derive(serde::Deserialize, serde::Serialize)]
pub struct Member {
    name: String,
    value: Value,
}

#[repr(C)]
#[derive(serde::Deserialize, serde::Serialize)]
pub enum Structs {
    #[serde(rename = "struct")]
    Struct(Struct),
}

#[repr(C)]
#[derive(serde::Deserialize, serde::Serialize)]
pub struct Array {
    #[serde(rename = "#content")]
    data: Vec<Value>,
}

#[test]
fn nested_struct() -> Result<(), xml::Error> {
    let exp_xml = r#"<?xml version="1.0"?>
    <value>
      <struct>
        <member>
          <name>outerStruct</name>
          <value>
            <array>
              <struct>
                <member>
                  <name>innerStruct</name>
                  <value>
                    <array>
                      <i32>0</i32>
                      <i32>1</i32>
                    </array>
                  </value>
                </member>
              </struct>
            </array>
          </value>
        </member>
      </struct>
    </value>
    "#;

    xml::from_str::<Value>(exp_xml)?;
    Ok(())
}

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

Successfully merging this pull request may close these issues.

2 participants