From 306f2c47cf6e3de77c6b9e029f77726197403a21 Mon Sep 17 00:00:00 2001
From: Damien Alexandre <mobman02@gmail.com>
Date: Fri, 8 Jan 2016 13:04:25 +0100
Subject: [PATCH] Fix Array to string conversion error on array requirements

Fix https://github.com/nelmio/NelmioApiDocBundle/issues/773
See https://github.com/FriendsOfSymfony/FOSRestBundle/pull/1015
---
 Extractor/Handler/FosRestHandler.php           |  4 ++++
 Tests/Extractor/Handler/FosRestHandlerTest.php | 14 ++++++++++++++
 Tests/Fixtures/Controller/TestController.php   |  8 ++++++++
 Tests/Fixtures/app/config/routing.yml          |  5 +++++
 4 files changed, 31 insertions(+)

diff --git a/Extractor/Handler/FosRestHandler.php b/Extractor/Handler/FosRestHandler.php
index 7c22694..056230c 100644
--- a/Extractor/Handler/FosRestHandler.php
+++ b/Extractor/Handler/FosRestHandler.php
@@ -83,6 +83,10 @@ class FosRestHandler implements HandlerInterface
             return substr($class, strrpos($class, '\\')+1);
         }
 
+        if (is_array($requirements) && isset($requirements['rule'])) {
+            return (string) $requirements['rule'];
+        }
+
         return (string) $requirements;
     }
 
diff --git a/Tests/Extractor/Handler/FosRestHandlerTest.php b/Tests/Extractor/Handler/FosRestHandlerTest.php
index 500cc31..ea6d252 100644
--- a/Tests/Extractor/Handler/FosRestHandlerTest.php
+++ b/Tests/Extractor/Handler/FosRestHandlerTest.php
@@ -175,4 +175,18 @@ class FosRestHandlerTest extends WebTestCase
         $this->assertArrayHasKey('dataType', $parameter);
         $this->assertEquals('string[]', $parameter['dataType']);
     }
+
+    public function testWithRequestParamArrayRequirements()
+    {
+        $container  = $this->getContainer();
+        $extractor  = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
+        $annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::routeWithQueryParamArrayRequirementsAction', 'test_route_29');
+
+        $this->assertNotNull($annotation);
+        $filters = $annotation->getFilters();
+
+        $this->assertArrayHasKey('param1', $filters);
+        $this->assertArrayHasKey('requirement', $filters['param1']);
+        $this->assertEquals('regexp', $filters['param1']['requirement']);
+    }
 }
diff --git a/Tests/Fixtures/Controller/TestController.php b/Tests/Fixtures/Controller/TestController.php
index d316dd0..a60ecc9 100644
--- a/Tests/Fixtures/Controller/TestController.php
+++ b/Tests/Fixtures/Controller/TestController.php
@@ -398,4 +398,12 @@ class TestController
     public function routeWithHostAction()
     {
     }
+
+    /**
+     * @ApiDoc()
+     * @QueryParam(name="param1", requirements={"rule": "regexp", "error_message": "warning"}, description="Param1 description.")
+     */
+    public function routeWithQueryParamArrayRequirementsAction()
+    {
+    }
 }
diff --git a/Tests/Fixtures/app/config/routing.yml b/Tests/Fixtures/app/config/routing.yml
index 6e8c479..fe077c0 100644
--- a/Tests/Fixtures/app/config/routing.yml
+++ b/Tests/Fixtures/app/config/routing.yml
@@ -238,3 +238,8 @@ test_route_28:
     requirements:
         domain: "%domain_dev%|%domain_prod%"
     defaults: { _controller: NelmioApiDocTestBundle:Test:routeWithHost, domain: "%domain_dev%", _format: json }
+
+test_route_29:
+    path: /z-query-param-array-requirements
+    methods: [GET]
+    defaults: { _controller: NelmioApiDocTestBundle:Test:routeWithQueryParamArrayRequirementsAction }