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

Deserialize xmlKeyValuePairs #840

Merged
merged 4 commits into from
Feb 3, 2018

Conversation

fdyckhoff
Copy link
Contributor

@fdyckhoff fdyckhoff commented Nov 22, 2017

Q A
Bug fix? yes
New feature? yes
Doc updated no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #820 schmittjoh/JMSSerializerBundle#521 schmittjoh/JMSSerializerBundle#470
License Apache-2.0

Copy link
Collaborator

@goetas goetas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Thanks a lot for your contribution. This was a long standing feature request!
I've left some review comment. Let me know if I can help!

// handle key-value-pairs
if (null !== $metadata && $metadata->xmlKeyValuePairs) {
if (2 !== count($type['params'])) {
throw new RuntimeException(sprintf('The array type must be specified as "array<K,V>" for Key-Value-Pairs.'));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sprintf here is not neccessary

{
$xml = $this->getContent('array_key_values_with_same_value_types');
$result = $this->serializer->deserialize($xml, ObjectWithXmlKeyValuePairsWithSameValueTypes::class, 'xml');
$this->assertEquals(new ObjectWithXmlKeyValuePairsWithSameValueTypes(), $result);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you put in the xml different values than the defaults for the property $list and assert that those values are set?
the thing is that this test will be true even if the deserializer simply ignores the $list property

@@ -151,6 +151,28 @@ public function visitDouble($data, array $type, Context $context)

public function visitArray($data, array $type, Context $context)
{
$metadata = $this->currentMetadata;
if (null === $metadata && $context->getMetadataStack()->count()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of this "dirty" check, what about adding here a setCurrentMetadata call as here. Should be faster and "cleaner"

relates to schmittjoh#820

deserialize arrays with key-value pairs

add test case with other values from default

set current metadata from stack in serialization context
@fdyckhoff fdyckhoff force-pushed the deserialize-xml-key-value-pairs branch from 6201e33 to 2852fe1 Compare November 24, 2017 17:15
@fdyckhoff
Copy link
Contributor Author

@goetas Something is still not perfect... In the project, where I integrated this feature, the exception that the array<K,V> type needs to be set is now thrown for other attributes (which do not have the key-value pair annotation set).

I added a testcase for this bug but I'm not sure how to fix it. Should I add the revertCurrentMetadata call before each return statement?
The previous version without setting the current metadata variable worked... Should I revert back to that?

@fdyckhoff
Copy link
Contributor Author

I added the revert calls... Works again - but the solution is not pretty... Do you have any improvement ideas?

@goetas
Copy link
Collaborator

goetas commented Nov 24, 2017

I was more thinking of something as https://github.com/dyvelop/serializer/compare/deserialize-xml-key-value-pairs...goetas:dyvelop-deserialize-xml-key-value-pairs-example?expand=1

(my example should ne not working but notice the setCurrentMetadata inside the visitProperty method

@fdyckhoff
Copy link
Contributor Author

Like this? 805286b

@goetas
Copy link
Collaborator

goetas commented Nov 27, 2017

Like this? 805286b

Not really. There should be no "visitor-specific" code in the graph navigator.

Did you check my comment (and the code there)?

I was more thinking of something as https://github.com/dyvelop/serializer/compare/deserialize-xml-key-value-pairs...goetas:dyvelop-deserialize-xml-key-value-pairs-example?expand=1
(my example should ne not working but notice the setCurrentMetadata inside the visitProperty method

@bentcoder
Copy link

Happy that this will address my issue tickets. Thanks.

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

Successfully merging this pull request may close these issues.

3 participants