Skip to content

Commit f637640

Browse files
authored
Merge pull request #710 from odparraj/fix-body-as-array
Fix Body As Array
2 parents 3b0d8d4 + 531724e commit f637640

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

src/Extracting/Generator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,6 @@ protected function generateConcreteSampleForArrayKeys($paramName, $paramExample,
239239
$paramName = str_replace(['][', '[', ']', '..'], ['.', '.', '', '.*.'], $paramName);
240240
}
241241
// Then generate a sample item for the dot notation
242-
Arr::set($values, str_replace('.*', '.0', $paramName), $paramExample);
242+
Arr::set($values, str_replace(['.*', '*.'], ['.0','0.'], $paramName), $paramExample);
243243
}
244244
}

tests/Fixtures/TestController.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ public function withBodyParameters()
8989
return '';
9090
}
9191

92+
/**
93+
* Endpoint with body parameters as array.
94+
*
95+
* @bodyParam *.first_name string The first name of the user. Example: John
96+
* @bodyParam *.last_name string The last name of the user. Example: Doe
97+
* @bodyParam *.contacts.*.first_name string The first name of the contact. Example: John
98+
* @bodyParam *.contacts.*.last_name string The last name of the contact. Example: Doe
99+
* @bodyParam *.roles.* string The name of the role. Example: Admin
100+
*/
101+
public function withBodyParametersAsArray()
102+
{
103+
return '';
104+
}
105+
92106
public function withFormRequestParameter(TestRequest $request)
93107
{
94108
return '';

tests/Unit/GeneratorTestCase.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,64 @@ public function can_parse_body_parameters()
171171
], $bodyParameters);
172172
}
173173

174+
/** @test */
175+
public function can_parse_body_parameters_as_array()
176+
{
177+
$route = $this->createRoute('GET', '/api/test', 'withBodyParametersAsArray');
178+
$generator = $this->generator->processRoute($route);
179+
$bodyParameters = $generator['bodyParameters'];
180+
$cleanBodyParameters = $generator['cleanBodyParameters'];
181+
182+
$this->assertArraySubset([
183+
'*.first_name' => [
184+
'type' => 'string',
185+
'description' => 'The first name of the user.',
186+
'required' => false,
187+
'value' => 'John',
188+
],
189+
'*.last_name' => [
190+
'type' => 'string',
191+
'description' => 'The last name of the user.',
192+
'required' => false,
193+
'value' => 'Doe',
194+
],
195+
'*.contacts.*.first_name' => [
196+
'type' => 'string',
197+
'description' => 'The first name of the contact.',
198+
'required' => false,
199+
'value' => 'John',
200+
],
201+
'*.contacts.*.last_name' => [
202+
'type' => 'string',
203+
'description' => 'The last name of the contact.',
204+
'required' => false,
205+
'value' => 'Doe',
206+
],
207+
'*.roles.*' => [
208+
'type' => 'string',
209+
'description' => 'The name of the role.',
210+
'required' => false,
211+
'value' => 'Admin',
212+
],
213+
], $bodyParameters);
214+
215+
$this->assertArraySubset([
216+
[
217+
'first_name' => 'John',
218+
'last_name' => 'Doe',
219+
'contacts' => [
220+
[
221+
'first_name' => 'John',
222+
'last_name' => 'Doe',
223+
]
224+
],
225+
'roles' => [
226+
'Admin'
227+
]
228+
]
229+
], $cleanBodyParameters);
230+
}
231+
174232
/** @test */
175233
public function it_ignores_non_commented_form_request()
176234
{

0 commit comments

Comments
 (0)