Skip to content

Commit

Permalink
fixed parameters handling taking in into consideration
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko committed Feb 12, 2025
1 parent 2f4137d commit 2e1ced1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public function handle(RouteInfo $routeInfo, array $parameterExtractionResults):
$param->setExtensionProperty('optional', true);
}

$param->setAttribute('nonBody', true);

return $param;
}, array_values(array_diff($route->parameterNames(), $this->getParametersFromString($route->getDomain()))));

Expand Down
11 changes: 5 additions & 6 deletions src/Support/OperationExtensions/RequestBodyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function handle(Operation $operation, RouteInfo $routeInfo)
->summary(Str::of($routeInfo->phpDoc()->getAttribute('summary'))->rtrim('.'))
->description($description);

$allParams = $rulesResults->flatMap->parameters->unique('name')->values()->all();
$allParams = $rulesResults->flatMap->parameters->unique(fn ($p) => "$p->name.$p->in")->values()->all();

$mediaType = $this->getMediaType($operation, $routeInfo, $allParams);

Expand Down Expand Up @@ -81,15 +81,14 @@ public function handle(Operation $operation, RouteInfo $routeInfo)
$schemalessResults = collect([$this->mergeSchemalessRulesResults($schemalessResults->values())]);

$schemas = $schemaResults->merge($schemalessResults)
->filter(fn (ParametersExtractionResult $r) => count($r->parameters) || $r->schemaName)
->map(function (ParametersExtractionResult $r) use ($nonBodyParams) {
$qpNames = collect($nonBodyParams)->keyBy('name');
$qpNames = collect($nonBodyParams)->keyBy(fn ($p) => "$p->name.$p->in");

$r->parameters = collect($r->parameters)->filter(fn ($p) => ! $qpNames->has($p->name))->values()->all();
$r->parameters = collect($r->parameters)->filter(fn ($p) => ! $qpNames->has("$p->name.$p->in"))->values()->all();

return $r;
})
->values()
->filter(fn (ParametersExtractionResult $r) => count($r->parameters) || $r->schemaName)
->map($this->makeSchemaFromResults(...));

if ($schemas->isEmpty()) {
Expand Down Expand Up @@ -155,7 +154,7 @@ protected function makeComposedRequestBodySchema(Collection $schemas)
protected function mergeSchemalessRulesResults(Collection $schemalessResults): ParametersExtractionResult
{
return new ParametersExtractionResult(
parameters: $this->convertDotNamedParamsToComplexStructures($schemalessResults->values()->flatMap->parameters->unique('name')->values()->all()),
parameters: $this->convertDotNamedParamsToComplexStructures($schemalessResults->values()->flatMap->parameters->unique(fn ($p) => "$p->name.$p->in")->values()->all()),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public function __construct(private Collection $parameters) {}

public function handle()
{
return $this->handleNested($this->parameters->keyBy('name'))
return $this->parameters->groupBy('in')
->map(fn ($parameters) => $this->handleNested($parameters->keyBy('name'))->values())
->flatten()
->values()
->all();
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Attributes/ParameterAnnotationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Dedoc\Scramble\Attributes\HeaderParameter;
use Dedoc\Scramble\Attributes\Parameter;
use Dedoc\Scramble\Attributes\PathParameter;
use Dedoc\Scramble\Attributes\QueryParameter;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;

Expand Down Expand Up @@ -68,6 +69,23 @@ class ParameterSimpleExampleController_ParameterAnnotationsTest
public function __invoke() {}
}

it('allows annotating parameters with the same names', function () {
$openApi = generateForRoute(fn (Router $r) => $r->get('api/test', SameNameParametersController_ParameterAnnotationsTest::class));

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

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 2e1ced1

Please sign in to comment.