From dbd207692159dca1eac35ce4267104517a6f3a7e Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Thu, 3 May 2018 13:36:24 -0300 Subject: [PATCH] Normalize description for classes implementing `\DateTimeInterface` --- ModelDescriber/JMSModelDescriber.php | 2 +- ModelDescriber/ObjectModelDescriber.php | 2 +- Tests/Functional/Entity/CustomDateTime.php | 27 ++++++++++++++++++++++ Tests/Functional/Entity/JMSUser.php | 7 ++++++ Tests/Functional/JMSFunctionalTest.php | 4 ++++ 5 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 Tests/Functional/Entity/CustomDateTime.php diff --git a/ModelDescriber/JMSModelDescriber.php b/ModelDescriber/JMSModelDescriber.php index f6026fc..1a06740 100644 --- a/ModelDescriber/JMSModelDescriber.php +++ b/ModelDescriber/JMSModelDescriber.php @@ -155,7 +155,7 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn } elseif (in_array($type, ['double', 'float'])) { $typeDef['type'] = 'number'; $typeDef['format'] = $type; - } elseif (in_array($type, ['DateTime', 'DateTimeImmutable'])) { + } elseif (is_subclass_of($type, \DateTimeInterface::class)) { $typeDef['type'] = 'string'; $typeDef['format'] = 'date-time'; } else { diff --git a/ModelDescriber/ObjectModelDescriber.php b/ModelDescriber/ObjectModelDescriber.php index 4bb4b5e..55b6340 100644 --- a/ModelDescriber/ObjectModelDescriber.php +++ b/ModelDescriber/ObjectModelDescriber.php @@ -106,7 +106,7 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar $property->setType('number'); $property->setFormat('float'); } elseif (Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType()) { - if (in_array($type->getClassName(), ['DateTime', 'DateTimeImmutable'])) { + if (is_subclass_of($type->getClassName(), \DateTimeInterface::class)) { $property->setType('string'); $property->setFormat('date-time'); } else { diff --git a/Tests/Functional/Entity/CustomDateTime.php b/Tests/Functional/Entity/CustomDateTime.php new file mode 100644 index 0000000..7f027c5 --- /dev/null +++ b/Tests/Functional/Entity/CustomDateTime.php @@ -0,0 +1,27 @@ + + */ +class CustomDateTime extends \DateTime +{ + /** + * @Serializer\Type("string") + * @Serializer\Expose + * @Serializer\SerializedName("format") + */ + private $format; +} diff --git a/Tests/Functional/Entity/JMSUser.php b/Tests/Functional/Entity/JMSUser.php index ee69223..f619a7e 100644 --- a/Tests/Functional/Entity/JMSUser.php +++ b/Tests/Functional/Entity/JMSUser.php @@ -91,6 +91,13 @@ class JMSUser * @Serializer\Expose */ private $favoriteDates; + + /** + * @Serializer\Type(Nelmio\ApiDocBundle\Tests\Functional\Entity\CustomDateTime::class) + * @Serializer\Expose + */ + private $customDate; + /** * @Serializer\Type("integer") * @Serializer\Expose diff --git a/Tests/Functional/JMSFunctionalTest.php b/Tests/Functional/JMSFunctionalTest.php index 2af7947..6c9f2c7 100644 --- a/Tests/Functional/JMSFunctionalTest.php +++ b/Tests/Functional/JMSFunctionalTest.php @@ -67,6 +67,10 @@ class JMSFunctionalTest extends WebTestCase 'format' => 'date-time', ], ], + 'custom_date' => [ + 'type' => 'string', + 'format' => 'date-time', + ], 'best_friend' => [ '$ref' => '#/definitions/User', ],