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

Nesting Field doesn't deserialize correctly #192

Closed
lessu opened this issue Sep 16, 2024 · 2 comments
Closed

Nesting Field doesn't deserialize correctly #192

lessu opened this issue Sep 16, 2024 · 2 comments

Comments

@lessu
Copy link
Contributor

lessu commented Sep 16, 2024

I had a structure like this (xsd standard)

#[derive(Clone, Default, Debug, PartialEq, YaDeserialize)]
#[yaserde(
    rename = "sequence",
    prefix = "xs",
    namespace = "xs: http://www.w3.org/2001/XMLSchema"
)]
pub struct Sequence {
    #[yaserde(attribute)]
    pub id: Option<String>,

    #[yaserde(attribute, rename = "maxOccurs",default = "default_max_occurs")]
    pub max_occurs: MaxOccurences,

    #[yaserde(attribute, rename = "minOccurs",default = "default_u32_1")]
    pub min_occurs: u32,

    #[yaserde(rename = "annotation", prefix = "xs")]
    pub annotation: Option<Annotation>,

    #[yaserde(rename = "element", prefix = "xs")]
    pub elements: Vec<Element>,

    #[yaserde(rename = "group", prefix = "xs")]
    pub groups: Vec<Group>,

    #[yaserde(rename = "choice", prefix = "xs")]
    pub choices: Vec<Choice>,

    #[yaserde(rename = "sequence", prefix = "xs")]
    pub sequences: Vec<Sequence>,

    #[yaserde(rename = "any", prefix = "xs")]
    pub any: Vec<Any>,
}

Sequence is nesting.

when parsing format like this

<?xml version="1.0" encoding="UTF-8"?>
<xsd:sequence xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
    <xsd:group ref="AR:AR-OBJECT"/>
    <xsd:group ref="AR:AUTOSAR"/>
</xsd:sequence>

The result is not correct

Sequence {
    id: None,
    max_occurs: Number {
        value: 1,
    },
    min_occurs: 1,
    annotation: None,
    elements: [],
    groups: [
        Group {
            id: None,
            max_occurs: None,
            min_occurs: None,
            name: None,
            ref_v: Some(
                "AR:AUTOSAR",
            ),
            annotation: None,
            componenet: None,
        },
    ],
    choices: [],
    sequences: [
        Sequence {
            id: None,
            max_occurs: Number {
                value: 1,
            },
            min_occurs: 1,
            annotation: None,
            elements: [],
            groups: [],
            choices: [],
            sequences: [],
            any: [],
        },
    ],
    any: [],
},
@lessu
Copy link
Contributor Author

lessu commented Sep 16, 2024

and the reason here is in expanded rust

impl ::yaserde::YaDeserialize for Sequence {
    #[allow(unused_variables)]
    fn deserialize<R: ::std::io::Read>(
            // ...
            match event {
                ::yaserde::__xml::reader::XmlEvent::StartElement {
                    ref name,
                    ref attributes,
                    ..
                } => {
                    if depth == 0 && name.local_name == "xs:sequence" {
                        let event = reader.next_event()?;
                    } else {
                        let namespace = name.namespace.clone().unwrap_or_default();
                        match (namespace.as_str(), name.local_name.as_str()) {
                            ("http://www.w3.org/2001/XMLSchema", "annotation") => {
                                if depth == 0 {
                                    let _root = reader.next_event();
                                }
                                if let Ok(
                                    ::yaserde::__xml::reader::XmlEvent::StartElement { .. },
                                ) = reader.peek()
                                {
                                    let value = <Annotation as ::yaserde::YaDeserialize>::deserialize(
                                        reader,
                                    )?;
                                    __annotation_value = ::std::option::Option::Some(value);
                                    let _event = reader.next_event()?;
                                }
                            }
      // ...

The way we check

                    if depth == 0 && name.local_name == "xs:sequence" {

is not correct

while name.local_name is "sequence", it didn't match this root check condition but hit the children deserialize

                            ("http://www.w3.org/2001/XMLSchema", "sequence") => {
                                if depth == 0 {
                                    let _root = reader.next_event();
                                }
                                if let Ok(
                                    ::yaserde::__xml::reader::XmlEvent::StartElement { .. },
                                ) = reader.peek()
                                {
                                    let value = <Sequence as ::yaserde::YaDeserialize>::deserialize(
                                        reader,
                                    )?;
                                    __sequences_value.push(value);
                                    let _event = reader.next_event()?;
                                }
                            }

lessu added a commit to lessu/yaserde that referenced this issue Sep 16, 2024
issue media-io#192

Signed-off-by: limingyi <lessu@163.com>
MarcAntoine-Arnaud pushed a commit that referenced this issue Sep 16, 2024
issue #192

Signed-off-by: limingyi <lessu@163.com>
@lessu
Copy link
Contributor Author

lessu commented Sep 16, 2024

Merged

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

1 participant