Skip to content

Commit

Permalink
Don't error when nesting params for response fields (closes #578)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Dec 5, 2022
1 parent 00e7c27 commit 42df1b1
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 19 deletions.
33 changes: 28 additions & 5 deletions src/Extracting/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,27 @@ protected static function convertStringValueToUploadedFileInstance(string $fileP
/**
* Transform body parameters such that object fields have a `fields` property containing a list of all subfields
* Subfields will be removed from the main parameter map
* For instance, if $parameters is ['dad' => [], 'dad.cars' => [], 'dad.age' => []],
* normalise this into ['dad' => [..., '__fields' => ['dad.cars' => [], 'dad.age' => []]]
* For instance, if $parameters is [
* 'dad' => new Parameter(...),
* 'dad.age' => new Parameter(...),
* 'dad.cars[]' => new Parameter(...),
* 'dad.cars[].model' => new Parameter(...),
* 'dad.cars[].price' => new Parameter(...),
* ],
* normalise this into [
* 'dad' => [
* ...,
* '__fields' => [
* 'dad.age' => [...],
* 'dad.cars' => [
* ...,
* '__fields' => [
* 'model' => [...],
* 'price' => [...],
* ],
* ],
* ],
* ]]
*
* @param array $parameters
*
Expand All @@ -429,13 +448,17 @@ public static function nestArrayAndObjectFields(array $parameters, array $cleanP
}

if (empty($normalisedParameters[$parentName])) {
$normalisedParameters[$parentName] = new Parameter([
$details = [
"name" => $parentName,
"type" => $parentName === '[]' ? "object[]" : "object",
"description" => "",
"required" => true,
"example" => [$fieldName => $parameter->example],
]);
];

if (!$parameter instanceof ResponseField)
$details["example"] = [$fieldName => $parameter->example];

$normalisedParameters[$parentName] = new Parameter($details);
}
}
$normalisedParameters[$name] = $parameter;
Expand Down
77 changes: 63 additions & 14 deletions tests/Unit/ExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Routing\Route;
use Knuckles\Camel\Extraction\ExtractedEndpointData;
use Knuckles\Camel\Extraction\Parameter;
use Knuckles\Camel\Extraction\ResponseField;
use Knuckles\Scribe\Extracting\Extractor;
use Knuckles\Scribe\Tests\Fixtures\TestController;
use Knuckles\Scribe\Tools\DocumentationConfig;
Expand Down Expand Up @@ -80,7 +81,7 @@ public function clean_can_properly_parse_array_keys()
'object.key3' => [
'name' => 'object.key3',
'type' => 'object',
'example'=> [],
'example' => [],
],
'object.key3.key1' => [
'name' => 'object.key3.key1',
Expand Down Expand Up @@ -118,8 +119,8 @@ public function clean_can_properly_parse_array_keys()
'key1' => '43',
'key2' => 77,
'key3' => [
'key1' => 'hoho'
]
'key1' => 'hoho',
],
],
'list' => [4],
'list_of_objects' => [
Expand Down Expand Up @@ -245,7 +246,7 @@ public function can_use_closure_in_routes_uses()
* @queryParam location_id required The id of the location.
* @bodyParam name required Name of the location
*/
$handler = fn () => 'hi';
$handler = fn() => 'hi';
$route = $this->createClosureRoute('POST', '/api/closure/test', $handler);

$parsed = $this->extractor->processRoute($route);
Expand Down Expand Up @@ -292,6 +293,54 @@ public function can_override_data_for_inherited_methods()
$this->assertArraySubset(["type" => "string"], $inherited->queryParameters["queryThing"]->toArray());
}

/** @test */
public function can_nest_array_and_object_parameters_correctly()
{
$parameters = [
"dad" => Parameter::create([
"name" => 'dad',
]),
"dad.age" => ResponseField::create([
"name" => 'dad.age',
]),
"dad.cars[]" => Parameter::create([
"name" => 'dad.cars[]',
]),
"dad.cars[].model" => Parameter::create([
"name" => 'dad.cars[].model',
]),
"dad.cars[].price" => ResponseField::create([
"name" => 'dad.cars[].price',
]),
];
$cleanParameters = [];

$nested = Extractor::nestArrayAndObjectFields($parameters, $cleanParameters);

$this->assertEquals(["dad"], array_keys($nested));
$this->assertArraySubset([
"dad" => [
"__fields" => [
"age" => [
"name" => "dad.age",
],
"cars" => [
"name" => "dad.cars",
"__fields" => [
"model" => [
"name" => "dad.cars[].model",
],
"price" => [
"name" => "dad.cars[].price",
],
],
],
],
],
], $nested);

}

public function createRoute(string $httpMethod, string $path, string $controllerMethod, $class = TestController::class)
{
return new Route([$httpMethod], $path, ['uses' => [$class, $controllerMethod]]);
Expand All @@ -316,64 +365,64 @@ public function authRules()
'enabled' => true,
'in' => 'bearer',
'name' => 'dfadb',
]
],
],
[
'name' => 'Authorization',
'where' => 'headers',
]
],
],
[
[
'auth' => [
'enabled' => true,
'in' => 'basic',
'name' => 'efwr',
]
],
],
[
'name' => 'Authorization',
'where' => 'headers',
]
],
],
[
[
'auth' => [
'enabled' => true,
'in' => 'header',
'name' => 'Api-Key',
]
],
],
[
'name' => 'Api-Key',
'where' => 'headers',
]
],
],
[
[
'auth' => [
'enabled' => true,
'in' => 'query',
'name' => 'apiKey',
]
],
],
[
'name' => 'apiKey',
'where' => 'queryParameters',
]
],
],
[
[
'auth' => [
'enabled' => true,
'in' => 'body',
'name' => 'access_token',
]
],
],
[
'name' => 'access_token',
'where' => 'bodyParameters',
]
],
],
];
}
Expand Down

0 comments on commit 42df1b1

Please sign in to comment.