From f2fc6b47c041447c606b98cf55ecc6b71d7a8b26 Mon Sep 17 00:00:00 2001
From: Zhukov Roman <zhukov.roman@gmail.com>
Date: Mon, 1 Feb 2016 14:33:17 +0300
Subject: [PATCH] Fix Array to string conversion error on array requirements
 Fixes #564

---
 Extractor/Handler/FosRestHandler.php          | 28 +++++++++++++++++++
 .../Extractor/Handler/FosRestHandlerTest.php  | 14 ++++++++++
 Tests/Fixtures/Controller/TestController.php  | 12 ++++++--
 Tests/Fixtures/app/config/routing.yml         |  5 ++++
 4 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/Extractor/Handler/FosRestHandler.php b/Extractor/Handler/FosRestHandler.php
index 056230c..8bddabe 100644
--- a/Extractor/Handler/FosRestHandler.php
+++ b/Extractor/Handler/FosRestHandler.php
@@ -87,6 +87,34 @@ class FosRestHandler implements HandlerInterface
             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;
     }
 
diff --git a/Tests/Extractor/Handler/FosRestHandlerTest.php b/Tests/Extractor/Handler/FosRestHandlerTest.php
index ea6d252..3fcac87 100644
--- a/Tests/Extractor/Handler/FosRestHandlerTest.php
+++ b/Tests/Extractor/Handler/FosRestHandlerTest.php
@@ -189,4 +189,18 @@ class FosRestHandlerTest extends WebTestCase
         $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']);
+    }
 }
diff --git a/Tests/Fixtures/Controller/TestController.php b/Tests/Fixtures/Controller/TestController.php
index a60ecc9..9bc4fd9 100644
--- a/Tests/Fixtures/Controller/TestController.php
+++ b/Tests/Fixtures/Controller/TestController.php
@@ -18,7 +18,7 @@ use Nelmio\ApiDocBundle\Tests\Fixtures\DependencyTypePath;
 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;
 
@@ -150,7 +150,7 @@ class TestController
 
     /**
      * @ApiDoc()
-     * @QueryParam(name="mail", requirements=@Email, description="Email of someone.")
+     * @QueryParam(name="mail", requirements=@Assert\Email, description="Email of someone.")
      */
     public function zActionWithConstraintAsRequirements()
     {
@@ -406,4 +406,12 @@ class TestController
     public function routeWithQueryParamArrayRequirementsAction()
     {
     }
+
+    /**
+     * @ApiDoc()
+     * @QueryParam(name="param1", requirements={@Assert\NotNull(), @Assert\NotBlank()}, description="Param1 description.")
+     */
+    public function routeWithQueryParamPlainArrayRequirementsAction()
+    {
+    }
 }
diff --git a/Tests/Fixtures/app/config/routing.yml b/Tests/Fixtures/app/config/routing.yml
index fe077c0..0f3628e 100644
--- a/Tests/Fixtures/app/config/routing.yml
+++ b/Tests/Fixtures/app/config/routing.yml
@@ -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 }