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

Exclude namespace from tag name when comparing against field names #702

Merged
merged 3 commits into from
May 13, 2024

Conversation

leftmostcat
Copy link
Contributor

@leftmostcat leftmostcat commented Jan 10, 2024

Attempting to deserialize the following snippet to the following struct fails with "missing field $value" due to not_in() depending on the full name of the tag rather than the local name. This makes it impossible (or at least very difficult) to deserialize xs:choice values when they have a namespace prefix. Relying on local name addresses this without causing any noticeable complications.

              <t:FolderId Id="Zm9vYmFyCg==" ChangeKey="AQAAAA=="/>
              <t:ParentFolderId Id="Zm9vCg==" ChangeKey="AQAAAA=="/>
              <t:FolderClass>IPF.Note</t:FolderClass>
              <t:DisplayName>Inbox</t:DisplayName>
              <t:TotalCount>48</t:TotalCount>
              <t:ChildFolderCount>0</t:ChildFolderCount>
              <t:UnreadCount>46</t:UnreadCount>
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct FolderInner {
    pub folder_id: BaseFolderId,
    pub parent_folder_id: Option<ParentFolderId>,
    pub folder_class: Option<String>,
    pub display_name: Option<String>,
    total_count: Option<u32>,
    child_folder_count: Option<u32>,
    unread_count: Option<u32>,
}

#[derive(Debug, Deserialize)]
pub enum BaseFolderId {
    FolderId(FolderId),

    ...
}

#[derive(Debug, Deserialize)]
pub struct FolderId {
    #[serde(rename = "@Id")]
    pub id: String,

    #[serde(rename = "@ChangeKey")]
    pub change_key: Option<String>,
}

@leftmostcat
Copy link
Contributor Author

This seems to address #683.

@Mingun Mingun added serde Issues related to mapping from Rust types to XML namespaces Issues related to namespaces support labels Jan 11, 2024
@codecov-commenter
Copy link

codecov-commenter commented Jan 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 61.32%. Comparing base (5d76174) to head (6177189).
Report is 5 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #702      +/-   ##
==========================================
+ Coverage   61.24%   61.32%   +0.08%     
==========================================
  Files          39       39              
  Lines       16277    16291      +14     
==========================================
+ Hits         9969     9991      +22     
+ Misses       6308     6300       -8     
Flag Coverage Δ
unittests 61.32% <100.00%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Mingun
Copy link
Collaborator

Mingun commented Jan 11, 2024

I would like to:

  1. have tests for that, at least this one in serde-issues.rs:
    /// Regression test for https://github.com/tafia/quick-xml/issues/683.
    #[test]
    fn issue683() {
        #[derive(Deserialize, Debug, PartialEq)]
        enum ScheduleLocation {
            #[serde(rename = "DT")]
            Destination,
        }
    
        #[derive(Deserialize, Debug, PartialEq)]
        #[allow(non_snake_case)]
        struct Schedule {
            cancelReason: Option<u32>,
            #[serde(rename = "$value")]
            locations: Vec<ScheduleLocation>,
        }
        let xml = r#"
            <schedule xmlns:ns2="http://www.thalesgroup.com/rtti/PushPort/Schedules/v3">
                <ns2:DT/>
                <ns2:cancelReason>918</ns2:cancelReason>
            </schedule>"#;
        let result = quick_xml::de::from_str::<Schedule>(xml);
        dbg!(&result);
        assert_eq!(
            result.unwrap(),
            Schedule {
                cancelReason: Some(918),
                locations: vec![ScheduleLocation::Destination],
            }
        );
    }
  2. Have any other tests which could be affected by this change (I think, in new tests/serde-namespaces.rs file)
  3. Have a changelog entry

Xiphoseer and others added 3 commits April 30, 2024 00:16
…ames

This updates the "not_in" check that decides whether to pass a
new start tag within a struct to a $value field, to only consider
the local part of a QName. It now uses the same logic as the QNameDeserializer
that is used for keys/fields already to ensure they stay in sync.

Using the namespaced name in serde (i.e. `#[serde(rename = "ns1:tag")]`)
fails with ``Custom("missing field `ns1:tag`")`` today, so this will
not break existing code.

Co-authored-by: Xiphoseer <me@xiphoseer.de>
@Mingun Mingun merged commit 4a4abf7 into tafia:master May 13, 2024
6 checks passed
@Mingun
Copy link
Collaborator

Mingun commented May 13, 2024

Thanks @leftmostcat and @Xiphoseer !

@leftmostcat leftmostcat deleted the fix-namespaced-choices branch May 13, 2024 17:17
@leftmostcat
Copy link
Contributor Author

Thanks for knocking this into shape and merging!

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

Successfully merging this pull request may close these issues.

Repeated choice plus other field
4 participants