Skip to content

Commit 8744814

Browse files
committed
Merge 3.4
2 parents 1aa5017 + da2e868 commit 8744814

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

src/State/Provider/ParameterProvider.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use ApiPlatform\Metadata\Operation;
1717
use ApiPlatform\State\Exception\ProviderNotFoundException;
1818
use ApiPlatform\State\ParameterNotFound;
19-
use ApiPlatform\State\ParameterProviderInterface;
2019
use ApiPlatform\State\ProviderInterface;
2120
use ApiPlatform\State\Util\ParameterParserTrait;
2221
use ApiPlatform\State\Util\RequestParser;
@@ -83,13 +82,15 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
8382
continue;
8483
}
8584

86-
if (!\is_string($provider) || !$this->locator->has($provider)) {
87-
throw new ProviderNotFoundException(\sprintf('Provider "%s" not found on operation "%s"', $provider, $operation->getName()));
85+
if (\is_string($provider)) {
86+
if (!$this->locator->has($provider)) {
87+
throw new ProviderNotFoundException(\sprintf('Provider "%s" not found on operation "%s"', $provider, $operation->getName()));
88+
}
89+
90+
$provider = $this->locator->get($provider);
8891
}
8992

90-
/** @var ParameterProviderInterface $providerInstance */
91-
$providerInstance = $this->locator->get($provider);
92-
if (($op = $providerInstance->provide($parameter, $values, $context)) instanceof Operation) {
93+
if (($op = $provider->provide($parameter, $values, $context)) instanceof Operation) {
9394
$operation = $op;
9495
}
9596
}

src/State/Util/ParameterParserTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ private function extractParameterValues(Parameter $parameter, array $values): st
5050
throw new \RuntimeException('A Parameter should have a key.');
5151
}
5252

53+
if ($parameter instanceof HeaderParameterInterface) {
54+
$key = strtolower($key);
55+
}
56+
5357
$parsedKey = explode('[:property]', $key);
5458
if (isset($parsedKey[0]) && isset($values[$parsedKey[0]])) {
5559
$key = $parsedKey[0];

tests/Fixtures/TestBundle/ApiResource/WithParameter.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'group' => new QueryParameter(provider: [self::class, 'provideGroup']),
4040
'properties' => new QueryParameter(filter: 'my_dummy.property'),
4141
'service' => new QueryParameter(provider: CustomGroupParameterProvider::class),
42+
'object' => new QueryParameter(provider: new CustomGroupParameterProvider()),
4243
'auth' => new HeaderParameter(provider: [self::class, 'restrictAccess']),
4344
'priority' => new QueryParameter(provider: [self::class, 'assertSecond'], priority: 10),
4445
'priorityb' => new QueryParameter(provider: [self::class, 'assertFirst'], priority: 20),
@@ -79,6 +80,13 @@
7980
parameters: new Parameters([new QueryParameter(key: 'q'), new HeaderParameter(key: 'q')]),
8081
provider: [self::class, 'headerAndQueryProvider']
8182
)]
83+
#[GetCollection(
84+
uriTemplate: 'header_required',
85+
parameters: [
86+
'Req' => new HeaderParameter(required: true, schema: ['type' => 'string']),
87+
],
88+
provider: [self::class, 'headerProvider']
89+
)]
8290
#[QueryParameter(key: 'everywhere')]
8391
class WithParameter
8492
{
@@ -127,7 +135,7 @@ public static function restrictAccess(): void
127135
throw new AccessDeniedHttpException();
128136
}
129137

130-
public static function headerAndQueryProvider(Operation $operation, array $uriVariables = [], array $context = [])
138+
public static function headerAndQueryProvider(Operation $operation, array $uriVariables = [], array $context = []): JsonResponse
131139
{
132140
$parameters = $operation->getParameters();
133141
$values = [$parameters->get('q', HeaderParameter::class)->getValue(), $parameters->get('q', QueryParameter::class)->getValue()];
@@ -154,4 +162,12 @@ public static function toInt(Parameter $parameter, array $parameters = [], array
154162

155163
return $operation->withParameters($parameters);
156164
}
165+
166+
public static function headerProvider(Operation $operation, array $uriVariables = [], array $context = []): JsonResponse
167+
{
168+
$parameters = $operation->getParameters();
169+
$values = [$parameters->get('Req', HeaderParameter::class)->getValue()];
170+
171+
return new JsonResponse($values);
172+
}
157173
}

tests/Functional/Parameters/ParameterTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public function testWithServiceProvider(): void
5555
$this->assertArrayNotHasKey('a', $response->toArray());
5656
}
5757

58+
public function testWithObjectProvider(): void
59+
{
60+
$response = self::createClient()->request('GET', 'with_parameters/1?service=blabla');
61+
$this->assertArrayNotHasKey('a', $response->toArray());
62+
}
63+
5864
public function testWithHeader(): void
5965
{
6066
self::createClient()->request('GET', 'with_parameters/1?service=blabla', ['headers' => ['auth' => 'foo']]);
@@ -91,4 +97,13 @@ public function testHeaderAndQuery(): void
9197
'blabla',
9298
]);
9399
}
100+
101+
public function testHeaderParameterRequired(): void
102+
{
103+
self::createClient()->request('GET', 'header_required', ['headers' => ['req' => 'blabla']]);
104+
$this->assertResponseStatusCodeSame(200);
105+
106+
self::createClient()->request('GET', 'header_required', ['headers' => []]);
107+
$this->assertResponseStatusCodeSame(422);
108+
}
94109
}

0 commit comments

Comments
 (0)