Skip to content

Commit

Permalink
fix(state): store parameter value without its key
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Jul 8, 2024
1 parent 83745df commit e02c898
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/State/Provider/ParameterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
}

$parameters->add($parameter->getKey(), $parameter = $parameter->withExtraProperties(
$parameter->getExtraProperties() + ['_api_values' => [$parameter->getKey() => $value]]
$parameter->getExtraProperties() + ['_api_values' => $value]
));

if (null === ($provider = $parameter->getProvider())) {
Expand Down
4 changes: 2 additions & 2 deletions src/State/Tests/ParameterProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public function has(string $id): bool
$operation = $request->attributes->get('_api_operation');

$this->assertEquals('ok', $operation->getName());
$this->assertEquals(['order' => ['foo' => 'asc']], $operation->getParameters()->get('order', QueryParameter::class)->getValue());
$this->assertEquals(['search[:property]' => ['a' => 'bar']], $operation->getParameters()->get('search[:property]', QueryParameter::class)->getValue());
$this->assertEquals(['foo' => 'asc'], $operation->getParameters()->get('order', QueryParameter::class)->getValue());
$this->assertEquals(['a' => 'bar'], $operation->getParameters()->get('search[:property]', QueryParameter::class)->getValue());
}

public static function provide(): void
Expand Down
13 changes: 11 additions & 2 deletions src/Symfony/Validator/State/ParameterValidatorProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Symfony\Validator\State;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ParameterNotFound;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\State\Util\ParameterParserTrait;
use ApiPlatform\Validator\Exception\ValidationException;
Expand All @@ -39,19 +40,27 @@ public function __construct(

public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
if (!($request = $context['request']) instanceof Request || false === $operation->getQueryParameterValidationEnabled()) {
if (!($request = $context['request']) instanceof Request) {
return $this->decorated->provide($operation, $uriVariables, $context);
}

$operation = $request->attributes->get('_api_operation') ?? $operation;
if (!($operation->getQueryParameterValidationEnabled() ?? true)) {
return $this->decorated->provide($operation, $uriVariables, $context);
}

$constraintViolationList = new ConstraintViolationList();
foreach ($operation->getParameters() ?? [] as $parameter) {
if (!$constraints = $parameter->getConstraints()) {
continue;
}

$key = $parameter->getKey();
$value = $parameter->getValue()[$key] ?? null;
$value = $parameter->getValue();
if ($value instanceof ParameterNotFound) {
$value = null;
}

$violations = $this->validator->validate($value, $constraints);
foreach ($violations as $violation) {
$constraintViolationList->add(new ConstraintViolation(
Expand Down

0 comments on commit e02c898

Please sign in to comment.