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

Different result between 0.26 and 0.27 #534

Closed
AntoniosBarotsis opened this issue Dec 29, 2022 · 2 comments
Closed

Different result between 0.26 and 0.27 #534

AntoniosBarotsis opened this issue Dec 29, 2022 · 2 comments
Labels
question serde Issues related to mapping from Rust types to XML

Comments

@AntoniosBarotsis
Copy link

AntoniosBarotsis commented Dec 29, 2022

Might be related to #510 but I don't think so.

I am using this crate to parse web feeds, I put together an example below

use quick_xml::de::from_str;
use serde_derive::{Deserialize, Serialize};

fn main() {
    let input = r#"
    <?xml version="1.0" encoding="utf-8"?>
    <!-- minimal, only containing the relevant attributes-->
    <feed xmlns="http://www.w3.org/2005/Atom">
        <title>Multi-Entries Feed</title>
        <link href="http://example.org/" />
        <updated>2022-09-13T18:30:02Z</updated>
        <entry>
            <title>&lt;b&gt;Star&lt;/b&gt; City</title>
            <link href="http://link.com"/>
            <updated>2022-09-18T21:00:00Z</updated>
            <summary>How did it work? &lt;i&gt;Details&lt;/i&gt; &lt;a href="http://liftoff.msfc.nasa.gov"&gt;here&lt;/a&gt;</summary>
        </entry>
    </feed>"#;

    let parsed = from_str::<AtomFeed>(input);

    dbg!(parsed);
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[serde(rename_all = "camelCase")]
#[serde(rename = "feed")]
pub struct AtomFeed {
    pub title: String,
    #[serde(rename = "entry", default)]
    pub entries: Vec<AtomPost>,
  }
  
  #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
  #[serde(rename_all = "camelCase")]
  #[serde(rename = "entry")]
  pub struct AtomPost {
    pub title: String,
    #[serde(rename = "link", default)]
    pub links: Vec<Link>,
    pub summary: Option<String>,
    pub updated: String,
  }
  
  #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
  pub struct Link {
    href: String,
  }

A bit lengthy but oh well 😄

When ran with 0.26 I get the correctly parsed atom feed, when ran with 0.27 I get missing field href.

I added the default parameter in the macro as you explain in #510 but as I said I don't think that's relevant here.

I'm not sure if I am missing something but I didn't see anything that could cause this in the changelog, any ideas?

If this turns out to be an actual issue in the crate I could perhaps share some of the tests I use in my project.

@Mingun
Copy link
Collaborator

Mingun commented Dec 29, 2022

This is because the way how we map elements vs attributes is changed. Now you should explicitly specify that the field represents an attribute by prepending an @ character to its name. Since release 0.27 we have a documentation for the mapping rules, so check it out.

The other your mapping is correct.

@Mingun Mingun closed this as completed Dec 29, 2022
@Mingun Mingun added question serde Issues related to mapping from Rust types to XML labels Dec 29, 2022
@AntoniosBarotsis
Copy link
Author

Thanks a lot for the reply!

I guess I didn't the changelog carefully enough because it was indeed mentioned, cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

No branches or pull requests

2 participants