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

Text bodys are not working with vectors #196

Open
ahassany opened this issue Oct 16, 2024 · 1 comment
Open

Text bodys are not working with vectors #196

ahassany opened this issue Oct 16, 2024 · 1 comment

Comments

@ahassany
Copy link

I've an element that accepts a text body defined as follow

#[derive(PartialEq, Debug, YaSerialize, YaDeserialize, Default)]
#[yaserde(
    rename = "capability",
    namespace = "urn:ietf:params:xml:ns:netconf:base:1.0"
)]
pub struct Capability {
    #[yaserde(text)]
    pub body: String,
}

And I can successfully serialize/deserialize it

    #[test]
    fn test_capability() {
        let input_str = r#"<?xml version="1.0" encoding="utf-8"?>
<capability xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">urn:ietf:params:netconf:base:1.1</capability>"#;
        let expected = Capability {
            body: "urn:ietf:params:netconf:base:1.1".to_string(),
        };
        let parsed: Capability = from_str(&input_str).unwrap();
        let serialized = to_string_pretty(&parsed).unwrap();

        assert_eq!(parsed, expected);
        assert_eq!(serialized, input_str);
    }

However when I try to use in a vector of another element, it fails expecting the inner element to be defined

#[derive(PartialEq, Debug, YaSerialize, YaDeserialize, Default, Serialize, Deserialize)]
#[yaserde(
    rename = "hello",
    namespace = "urn:ietf:params:xml:ns:netconf:base:1.0"
)]
pub struct Hello {
    #[yaserde(child, rename = "capabilities")]
    pub capabilities: Vec<Capability>,
    #[yaserde(child, rename = "session-id")]
    pub session_id: Option<u32>,
}

    #[test]
    fn test_hello() {
        let content = r#"
            <hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
               <capabilities>
                 <capability>
                   urn:ietf:params:netconf:base:1.1
                 </capability>
                 <capability>
                   urn:ietf:params:netconf:capability:startup:1.0
                 </capability>
                 <capability>
                   http://example.net/router/2.3/myfeature
                 </capability>
               </capabilities>
               <session-id>4</session-id>
             </hello>
            "#;
        let loaded: Hello = from_str(&content).unwrap();
        let expected = Hello {
            session_id: Some(1),
            capabilities: vec![
                Capability {
                    body: "urn:ietf:params:netconf:base:1.1".to_string(),
                },
                Capability {
                    body: "urn:ietf:params:netconf:capability:startup:1.0".to_string(),
                },
                Capability {
                    body: "http://example.net/router/2.3/myfeature".to_string(),
                },
            ],
        };
        assert_eq!(loaded, expected);
        let loaded_str = to_string_pretty(&loaded).unwrap();
        assert_eq!(loaded_str, content);
    }

I get the following error: called Result::unwrap()on anErr value: "body is a required field of Capability"

@jonsaw
Copy link

jonsaw commented Dec 19, 2024

Getting the same error here as well

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

2 participants