Merge pull request #1313 from phansys/modeldescriber

Normalize description for classes implementing `\DateTimeInterface`
This commit is contained in:
Guilhem N 2018-05-05 23:20:03 +02:00 committed by GitHub
commit 304b3f5816
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 2 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -0,0 +1,27 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\Functional\Entity;
use JMS\Serializer\Annotation as Serializer;
/**
* @author Javier Spagnoletti <phansys@gmail.com>
*/
class CustomDateTime extends \DateTime
{
/**
* @Serializer\Type("string")
* @Serializer\Expose
* @Serializer\SerializedName("format")
*/
private $format;
}

View File

@ -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

View File

@ -67,6 +67,10 @@ class JMSFunctionalTest extends WebTestCase
'format' => 'date-time',
],
],
'custom_date' => [
'type' => 'string',
'format' => 'date-time',
],
'best_friend' => [
'$ref' => '#/definitions/User',
],