media-io / yaserde

Yet Another Serializer/Deserializer
MIT License
174 stars 58 forks source link

Attribute does not serialize in enum #149

Open sobczyk opened 1 year ago

sobczyk commented 1 year ago
use yaserde_derive::YaSerialize;

#[derive(Default, YaSerialize)]
struct A
{
    #[yaserde(attribute)]
    val: u8
}
#[derive(Default, YaSerialize)]
struct B
{
    val: u8
}
#[derive(YaSerialize)]
enum MainNode
{
    A(A),
    B(B),
}

#[derive(Default, YaSerialize)]
struct Main
{
    #[yaserde(child, flatten)]
    m: Vec<MainNode>
}

fn sample() -> Main {
    Main {
        m: vec![
            MainNode::A(
                A{val: 1}
                ),
            MainNode::B(
                B{val: 2}
                )
        ]
    }
}
fn main()
{
    let node = sample();
    println!("{}", yaserde::ser::to_string(&node).unwrap());
}

#[cfg(test)]
mod tests {
    const XML: &str = r##"<?xml version="1.0" encoding="utf-8"?><Main><A val="1" /><B><val>2</val></B></Main>"##;
    use crate::sample;
    #[test]
    fn attribute() {
        let node = sample();
        let data = yaserde::ser::to_string(&node).unwrap();
        assert_eq!(data, XML);
    }
}

the A::val is not serialized from Main structure.

OkieOth commented 7 months ago

I have the same issue and will try to debug.

OkieOth commented 7 months ago

Found something and created a PR https://github.com/media-io/yaserde/pull/167