Browse Source

Wrap FormRequest autogenerated array examples properly

shalvah 4 years ago
parent
commit
eb6437c1cc

+ 21 - 4
src/Extracting/Strategies/BodyParameters/GetFromFormRequest.php

@@ -426,6 +426,7 @@ class GetFromFormRequest extends Strategy
     {
         $results = [];
         foreach ($bodyParametersFromValidationRules as $name => $details) {
+
             if (isset($results[$name])) {
                 continue;
             }
@@ -435,10 +436,25 @@ class GetFromFormRequest extends Strategy
                 $details['type'] = 'string[]';
             }
 
-            // Change cars.*.dogs.things.*.* with type X to cars.*.dogs.things with type X[][]
-            while (Str::endsWith($name, '.*')) {
-                $details['type'] .= '[]';
-                $name = substr($name, 0, -2);
+            if (Str::endsWith($name, '.*')) {
+                // Wrap array example properly
+                $needsWrapping = !is_array($details['value']);
+
+                $nestingLevel = 0;
+                // Change cars.*.dogs.things.*.* with type X to cars.*.dogs.things with type X[][]
+                while (Str::endsWith($name, '.*')) {
+                    $details['type'] .= '[]';
+                    if ($needsWrapping) {
+                        // Make it two items in each array
+                        $secondItem = $secondValue = $details['setter']();
+                        for ($i = 0; $i < $nestingLevel; $i++) {
+                            $secondItem = [$secondValue];
+                        }
+                        $details['value'] = [$details['value'], $secondItem];
+                    }
+                    $name = substr($name, 0, -2);
+                    $nestingLevel++;
+                }
             }
 
             // Now make sure the field cars.*.dogs exists
@@ -498,6 +514,7 @@ class GetFromFormRequest extends Strategy
                 $details['type'] = 'object';
             }
             $results[$name] = $details;
+
         }
 
         return $results;

+ 2 - 0
tests/Strategies/BodyParameters/GetFromFormRequestTest.php

@@ -108,6 +108,8 @@ class GetFromFormRequestTest extends TestCase
                 'value' => 'Doe',
             ],
         ], $results);
+
+        $this->assertIsArray($results['ids']['value']);
     }
 
     /**