diff --git a/RouteDescriber/FosRestDescriber.php b/RouteDescriber/FosRestDescriber.php index 8f3e814..1dede6b 100644 --- a/RouteDescriber/FosRestDescriber.php +++ b/RouteDescriber/FosRestDescriber.php @@ -18,6 +18,7 @@ use Nelmio\ApiDocBundle\OpenApiPhp\Util; use OpenApi\Annotations as OA; use Symfony\Component\Routing\Route; use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Constraints\DateTime; use Symfony\Component\Validator\Constraints\Regex; final class FosRestDescriber implements RouteDescriberInterface @@ -104,6 +105,19 @@ final class FosRestDescriber implements RouteDescriberInterface private function getFormat($requirements) { if ($requirements instanceof Constraint && !$requirements instanceof Regex) { + if ($requirements instanceof DateTime) { + // As defined per RFC3339 + if (\DateTime::RFC3339 === $requirements->format || 'c' === $requirements->format) { + return 'date-time'; + } + + if ('Y-m-d' === $requirements->format) { + return 'date'; + } + + return null; + } + $reflectionClass = new \ReflectionClass($requirements); return $reflectionClass->getShortName(); diff --git a/Tests/Functional/Controller/FOSRestController.php b/Tests/Functional/Controller/FOSRestController.php index ada65d4..772af46 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,10 @@ 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:sP")) + * @RequestParam(name="datetimeAlt", requirements=@DateTime("c")) + * @RequestParam(name="datetimeNoFormat", requirements=@DateTime()) + * @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..66917b8 100644 --- a/Tests/Functional/FOSRestTest.php +++ b/Tests/Functional/FOSRestTest.php @@ -50,6 +50,18 @@ class FOSRestTest extends WebTestCase $this->assertEquals(OA\UNDEFINED, $bazProperty->pattern); $this->assertEquals('IsTrue', $bazProperty->format); + $dateTimeProperty = $this->getProperty($bodySchema, 'datetime'); + $this->assertEquals('date-time', $dateTimeProperty->format); + + $dateTimeAltProperty = $this->getProperty($bodySchema, 'datetimeAlt'); + $this->assertEquals('date-time', $dateTimeAltProperty->format); + + $dateTimeNoFormatProperty = $this->getProperty($bodySchema, 'datetimeNoFormat'); + $this->assertEquals(OA\UNDEFINED, $dateTimeNoFormatProperty->format); + + $dateProperty = $this->getProperty($bodySchema, 'date'); + $this->assertEquals('date', $dateProperty->format); + // The _format path attribute should be removed $this->assertNotHasParameter('_format', 'path', $operation); }