Переглянути джерело

Properly support child array parameters in body arrays, and make the comments clearer.

Rick Sharp 3 роки тому
батько
коміт
05aaba1877
2 змінених файлів з 25 додано та 6 видалено
  1. 10 4
      src/Extracting/Generator.php
  2. 15 2
      tests/Unit/GeneratorTest.php

+ 10 - 4
src/Extracting/Generator.php

@@ -314,11 +314,17 @@ class Generator
         }
 
         if (empty($baseNameInOriginalParams)) {
-            // This indicates that the body is an array of objects, so each parameter should be a key in the first object in that array
+            // If this is empty, it indicates that the body is an array of objects. (i.e. "[].param")
+            // Therefore, each parameter should be an element of the first object in that array.
             $results[0][$paramName] = $value;
-        } elseif (array_key_exists(0, $results)) {
-            // The body is an array of objects, so every parameter must be an element of the array object.
-            $dotPath = '0.'.substr($path, 3);
+        } elseif (Str::startsWith($path, '[]')) {
+            // If the body is an array, then any top level parameters (i.e. "[].param") would have been handled by the previous block
+            // Therefore, we assume that this is a child parameter (i.e. "[].parent.child" or "[].parent[].child"
+
+            // Remove the top-level array brackets
+            $dotPath = substr($path, 3);
+            // Use correct dot notation for any child arrays
+            $dotPath = '0.' . str_replace('[]', '.0', $dotPath);
             Arr::set($results, $dotPath, $value);
         } elseif (Arr::has($source, $baseNameInOriginalParams)) {
             $parentData = Arr::get($source, $baseNameInOriginalParams);

+ 15 - 2
tests/Unit/GeneratorTest.php

@@ -153,6 +153,14 @@ class GeneratorTest extends TestCase
                 'type' => 'string',
                 'value' => 'hoho',
             ],
+            '[].key3.key2' => [
+                'type' => 'array',
+                'value' => [],
+            ],
+            '[].key3.key2[].subkey1' => [
+                'type' => 'string',
+                'value' => 'haha',
+            ],
         ];
 
         $cleanBodyParameters = Generator::cleanParams($parameters);
@@ -163,8 +171,13 @@ class GeneratorTest extends TestCase
                 'key2' => 77,
                 'key3' => [
                     'key1' => [
-                      'objkey1' => 'hoho',
-                    ]
+                        'objkey1' => 'hoho',
+                    ],
+                    'key2' => [
+                        [
+                            'subkey1' => 'haha',
+                        ]
+                    ],
                 ]
             ],
         ], $cleanBodyParameters);