mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 23:59:26 +03:00
Refactored code to achieve the logic we want for pattern and format.
This commit is contained in:
parent
5a1dfa6ead
commit
e83856ad9a
@ -52,37 +52,44 @@ final class FosRestDescriber implements RouteDescriberInterface
|
||||
$parameter->setDescription($annotation->description);
|
||||
}
|
||||
|
||||
$normalizedRequirements = $this->normalizeRequirements($annotation->requirements);
|
||||
if (null !== $normalizedRequirements) {
|
||||
if ($normalizedRequirements instanceof Constraint) {
|
||||
if ($normalizedRequirements instanceof Regex) {
|
||||
$format = $normalizedRequirements->getHtmlPattern();
|
||||
} else {
|
||||
$reflectionClass = new \ReflectionClass($normalizedRequirements);
|
||||
$format = $reflectionClass->getShortName();
|
||||
}
|
||||
$pattern = $this->getPattern($annotation->requirements);
|
||||
if (null !== $pattern) {
|
||||
$parameter->setPattern($pattern);
|
||||
}
|
||||
|
||||
$parameter->setFormat($format);
|
||||
} else {
|
||||
$parameter->setPattern($normalizedRequirements);
|
||||
}
|
||||
$format = $this->getFormat($annotation->requirements);
|
||||
if (null !== $format) {
|
||||
$parameter->setFormat($format);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizeRequirements($requirements)
|
||||
private function getPattern($requirements)
|
||||
{
|
||||
// if pattern
|
||||
if (is_array($requirements) && isset($requirements['rule'])) {
|
||||
return (string) $requirements['rule'];
|
||||
}
|
||||
|
||||
if (is_string($requirements)) {
|
||||
return $requirements;
|
||||
}
|
||||
// if custom constraint
|
||||
if ($requirements instanceof Constraint) {
|
||||
return $requirements;
|
||||
|
||||
if ($requirements instanceof Regex) {
|
||||
return $requirements->getHtmlPattern();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function getFormat($requirements)
|
||||
{
|
||||
if ($requirements instanceof Constraint && !$requirements instanceof Regex) {
|
||||
$reflectionClass = new \ReflectionClass($requirements);
|
||||
|
||||
return $reflectionClass->getShortName();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -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\IsTrue;
|
||||
use Symfony\Component\Validator\Constraints\Regex;
|
||||
|
||||
/**
|
||||
@ -110,6 +111,7 @@ class ApiController
|
||||
* @Route("/fosrest.{_format}", methods={"POST"})
|
||||
* @QueryParam(name="foo", requirements=@Regex("/^\d+$/"))
|
||||
* @RequestParam(name="bar", requirements="\d+")
|
||||
* @RequestParam(name="baz", requirements=@IsTrue)
|
||||
*/
|
||||
public function fosrestAction()
|
||||
{
|
||||
|
@ -124,17 +124,23 @@ class FunctionalTest extends WebTestCase
|
||||
$parameters = $operation->getParameters();
|
||||
$this->assertTrue($parameters->has('foo', 'query'));
|
||||
$this->assertTrue($parameters->has('bar', 'formData'));
|
||||
$this->assertTrue($parameters->has('baz', 'formData'));
|
||||
|
||||
$fooParameter = $parameters->get('foo', 'query');
|
||||
$this->assertNotNull($fooParameter->getFormat());
|
||||
$this->assertEquals('\d+', $fooParameter->getFormat());
|
||||
$this->assertNull($fooParameter->getPattern());
|
||||
$this->assertNotNull($fooParameter->getPattern());
|
||||
$this->assertEquals('\d+', $fooParameter->getPattern());
|
||||
$this->assertNull($fooParameter->getFormat());
|
||||
|
||||
$barParameter = $parameters->get('bar', 'formData');
|
||||
$this->assertNotNull($barParameter->getPattern());
|
||||
$this->assertEquals('\d+', $barParameter->getPattern());
|
||||
$this->assertNull($barParameter->getFormat());
|
||||
|
||||
$bazParameter = $parameters->get('baz', 'formData');
|
||||
$this->assertNotNull($bazParameter->getFormat());
|
||||
$this->assertEquals('IsTrue', $bazParameter->getFormat());
|
||||
$this->assertNull($bazParameter->getPattern());
|
||||
|
||||
// The _format path attribute should be removed
|
||||
$this->assertFalse($parameters->has('_format', 'path'));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user