mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 15:51:48 +03:00
Merge pull request #365 from pyrech/master
Fix #357 - doc broken with Validator Constraints in FOSRestBundle requirements
This commit is contained in:
commit
968a162544
@ -14,41 +14,64 @@ namespace Nelmio\ApiDocBundle\Extractor\Handler;
|
||||
use Nelmio\ApiDocBundle\Extractor\HandlerInterface;
|
||||
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
||||
use Symfony\Component\Routing\Route;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\Constraints\Regex;
|
||||
use FOS\RestBundle\Controller\Annotations\RequestParam;
|
||||
use FOS\RestBundle\Controller\Annotations\QueryParam;
|
||||
|
||||
class FosRestHandler implements HandlerInterface
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
|
||||
{
|
||||
foreach ($annotations as $annot) {
|
||||
if ($annot instanceof RequestParam) {
|
||||
$annotation->addParameter($annot->name, array(
|
||||
'required' => $annot->strict && $annot->default === null,
|
||||
'dataType' => $annot->requirements,
|
||||
'dataType' => $this->handleRequirements($annot->requirements),
|
||||
'description' => $annot->description,
|
||||
'readonly' => false
|
||||
));
|
||||
} elseif ($annot instanceof QueryParam) {
|
||||
if ($annot->strict && $annot->nullable === false && $annot->default === null) {
|
||||
$annotation->addRequirement($annot->name, array(
|
||||
'requirement' => $annot->requirements,
|
||||
'requirement' => $this->handleRequirements($annot->requirements),
|
||||
'dataType' => '',
|
||||
'description' => $annot->description,
|
||||
));
|
||||
} elseif ($annot->default !== null) {
|
||||
$annotation->addFilter($annot->name, array(
|
||||
'requirement' => $annot->requirements,
|
||||
'requirement' => $this->handleRequirements($annot->requirements),
|
||||
'description' => $annot->description,
|
||||
'default' => $annot->default,
|
||||
));
|
||||
} else {
|
||||
$annotation->addFilter($annot->name, array(
|
||||
'requirement' => $annot->requirements,
|
||||
'requirement' => $this->handleRequirements($annot->requirements),
|
||||
'description' => $annot->description,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle FOSRestBundle requirements in order to return a string.
|
||||
*
|
||||
* @param mixed $requirements
|
||||
* @return string
|
||||
*/
|
||||
private function handleRequirements($requirements)
|
||||
{
|
||||
if (is_object($requirements) && $requirements instanceof Constraint) {
|
||||
if ($requirements instanceof Regex) {
|
||||
return $requirements->getHtmlPattern();
|
||||
}
|
||||
$class = get_class($requirements);
|
||||
return substr($class, strrpos($class, '\\')+1);
|
||||
}
|
||||
return (string)$requirements;
|
||||
}
|
||||
}
|
||||
|
@ -86,5 +86,23 @@ class FosRestHandlerTest extends WebTestCase
|
||||
|
||||
$this->assertArrayNotHasKey('default', $filter);
|
||||
}
|
||||
|
||||
public function testGetWithConstraintAsRequirements()
|
||||
{
|
||||
$container = $this->getContainer();
|
||||
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
|
||||
$annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::zActionWithConstraintAsRequirements', 'test_route_21');
|
||||
|
||||
$this->assertNotNull($annotation);
|
||||
|
||||
$filters = $annotation->getFilters();
|
||||
$this->assertCount(1, $filters);
|
||||
$this->assertArrayHasKey('mail', $filters);
|
||||
|
||||
$filter = $filters['mail'];
|
||||
|
||||
$this->assertArrayHasKey('requirement', $filter);
|
||||
$this->assertEquals($filter['requirement'], 'Email');
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ use FOS\RestBundle\Controller\Annotations\QueryParam;
|
||||
use FOS\RestBundle\Controller\Annotations\RequestParam;
|
||||
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Validator\Constraints\Email;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
|
||||
|
||||
class TestController
|
||||
@ -129,6 +130,14 @@ class TestController
|
||||
public function zActionWithQueryParamNoDefaultAction()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @ApiDoc()
|
||||
* @QueryParam(name="mail", requirements=@Email, description="Email of someone.")
|
||||
*/
|
||||
public function zActionWithConstraintAsRequirements()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @ApiDoc(
|
||||
|
@ -149,3 +149,9 @@ test_route_private:
|
||||
test_route_exclusive:
|
||||
pattern: /exclusive
|
||||
defaults: { _controller: NelmioApiDocTestBundle:Test:exclusive }
|
||||
|
||||
test_route_21:
|
||||
pattern: /z-action-with-constraint-requirements
|
||||
defaults: { _controller: NelmioApiDocTestBundle:Test:zActionWithConstraintAsRequirements }
|
||||
requirements:
|
||||
_method: GET
|
||||
|
Loading…
x
Reference in New Issue
Block a user