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

Empty namespace #1087

Closed
antonioturdo opened this issue May 20, 2019 · 11 comments
Closed

Empty namespace #1087

antonioturdo opened this issue May 20, 2019 · 11 comments

Comments

@antonioturdo
Copy link

antonioturdo commented May 20, 2019

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

I really am not sure whether this is a bug or not. I have a third party xml that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<A xmlns="http://namespace.org/ns">
   <B xmlns="">text</B>
</A>

An empty namespace should means no default namespace has to be considered for B.

Current implementation:

$namespaces = $data->getDocNamespaces();

if (isset($namespaces[''])) {
    $prefix = uniqid('ns-');
    $data->registerXPathNamespace($prefix, $namespaces['']);
    $nodes = $data->xpath('./' . $prefix . ':' . $name);
} else {
    $nodes = $data->xpath('./' . $name);
}

The document has default namespace, but since "B" element is not in the default namespace, I think xpath correctly doesn't found it.

Steps required to reproduce the problem

  1. Deserialize the xml shown above

Expected Result

  • An object "A" with the "B" property set.

Actual Result

  • An object "A" with null "B" property.
@goetas
Copy link
Collaborator

goetas commented May 22, 2019

<?xml version="1.0" encoding="utf-8"?>
<A xmlns="http://namespace.org/ns">
   <B xmlns="">text</B>
</A>

OR

<?xml version="1.0" encoding="utf-8"?>
<x:A xmlns="http://namespace.org/ns" y:xmlns="">
   <y:B>text</y:B>
</x:A>

OR

<?xml version="1.0" encoding="utf-8"?>
<x:A x:xmlns="http://namespace.org/ns">
   <B>text</B>
</x:A>

Are all equivalent.

The result depends on your XML mapping info in the entity. Can you share those info please?

@antonioturdo
Copy link
Author

antonioturdo commented May 23, 2019

use JMS\Serializer\Annotation as JMS;

/**
 * @JMS\XmlNamespace(uri="http://namespace.org/ns")
 * @JMS\XmlNamespace(uri="", prefix="y")
 * @JMS\XmlRoot(name="A")
 */
class A {
    
	/**
	 * @JMS\Type("MyNamespace\Entity\B")
	 * @JMS\SerializedName("B")
         * @JMS\XmlElement(namespace="")
	 */	
	protected $B;
}

In class B I haven't annotations.
The empty namespace annotation in B property does not have effects, because

if ($metadata->xmlNamespace) {
    $node = $data->children($metadata->xmlNamespace)->$name;
    if (!$node->count()) {
        return;
    }
}

this condition evaluates to false and hovewer, also changing that to $metadata->xmlNamespace !== null, children method does not return nodes.

I also noticed that getDocNamespaces method of SimpleXMLElement does not return the namespace with empty url (using xml with y:xmlns="").

@goetas
Copy link
Collaborator

goetas commented May 23, 2019

Have you just tried omitting JMS\XmlElement() on the $B variable?

@antonioturdo
Copy link
Author

Yes, I did and it does not work.

@discordier
Copy link
Contributor

Running into the exact same problem atm while trying to consume and create XML for German Tax authorities.

Currently trying to throw a PR together.

@goetas
Copy link
Collaborator

goetas commented Jun 13, 2019

Would be great, thanks

discordier added a commit to discordier/serializer that referenced this issue Jun 13, 2019
This is a basic implementation of `xmlns=""` handling.
discordier added a commit to discordier/serializer that referenced this issue Jun 13, 2019
This is a basic implementation of `xmlns=""` handling.
@discordier
Copy link
Contributor

discordier commented Jun 13, 2019

See #1095.

From: #1087 (comment)

Case 1 & 3 should be fixed.
Case 2 is illegal according to XML specs.

@antonioturdo
Copy link
Author

I think it should be handled for collections too.

@discordier
Copy link
Contributor

If you could come up with a proper test case, I am willing to add it to my implementation.

However, I'd love to hear input vrom @goetas first if I am on the right track.

PS: @antonioturdo is your issue fixed with my implementation? Did you check?

discordier added a commit to discordier/serializer that referenced this issue Jun 17, 2019
This is a basic implementation of `xmlns=""` handling.
discordier added a commit to discordier/serializer that referenced this issue Jun 17, 2019
This is a basic implementation of `xmlns=""` handling.
@antonioturdo
Copy link
Author

I am sorry I have no time to provide a test case about collections.

@goetas
Copy link
Collaborator

goetas commented Aug 18, 2019

implemented in #1095

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants