-
-
Notifications
You must be signed in to change notification settings - Fork 586
XML Serialization: fix #1601 #1602
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
Conversation
|
Seems like Scrutinizer is somehow broken. Could somebody restart it or just ignore it...? |
|
Thanks for pull request. Looks like an useful feature! I left one small comment. Can you also add some documentation for it, please? Best, Marcin. |
|
Thanks for the review! The deserialization side of this feature already exists, as the serializer passes the name as an xpath query. I wasn't aware of this until Mirgen pointed it out in this answer. However, there is an issue with deserialization when namespaces are involved. Using this example: serializer/tests/Fixtures/ObjectWithElementAttributeSyntax.php Lines 44 to 49 in 3ecd094
With the current PR, we can serialize In the tests, we first serialize the entity to XML, compare it to the expected result, then try to deserialize back into an object. We had to add this bypass to set serializer/tests/Serializer/XmlSerializationTest.php Lines 662 to 664 in 3ecd094
I'd like to address this deserialization issue separately to keep this PR focused on the serialization side. In my use case, I don't need namespaces, so I'm comfortable with the current implementation. This is an edge case that likely won't affect 99.9% of users, but it would be good to document it or open an issue about it to track the namespace deserialization problem and possible interest fixing it. For documentation, I'll add it as requested. Since this extends an "accidental feature" that was already working for deserialization, this PR completes the picture by providing serialization support. Do you have specific recommendations for how you'd like the documentation structured? I don't have any clue how to document this. I'll implement the requested argument updates soonish after I hear back from you. |
|
Sounds like a plan! Sounds like we will be able to release it next week :) |
|
I continued working on this and got a bit carried away implementing the deserialization side as well, since I could see potential issues arising from incomplete support later. I've fixed the deserialization issues with namespaces and added implementation to serialize attributes into the current object, essentially adding serialization support to this answer. While testing the TLDR - After the latest changes, this PR now has full support for:
I'll work on documenting this feature now. |
|
There was error in tests when running on PHP 7.4: I removed the FYI, totally not related to this PR, I can see this type being used also in serializer/src/Handler/UnionHandler.php Lines 53 to 58 in 3c2d00a
Cheers! |
This PR introduces a new capability to the XML serialization process, allowing properties annotated with
#[XmlElement]to define attributes on sibling XML elements using the#[SerializedName("ElementName/@AttributeName")]syntax.Problem:
Previously, when a property was configured with
#[XmlElement]and#[SerializedName("ElementName/@AttributeName")], the serializer would attempt to create an XML element literally namedElementName/@AttributeName. This would lead to aDOMExceptiondue to invalid characters in the element name. This behavior was inconsistent with the deserialization process, which correctly interprets this syntax to map an XML attribute to a PHP property. This issue is described in #1601.Solution:
The
XmlSerializationVisitor::visitPropertymethod has updated: (simplified/tl;dr)serializedNamecontains the/@patterntrySerializePropertyAsAttributeOnSiblingElement.serializedName.null, the attribute is not rendered.Hopefully, this can be checked quickly, so I know how I continue with my own project using this.