From 5a1dfa6eadaf94080317fe5d00033394e1f48160 Mon Sep 17 00:00:00 2001 From: Zarko Stankovic Date: Thu, 15 Mar 2018 19:09:51 +0100 Subject: [PATCH] Added support for "format" field for Constraint objects. --- RouteDescriber/FosRestDescriber.php | 21 ++++++++++++------- Tests/Functional/Controller/ApiController.php | 3 ++- Tests/Functional/FunctionalTest.php | 6 ++++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/RouteDescriber/FosRestDescriber.php b/RouteDescriber/FosRestDescriber.php index fef4b18..3984332 100644 --- a/RouteDescriber/FosRestDescriber.php +++ b/RouteDescriber/FosRestDescriber.php @@ -54,7 +54,18 @@ final class FosRestDescriber implements RouteDescriberInterface $normalizedRequirements = $this->normalizeRequirements($annotation->requirements); if (null !== $normalizedRequirements) { - $parameter->setPattern($normalizedRequirements); + if ($normalizedRequirements instanceof Constraint) { + if ($normalizedRequirements instanceof Regex) { + $format = $normalizedRequirements->getHtmlPattern(); + } else { + $reflectionClass = new \ReflectionClass($normalizedRequirements); + $format = $reflectionClass->getShortName(); + } + + $parameter->setFormat($format); + } else { + $parameter->setPattern($normalizedRequirements); + } } } } @@ -71,13 +82,7 @@ final class FosRestDescriber implements RouteDescriberInterface } // if custom constraint if ($requirements instanceof Constraint) { - if ($requirements instanceof Regex) { - return $requirements->getHtmlPattern(); - } - - $reflectionClass = new \ReflectionClass($requirements); - - return $reflectionClass->getShortName(); + return $requirements; } } } diff --git a/Tests/Functional/Controller/ApiController.php b/Tests/Functional/Controller/ApiController.php index 8ec78ba..9b8a160 100644 --- a/Tests/Functional/Controller/ApiController.php +++ b/Tests/Functional/Controller/ApiController.php @@ -23,6 +23,7 @@ use Nelmio\ApiDocBundle\Tests\Functional\Form\DummyType; use Nelmio\ApiDocBundle\Tests\Functional\Form\UserType; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Swagger\Annotations as SWG; +use Symfony\Component\Validator\Constraints\Regex; /** * @Route("/api") @@ -107,7 +108,7 @@ class ApiController /** * @Route("/fosrest.{_format}", methods={"POST"}) - * @QueryParam(name="foo") + * @QueryParam(name="foo", requirements=@Regex("/^\d+$/")) * @RequestParam(name="bar", requirements="\d+") */ public function fosrestAction() diff --git a/Tests/Functional/FunctionalTest.php b/Tests/Functional/FunctionalTest.php index 2069ed3..7684a83 100644 --- a/Tests/Functional/FunctionalTest.php +++ b/Tests/Functional/FunctionalTest.php @@ -125,9 +125,15 @@ class FunctionalTest extends WebTestCase $this->assertTrue($parameters->has('foo', 'query')); $this->assertTrue($parameters->has('bar', 'formData')); + $fooParameter = $parameters->get('foo', 'query'); + $this->assertNotNull($fooParameter->getFormat()); + $this->assertEquals('\d+', $fooParameter->getFormat()); + $this->assertNull($fooParameter->getPattern()); + $barParameter = $parameters->get('bar', 'formData'); $this->assertNotNull($barParameter->getPattern()); $this->assertEquals('\d+', $barParameter->getPattern()); + $this->assertNull($barParameter->getFormat()); // The _format path attribute should be removed $this->assertFalse($parameters->has('_format', 'path'));