Skip to content

Problem when deserializing attribute when using namespaces #15

@mat813

Description

@mat813

I am trying to use xml_serde with attributes in namespaces, and I encounter a problem, with this code:

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct Test {
    #[serde(rename = "{domain}stuff")]
    stuff: InnerTest,
}

#[derive(Debug, Serialize, Deserialize)]
struct InnerTest {
    #[serde(rename = "$attr:{domain}a")]
    pub attribute: i32,
    #[serde(rename = "$value")]
    pub value: String,
}

fn main() -> Result<()> {
    let xml = xml_serde::to_string(&Test {
        stuff: InnerTest {
            attribute: 123,
            value: "foobar".to_string(),
        },
    }).unwrap();

    println!("{xml}");

    let test: xml_serde::Result<Test> = xml_serde::from_string(xml);

    println!("{test:?}");
    Ok(())
}

And it outputs:

<?xml version="1.0" encoding="UTF-8"?>
<stuff xmlns="domain" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="domain domain.xsd" a="123">foobar</stuff>
Err(Message("missing field `$attr:{domain}a`"))

So, serialization is ok, the generated xml is correct, but when deserializing the attribute, it seems that it has "forgotten" there was a default namespace, and it fails.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions