Skip to content

Commit

Permalink
Properly support child array parameters in body arrays, and make the …
Browse files Browse the repository at this point in the history
…comments clearer.
  • Loading branch information
ricksharp7 committed Jun 8, 2021
1 parent ba7e042 commit 05aaba1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/Extracting/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 15 additions & 2 deletions tests/Unit/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 05aaba1

Please sign in to comment.