Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array to string conversion error for QueryParam with array for defaults #1047

Closed
williamAlhant opened this issue Jul 28, 2017 · 3 comments
Closed

Comments

@williamAlhant
Copy link

williamAlhant commented Jul 28, 2017

Version : 2.13.2

The following annotation leads to an array to string conversion error :
* @Rest\QueryParam(name="filter", nullable=true, default={})
while the following is fine :
* @Rest\QueryParam(name="filter", nullable=true)

I noticed there is a related issue (#564) which has been fixed. However the fix seems to address the 'requirements' parameter only, and not the 'default' parameter.

@discordier
Copy link
Contributor

Having the same problem here with the following annotation:

 * @FOS\QueryParam(
 *     name="order_by",
 *     nullable=true,
 *     map=true,
 *     default={"name"="ASC", "givenName"="ASC"},
 *     description="Order by fields. Must be an array ie. &order_by[name]=ASC&order_by[givenName]=DESC"
 * )

@discordier
Copy link
Contributor

Hotfixed it on my side with the following patch:

--- Nelmio/ApiDocBundle/Extractor/Handler/FosRestHandler.2.13.2.php
+++ Nelmio/ApiDocBundle/Extractor/Handler/FosRestHandler.php
@@ -40,7 +40,7 @@
                     'readonly'    => false
                 );
                 if ($annot->strict === false) {
-                    $data['default'] = $annot->default;
+                    $data['default'] = $this->handleDefault($annot->default);
                 }
                 $annotation->addParameter($annot->name, $data);
             } elseif ($annot instanceof QueryParam) {
@@ -54,7 +54,7 @@
                     $annotation->addFilter($annot->name, array(
                         'requirement'   => $this->handleRequirements($annot->requirements).((property_exists($annot, 'map') ? $annot->map : $annot->array) ? '[]' : ''),
                         'description'   => $annot->description,
-                        'default'   => $annot->default,
+                        'default'   => $this->handleDefault($annot->default),
                     ));
                 } elseif ($annot->requirements !== null) {
                     $annotation->addFilter($annot->name, array(
@@ -120,6 +120,26 @@
         }
 
         return (string) $requirements;
+    }
+
+    /**
+     * Handle FOSRestBundle default value in order to return a string.
+     *
+     * @param  mixed  $requirements
+     * @return string
+     */
+    private function handleDefault($requirements)
+    {
+        if (is_array($requirements)) {
+            $results = [];
+            foreach ($requirements as $key => $value) {
+                $results[] = $key . ': ' . $value;
+            }
+
+            return implode(', ', $results);
+        }
+
+        return (string) $requirements;
     }
 
     public function inferType($requirement)

The output is not that pretty but at least it does not crash anymore.

@GuilhemN
Copy link
Collaborator

Closing as 2.0 is no longer maintained, please submit a PR if you still want it to be fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants