mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 23:59:26 +03:00
Added support for "format" field for Constraint objects.
This commit is contained in:
parent
499886deab
commit
5a1dfa6ead
@ -54,7 +54,18 @@ final class FosRestDescriber implements RouteDescriberInterface
|
|||||||
|
|
||||||
$normalizedRequirements = $this->normalizeRequirements($annotation->requirements);
|
$normalizedRequirements = $this->normalizeRequirements($annotation->requirements);
|
||||||
if (null !== $normalizedRequirements) {
|
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 custom constraint
|
||||||
if ($requirements instanceof Constraint) {
|
if ($requirements instanceof Constraint) {
|
||||||
if ($requirements instanceof Regex) {
|
return $requirements;
|
||||||
return $requirements->getHtmlPattern();
|
|
||||||
}
|
|
||||||
|
|
||||||
$reflectionClass = new \ReflectionClass($requirements);
|
|
||||||
|
|
||||||
return $reflectionClass->getShortName();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ use Nelmio\ApiDocBundle\Tests\Functional\Form\DummyType;
|
|||||||
use Nelmio\ApiDocBundle\Tests\Functional\Form\UserType;
|
use Nelmio\ApiDocBundle\Tests\Functional\Form\UserType;
|
||||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||||
use Swagger\Annotations as SWG;
|
use Swagger\Annotations as SWG;
|
||||||
|
use Symfony\Component\Validator\Constraints\Regex;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Route("/api")
|
* @Route("/api")
|
||||||
@ -107,7 +108,7 @@ class ApiController
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @Route("/fosrest.{_format}", methods={"POST"})
|
* @Route("/fosrest.{_format}", methods={"POST"})
|
||||||
* @QueryParam(name="foo")
|
* @QueryParam(name="foo", requirements=@Regex("/^\d+$/"))
|
||||||
* @RequestParam(name="bar", requirements="\d+")
|
* @RequestParam(name="bar", requirements="\d+")
|
||||||
*/
|
*/
|
||||||
public function fosrestAction()
|
public function fosrestAction()
|
||||||
|
@ -125,9 +125,15 @@ class FunctionalTest extends WebTestCase
|
|||||||
$this->assertTrue($parameters->has('foo', 'query'));
|
$this->assertTrue($parameters->has('foo', 'query'));
|
||||||
$this->assertTrue($parameters->has('bar', 'formData'));
|
$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');
|
$barParameter = $parameters->get('bar', 'formData');
|
||||||
$this->assertNotNull($barParameter->getPattern());
|
$this->assertNotNull($barParameter->getPattern());
|
||||||
$this->assertEquals('\d+', $barParameter->getPattern());
|
$this->assertEquals('\d+', $barParameter->getPattern());
|
||||||
|
$this->assertNull($barParameter->getFormat());
|
||||||
|
|
||||||
// The _format path attribute should be removed
|
// The _format path attribute should be removed
|
||||||
$this->assertFalse($parameters->has('_format', 'path'));
|
$this->assertFalse($parameters->has('_format', 'path'));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user