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

XmlRoot and XmlNamespace conflict #1389

Open
proggeler opened this issue Jan 28, 2022 · 0 comments
Open

XmlRoot and XmlNamespace conflict #1389

proggeler opened this issue Jan 28, 2022 · 0 comments

Comments

@proggeler
Copy link

proggeler commented Jan 28, 2022

Q A
Bug report? yes
Feature request? yes
BC Break report? no
RFC? yes

Both XMLNamespace and XMLRoot can define a namespace and prefix, but it is unclear what takes precedence. They even can conflict when they both point to the same prefix.

It would be better to replace the XmlRoot::prefix with XmlRoot::withPrefix (or usePrefix):

/**
 * @JMS\XmlRoot(name="Foo", namespace="urn.test.ns", withPrefix=true)
 * @JMS\XmlNamespace(uri="urn.test.ns", prefix="ns2")
 */
class Foo
{}

This way the prefix can only be set by the XmlNamespace.

Steps required to reproduce the problem

/**
 * @JMS\XmlRoot(name="Foo", namespace="urn.test.ns", prefix="ns1")
 * @JMS\XmlNamespace(uri="urn.test.ns", prefix="ns2")
 */
class Foo
{
    /**
     * @JMS\Type("int")
     * @JMS\XmlElement(namespace="urn.test.ns")
     */
    public $count = 1;
}

Expected Result

Both point to the same namespace so XmlNamespace takes precedence. (consistent with XmlRoot without prefix)

<?xml version="1.0" encoding="UTF-8"?>
<ns2:Foo xmlns:ns2="urn.test.ns">
  <ns2:count>1</ns2:count>
</ns2:Foo>

Actual Result

XmlRoot prefix is used and there is an useless namespace definition.

<?xml version="1.0" encoding="UTF-8"?>
<ns1:Foo xmlns:ns1="urn.test.ns" xmlns:ns2="urn.test.ns">
  <ns1:count>1</ns1:count>
</ns1:Foo>

Second example

/**
 * @JMS\XmlRoot(name="Foo", namespace="urn.test.ns", prefix="ns1")
 * @JMS\XmlNamespace(uri="urn.test.other.ns", prefix="ns1")
 */
class Foo
{
    /**
     * @JMS\Type("int")
     * @JMS\XmlElement(namespace="urn.test.other.ns")
     */
    public $count = 1;
}

Expected Result

An exception about the conflicting namespaces, or a generated prefix for the prefix that is the second one pointing to the same prefix:

<?xml version="1.0" encoding="UTF-8"?>
<ns-86f87953:Foo xmlns:ns86f87953="urn.test.ns" xmlns:ns1="urn.test.other.ns">
  <ns1:count>1</ns1:count>
</ns1:Foo>

Actual Result

Apparently the XmlNamespace takes precedence now.
This is not valid due to the overriding prefix. (Foo uses the wrong namespace)

<ns1:Foo xmlns:ns1="urn.test.other.ns">
  <ns1:count>1</ns1:count>
</ns1:Foo>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant