Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

"array" type: Not working for arrays of DateTime objects #199

Closed
lukey78 opened this issue Dec 5, 2013 · 6 comments
Closed

"array" type: Not working for arrays of DateTime objects #199

lukey78 opened this issue Dec 5, 2013 · 6 comments

Comments

@lukey78
Copy link

lukey78 commented Dec 5, 2013

I recently tried to use the following annotation that should work as documented:

@JMS\Type("array<DateTime<'Y-m-d'>>")

But it doesn't. The DateTimes are serialized to empty strings.

While investigating the problem I found that line in the visitArray function of (e.g.) the GenericSerializationVisitor:

   $v = $this->navigator->accept($v, isset($type['params'][1]) ? $type['params'][1] : null, $context);

$type['params'][1] normally doesn't exist. The accept function is called with a null type which results in auto detection of the type.

If I change the $type['params'][1] to $type['params'][0] everything seems to work as expected.

Is this a bug or a feature? I could also submit a pull request if this is a bug.

Thanks,
Jens

@schmittjoh
Copy link
Owner

This seems like a bug. A tested fix would be appreciated.

@schmittjoh
Copy link
Owner

When you work on a fix, can you make sure that you do not break support for:

array<string,DateTime<'Y-m-d'>>

@lukey78
Copy link
Author

lukey78 commented Dec 5, 2013

I tried running the test suite and got errors, e.g.:

Call to undefined method JMS\Serializer\Tests\Serializer\ContextTest::callback() 

What's wrong?

@stof
Copy link
Contributor

stof commented Dec 7, 2013

@lukey78 Your PHPUnit version is too old. Use PHPUnit 3.7 (i.e. the current stable version)

@lukey78
Copy link
Author

lukey78 commented Dec 7, 2013

@stof: Thanks!

I tried fixing this issue and stumbled upon the following behaviour where I'm not sure if it's wrong what I'm doing or if the Serializer doesn't handle this correctly.

Given an object with the following annotated property:

    /**
     * @var \DateTime[]
     * @Type("array<string,DateTime>")
     * @XmlKeyValuePairs
     */
    private $exampleArray;

Serialized to XML with some example data I get:

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <example_array>
    <testdate1><![CDATA[2047-01-01T12:47:47+0100]]></testdate1>
    <testdate2><![CDATA[2013-12-05T00:00:00+0100]]></testdate2>
  </example_array>
</result>

When trying to deserialize this XML back into the object I get an empty array:

object(ExampleObject)#554 (1) {
  ["exampleArray":"ExampleObject":private]=>
  array(0) {
  }
}

Maybe I used the wrong XML annotations?
Serializing and deserializing this from/to the other formats (json/yml) works.

@schmittjoh
Copy link
Owner

I think XmlKeyValuePairs is a one way street currently. You can remove the deserialization part of this test case.

schmittjoh added a commit that referenced this issue Feb 5, 2014
fix issue #199: "array" type ignoring DateTime format
@lukey78 lukey78 closed this as completed Feb 5, 2014
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants