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

Proposed fix for serializing custom DateTimeInterface implementations #1344

Merged
merged 2 commits into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/GraphNavigator/SerializationGraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function accept($data, ?array $type = null)
// If we're serializing a polymorphic type, then we'll be interested in the
// metadata for the actual type of the object, not the base class.
if (class_exists($type['name'], false) || interface_exists($type['name'], false)) {
if (is_subclass_of($data, $type['name'], false)) {
if (is_subclass_of($data, $type['name'], false) && null === $this->handlerRegistry->getHandler(GraphNavigatorInterface::DIRECTION_SERIALIZATION, $type['name'], $this->format)) {
$type = ['name' => \get_class($data), 'params' => $type['params'] ?? []];
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/Handler/DateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,18 @@ public static function getSubscribingMethods()
}

$methods[] = [
'type' => 'DateTimeInterface',
'type' => \DateTimeInterface::class,
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
'format' => $format,
'method' => 'deserializeDateTimeFrom' . ucfirst($format),
];

$methods[] = [
'type' => \DateTimeInterface::class,
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
'format' => $format,
'method' => 'serializeDateTimeInterface',
];
}

return $methods;
Expand All @@ -72,7 +79,7 @@ public function __construct(string $defaultFormat = \DateTime::ATOM, string $def
/**
* @return \DOMCdataSection|\DOMText|mixed
*/
private function serializeDateTimeInterface(
public function serializeDateTimeInterface(
SerializationVisitorInterface $visitor,
\DateTimeInterface $date,
array $type,
Expand Down
23 changes: 23 additions & 0 deletions tests/Fixtures/DateTimeContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures;

use DateTimeInterface;
use JMS\Serializer\Annotation as JMS;

class DateTimeContainer
{
/**
* @var DateTimeInterface
* @JMS\Type("DateTimeInterface<'Y-m-d'>")
* @JMS\SerializedName("custom")
*/
public $customDateTime;

public function __construct(DateTimeInterface $custom)
{
$this->customDateTime = $custom;
}
}
12 changes: 12 additions & 0 deletions tests/Fixtures/DateTimeCustomObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures;

use DateTimeImmutable;

class DateTimeCustomObject extends DateTimeImmutable
{

}
10 changes: 10 additions & 0 deletions tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
use JMS\Serializer\Tests\Fixtures\CurrencyAwarePrice;
use JMS\Serializer\Tests\Fixtures\CustomDeserializationObject;
use JMS\Serializer\Tests\Fixtures\DateTimeArraysObject;
use JMS\Serializer\Tests\Fixtures\DateTimeContainer;
use JMS\Serializer\Tests\Fixtures\DateTimeCustomObject;
use JMS\Serializer\Tests\Fixtures\Discriminator\Car;
use JMS\Serializer\Tests\Fixtures\Discriminator\ImagePost;
use JMS\Serializer\Tests\Fixtures\Discriminator\Moped;
Expand Down Expand Up @@ -643,6 +645,14 @@ public function testArrayListAndMapDifference()
self::assertEquals($this->getContent('array_list_and_map_difference'), $this->serialize($data));
}

public function testCustomDateObject()
{
$data = new DateTimeContainer(new DateTimeCustomObject('2021-09-07'));

self::assertEquals($this->getContent('custom_datetimeinterface'), $this->serialize($data));
}


public function testDateTimeArrays()
{
$data = [
Expand Down
1 change: 1 addition & 0 deletions tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ protected function getContent($key)
$outputs['user_discriminator'] = '{"entityName":"User"}';
$outputs['user_discriminator_extended'] = '{"entityName":"ExtendedUser"}';
$outputs['typed_props'] = '{"id":1,"role":{"id":5},"vehicle":{"type":"car"},"created":"2010-10-01T00:00:00+00:00","updated":"2011-10-01T00:00:00+00:00","tags":["a","b"]}';
$outputs['custom_datetimeinterface'] = '{"custom":"2021-09-07"}';
}

if (!isset($outputs[$key])) {
Expand Down
4 changes: 4 additions & 0 deletions tests/Serializer/xml/custom_datetimeinterface.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<custom><![CDATA[2021-09-07]]></custom>
</result>