Skip to content

Commit

Permalink
handle both inferred and manually defined parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko committed Feb 12, 2025
1 parent 2e1ced1 commit b005a15
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public function handle(RouteInfo $routeInfo, array $parameterExtractionResults):
->map(fn (ReflectionAttribute $ra) => $this->createParameter($parameterExtractionResults, $ra->newInstance(), $ra->getArguments()))
->all();

$extractedAttributes = collect($parameters)->map->name->all();
$extractedAttributes = collect($parameters)->map(fn ($p) => "$p->name.$p->in")->all();

foreach ($parameterExtractionResults as $automaticallyExtractedParameters) {
$automaticallyExtractedParameters->parameters = collect($automaticallyExtractedParameters->parameters)
->filter(fn (Parameter $p) => ! in_array($p->name, $extractedAttributes))
->filter(fn (Parameter $p) => ! in_array("$p->name.$p->in", $extractedAttributes))
->values()
->all();
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Attributes/ParameterAnnotationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ class SameNameParametersController_ParameterAnnotationsTest
public function __invoke() {}
}

it('allows defining parameters with the same names as inferred in different locations', function () {
$openApi = generateForRoute(fn (Router $r) => $r->get('api/test/{test}', SameNameParametersAsInferredController_ParameterAnnotationsTest::class));

expect($parameters = $openApi['paths']['/test/{test}']['get']['parameters'])
->toHaveCount(2)
->and($parameters[0]['name'])->toBe('test')
->and($parameters[1]['name'])->toBe('test')
->and($parameters[0]['in'])->toBe('path')
->and($parameters[1]['in'])->toBe('query');
});
class SameNameParametersAsInferredController_ParameterAnnotationsTest
{
#[QueryParameter('test')]
public function __invoke(string $test) {}
}

it('supports complex examples for Parameter annotations', function () {
$openApi = generateForRoute(fn (Router $r) => $r->get('api/test', ParameterComplexExampleController_ParameterAnnotationsTest::class));

Expand Down

0 comments on commit b005a15

Please sign in to comment.