diff --git a/src/Extracting/Generator.php b/src/Extracting/Generator.php index 1c57d6c3..cbafc2ff 100644 --- a/src/Extracting/Generator.php +++ b/src/Extracting/Generator.php @@ -314,11 +314,17 @@ public static function setObject(array &$results, string $path, $value, array $s } 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); diff --git a/tests/Unit/GeneratorTest.php b/tests/Unit/GeneratorTest.php index 10154fd1..11541dfc 100644 --- a/tests/Unit/GeneratorTest.php +++ b/tests/Unit/GeneratorTest.php @@ -153,6 +153,14 @@ public function clean_can_properly_parse_a_body_array() 'type' => 'string', 'value' => 'hoho', ], + '[].key3.key2' => [ + 'type' => 'array', + 'value' => [], + ], + '[].key3.key2[].subkey1' => [ + 'type' => 'string', + 'value' => 'haha', + ], ]; $cleanBodyParameters = Generator::cleanParams($parameters); @@ -163,8 +171,13 @@ public function clean_can_properly_parse_a_body_array() 'key2' => 77, 'key3' => [ 'key1' => [ - 'objkey1' => 'hoho', - ] + 'objkey1' => 'hoho', + ], + 'key2' => [ + [ + 'subkey1' => 'haha', + ] + ], ] ], ], $cleanBodyParameters);