Skip to content

Commit da2e868

Browse files
authoredMar 18, 2025
fix: header parameter should be case insensitive (#7031)
fixes #7022
1 parent 7cb5a6d commit da2e868

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed
 

‎src/State/Util/ParameterParserTrait.php

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ private function extractParameterValues(Parameter $parameter, array $values): st
4646
{
4747
$accessors = null;
4848
$key = $parameter->getKey();
49+
if (null === $key) {
50+
throw new \RuntimeException('A Parameter should have a key.');
51+
}
52+
53+
if ($parameter instanceof HeaderParameterInterface) {
54+
$key = strtolower($key);
55+
}
56+
4957
$parsedKey = explode('[:property]', $key);
5058
if (isset($parsedKey[0]) && isset($values[$parsedKey[0]])) {
5159
$key = $parsedKey[0];

‎tests/Fixtures/TestBundle/ApiResource/WithParameter.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@
8080
parameters: new Parameters([new QueryParameter(key: 'q'), new HeaderParameter(key: 'q')]),
8181
provider: [self::class, 'headerAndQueryProvider']
8282
)]
83+
#[GetCollection(
84+
uriTemplate: 'header_required',
85+
parameters: [
86+
'Req' => new HeaderParameter(required: true, schema: ['type' => 'string']),
87+
],
88+
provider: [self::class, 'headerProvider']
89+
)]
8390
#[QueryParameter(key: 'everywhere')]
8491
class WithParameter
8592
{
@@ -128,7 +135,7 @@ public static function restrictAccess(): void
128135
throw new AccessDeniedHttpException();
129136
}
130137

131-
public static function headerAndQueryProvider(Operation $operation, array $uriVariables = [], array $context = [])
138+
public static function headerAndQueryProvider(Operation $operation, array $uriVariables = [], array $context = []): JsonResponse
132139
{
133140
$parameters = $operation->getParameters();
134141
$values = [$parameters->get('q', HeaderParameter::class)->getValue(), $parameters->get('q', QueryParameter::class)->getValue()];
@@ -155,4 +162,12 @@ public static function toInt(Parameter $parameter, array $parameters = [], array
155162

156163
return $operation->withParameters($parameters);
157164
}
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+
}
158173
}

‎tests/Functional/Parameters/ParameterTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,13 @@ public function testHeaderAndQuery(): void
8585
'blabla',
8686
]);
8787
}
88+
89+
public function testHeaderParameterRequired(): void
90+
{
91+
self::createClient()->request('GET', 'header_required', ['headers' => ['req' => 'blabla']]);
92+
$this->assertResponseStatusCodeSame(200);
93+
94+
self::createClient()->request('GET', 'header_required', ['headers' => []]);
95+
$this->assertResponseStatusCodeSame(422);
96+
}
8897
}

0 commit comments

Comments
 (0)