From 7f9b00d62007dd397aa5622072a8e51d24d14571 Mon Sep 17 00:00:00 2001 From: Thomas Lallement Date: Tue, 16 Mar 2021 10:40:12 +0100 Subject: [PATCH] Code improvements and add unit test --- RouteDescriber/FosRestDescriber.php | 7 ++++++- Tests/Functional/Controller/FOSRestController.php | 3 +++ Tests/Functional/FOSRestTest.php | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/RouteDescriber/FosRestDescriber.php b/RouteDescriber/FosRestDescriber.php index cc07217..4b9f8fc 100644 --- a/RouteDescriber/FosRestDescriber.php +++ b/RouteDescriber/FosRestDescriber.php @@ -107,7 +107,12 @@ final class FosRestDescriber implements RouteDescriberInterface if ($requirements instanceof Constraint && !$requirements instanceof Regex) { if ($requirements instanceof DateTime) { - return 'date-time'; + // As defined per RFC3339 + if ($requirements->format === 'Y-m-d\TH:i:s') { + return 'date-time'; + } else if ($requirements->format === 'Y-m-d') { + return 'date'; + } } $reflectionClass = new \ReflectionClass($requirements); diff --git a/Tests/Functional/Controller/FOSRestController.php b/Tests/Functional/Controller/FOSRestController.php index ada65d4..ab2c66c 100644 --- a/Tests/Functional/Controller/FOSRestController.php +++ b/Tests/Functional/Controller/FOSRestController.php @@ -14,6 +14,7 @@ namespace Nelmio\ApiDocBundle\Tests\Functional\Controller; use FOS\RestBundle\Controller\Annotations\QueryParam; use FOS\RestBundle\Controller\Annotations\RequestParam; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\Validator\Constraints\DateTime; use Symfony\Component\Validator\Constraints\IsTrue; use Symfony\Component\Validator\Constraints\Regex; @@ -28,6 +29,8 @@ class FOSRestController * @QueryParam(name="mapped", map=true) * @RequestParam(name="Barraa", key="bar", requirements="\d+") * @RequestParam(name="baz", requirements=@IsTrue) + * @RequestParam(name="datetime", requirements=@DateTime("Y-m-d\TH:i:s")) + * @RequestParam(name="date", requirements=@DateTime("Y-m-d")) */ public function fosrestAction() { diff --git a/Tests/Functional/FOSRestTest.php b/Tests/Functional/FOSRestTest.php index 7c9c5a3..a67cb7d 100644 --- a/Tests/Functional/FOSRestTest.php +++ b/Tests/Functional/FOSRestTest.php @@ -50,6 +50,12 @@ class FOSRestTest extends WebTestCase $this->assertEquals(OA\UNDEFINED, $bazProperty->pattern); $this->assertEquals('IsTrue', $bazProperty->format); + $barProperty = $this->getProperty($bodySchema, 'datetime'); + $this->assertEquals('date-time', $barProperty->format); + + $barProperty = $this->getProperty($bodySchema, 'date'); + $this->assertEquals('date', $barProperty->format); + // The _format path attribute should be removed $this->assertNotHasParameter('_format', 'path', $operation); }