Skip to content

Commit

Permalink
Fix Array to string conversion error on array requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
zhukovra committed Feb 1, 2016
1 parent 5d9c47b commit f2fc6b4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
28 changes: 28 additions & 0 deletions Extractor/Handler/FosRestHandler.php
Expand Up @@ -87,6 +87,34 @@ private function handleRequirements($requirements)
return (string) $requirements['rule'];
}

if (is_array($requirements) && array_key_exists(0, $requirements)) {

$output = array();

foreach ($requirements as $req) {

if (is_object($req) && $req instanceof Constraint) {
if ($req instanceof Regex) {
$output[] = $req->getHtmlPattern();
} else {
$class = get_class($req);
$output[] = substr($class, strrpos($class, '\\')+1);
}

}

if (is_array($req)) {
if (array_key_exists('_format', $req)) {
$output[] = 'Format: '.$req['_format'];
} else if (isset($req['rule'])) {
$output[] = $req['rule'];
}
}
}

return implode(', ', $output);
}

return (string) $requirements;
}

Expand Down
14 changes: 14 additions & 0 deletions Tests/Extractor/Handler/FosRestHandlerTest.php
Expand Up @@ -189,4 +189,18 @@ public function testWithRequestParamArrayRequirements()
$this->assertArrayHasKey('requirement', $filters['param1']);
$this->assertEquals('regexp', $filters['param1']['requirement']);
}

public function testWithRequestParamPlainArrayRequirements()
{
$container = $this->getContainer();
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
$annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::routeWithQueryParamPlainArrayRequirementsAction', 'test_route_30');

$this->assertNotNull($annotation);
$filters = $annotation->getFilters();

$this->assertArrayHasKey('param1', $filters);
$this->assertArrayHasKey('requirement', $filters['param1']);
$this->assertEquals('NotNull, NotBlank', $filters['param1']['requirement']);
}
}
12 changes: 10 additions & 2 deletions Tests/Fixtures/Controller/TestController.php
Expand Up @@ -18,7 +18,7 @@
use Nelmio\ApiDocBundle\Tests\Fixtures\RequestParamHelper;
use Nelmio\ApiDocBundle\Util\LegacyFormHelper;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints as Assert;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;

Expand Down Expand Up @@ -150,7 +150,7 @@ public function zActionWithQueryParamNoDefaultAction()

/**
* @ApiDoc()
* @QueryParam(name="mail", requirements=@Email, description="Email of someone.")
* @QueryParam(name="mail", requirements=@Assert\Email, description="Email of someone.")
*/
public function zActionWithConstraintAsRequirements()
{
Expand Down Expand Up @@ -406,4 +406,12 @@ public function routeWithHostAction()
public function routeWithQueryParamArrayRequirementsAction()
{
}

/**
* @ApiDoc()
* @QueryParam(name="param1", requirements={@Assert\NotNull(), @Assert\NotBlank()}, description="Param1 description.")
*/
public function routeWithQueryParamPlainArrayRequirementsAction()
{
}
}
5 changes: 5 additions & 0 deletions Tests/Fixtures/app/config/routing.yml
Expand Up @@ -243,3 +243,8 @@ test_route_29:
path: /z-query-param-array-requirements
methods: [GET]
defaults: { _controller: NelmioApiDocTestBundle:Test:routeWithQueryParamArrayRequirementsAction }

test_route_30:
path: /z-query-param-plain-array-requirements
methods: [GET]
defaults: { _controller: NelmioApiDocTestBundle:Test:routeWithQueryParamPlainArrayRequirementsAction }

0 comments on commit f2fc6b4

Please sign in to comment.