Skip to content

Commit

Permalink
Merge pull request #710 from odparraj/fix-body-as-array
Browse files Browse the repository at this point in the history
Fix Body As Array
  • Loading branch information
shalvah authored Mar 11, 2020
2 parents 3b0d8d4 + 531724e commit f637640
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Extracting/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,6 @@ protected function generateConcreteSampleForArrayKeys($paramName, $paramExample,
$paramName = str_replace(['][', '[', ']', '..'], ['.', '.', '', '.*.'], $paramName);
}
// Then generate a sample item for the dot notation
Arr::set($values, str_replace('.*', '.0', $paramName), $paramExample);
Arr::set($values, str_replace(['.*', '*.'], ['.0','0.'], $paramName), $paramExample);
}
}
14 changes: 14 additions & 0 deletions tests/Fixtures/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ public function withBodyParameters()
return '';
}

/**
* Endpoint with body parameters as array.
*
* @bodyParam *.first_name string The first name of the user. Example: John
* @bodyParam *.last_name string The last name of the user. Example: Doe
* @bodyParam *.contacts.*.first_name string The first name of the contact. Example: John
* @bodyParam *.contacts.*.last_name string The last name of the contact. Example: Doe
* @bodyParam *.roles.* string The name of the role. Example: Admin
*/
public function withBodyParametersAsArray()
{
return '';
}

public function withFormRequestParameter(TestRequest $request)
{
return '';
Expand Down
58 changes: 58 additions & 0 deletions tests/Unit/GeneratorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,64 @@ public function can_parse_body_parameters()
], $bodyParameters);
}

/** @test */
public function can_parse_body_parameters_as_array()
{
$route = $this->createRoute('GET', '/api/test', 'withBodyParametersAsArray');
$generator = $this->generator->processRoute($route);
$bodyParameters = $generator['bodyParameters'];
$cleanBodyParameters = $generator['cleanBodyParameters'];

$this->assertArraySubset([
'*.first_name' => [
'type' => 'string',
'description' => 'The first name of the user.',
'required' => false,
'value' => 'John',
],
'*.last_name' => [
'type' => 'string',
'description' => 'The last name of the user.',
'required' => false,
'value' => 'Doe',
],
'*.contacts.*.first_name' => [
'type' => 'string',
'description' => 'The first name of the contact.',
'required' => false,
'value' => 'John',
],
'*.contacts.*.last_name' => [
'type' => 'string',
'description' => 'The last name of the contact.',
'required' => false,
'value' => 'Doe',
],
'*.roles.*' => [
'type' => 'string',
'description' => 'The name of the role.',
'required' => false,
'value' => 'Admin',
],
], $bodyParameters);

$this->assertArraySubset([
[
'first_name' => 'John',
'last_name' => 'Doe',
'contacts' => [
[
'first_name' => 'John',
'last_name' => 'Doe',
]
],
'roles' => [
'Admin'
]
]
], $cleanBodyParameters);
}

/** @test */
public function it_ignores_non_commented_form_request()
{
Expand Down

0 comments on commit f637640

Please sign in to comment.