Skip to content

PrefixedAttName lost in subtree when serializing #114

Description

@valkum

Another (possibly) bug I encountered while trying to implement more epp specs (launch-1.0 which requires rfc7848 see https://datatracker.ietf.org/doc/html/rfc7848#page-11 for example).

If you have types in one namespace that reference types in another namespace and you want to use prefixes, the serializer will assume the wrong default namespace for all elements below the first prefixed namespace.

Repro:

use instant_xml::{FromXml, ToXml};

const NS1: &str = "urn:ns1";
const NS2: &str = "urn:ns2";

#[derive(FromXml, ToXml, PartialEq, Debug)]
#[xml(ns(NS1, ns2 = NS2))]
struct Root {
    a: A,
    // This uses a field from a different namespace
    b: B,
}

#[derive(FromXml, ToXml, PartialEq, Debug)]
#[xml(ns(NS1))]
struct A {
    value: String,
}

// These are structs in the NS2 namespace
#[derive(FromXml, ToXml, PartialEq, Debug)]
#[xml(ns(NS2))]
struct B {
    value: String,
    c: C,
}

#[derive(FromXml, ToXml, PartialEq, Debug)]
#[xml(ns(NS2))]
struct C {
    value: String,
}

fn main() {
    let root = Root {
        a: A {
            value: "Value A".to_string(),
        },
        b: B {
            value: "Value B".to_string(),
            c: C {
                value: "Value C".to_string(),
            },
        },
    };

    let xml_string = instant_xml::to_string(&root).unwrap();
    println!("Serialized XML:\n{}", xml_string);
}

Serializes to

<Root xmlns="urn:ns1" xmlns:ns2="urn:ns2">
  <A>
    <value>Value A</value>
  </A>
  <ns2:B>
    <value>Value B</value>
    <C>
      <value>Value C</value>
    </C>
  </ns2:B>
</Root>

But should serialize to either:

<Root xmlns="urn:ns1" xmlns:ns2="urn:ns2">
  <A>
    <value>Value A</value>
  </A>
  <ns2:B>
    <ns2:value>Value B</ns2:value>
    <ns2:C>
      <ns2:value>Value C</ns2:value>
    </ns2:C>
  </ns2:B>
</Root>

Note that you can remove the prefix ns from Root, to get a <B xmlns="urn:ns2"> which will emit the correct code with default namspacing rules.

From the XML name spec:

The scope of a namespace declaration declaring a prefix extends from the beginning of the start-tag in which it appears to the end of the corresponding end-tag, excluding the scope of any inner declarations with the same NSAttName part. In the case of an empty tag, the scope is the tag itself.
Thus the ns2 prefix is missing on C and the respective values inside <ns2:B> and </ns2:B>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions