Skip to content

Commit bdd90ea

Browse files
committed
Code review fixes
1 parent aa7faca commit bdd90ea

File tree

5 files changed

+29
-32
lines changed

5 files changed

+29
-32
lines changed

src/Symfony/Component/HttpKernel/Attribute/MapQueryString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @author Konstantin Myakshin <molodchick@gmail.com>
2020
*/
2121
#[\Attribute(\Attribute::TARGET_PARAMETER)]
22-
final class MapQueryString extends ValueResolver
22+
class MapQueryString extends ValueResolver
2323
{
2424
public function __construct(
2525
public readonly array $context = [],

src/Symfony/Component/HttpKernel/Attribute/MapRequestPayload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @author Konstantin Myakshin <molodchick@gmail.com>
2020
*/
2121
#[\Attribute(\Attribute::TARGET_PARAMETER)]
22-
final class MapRequestPayload extends ValueResolver
22+
class MapRequestPayload extends ValueResolver
2323
{
2424
public function __construct(
2525
public readonly array $context = [],

src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
2323
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
2424
use Symfony\Component\Serializer\Exception\UnsupportedFormatException;
25-
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
2625
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2726
use Symfony\Component\Serializer\SerializerInterface;
2827
use Symfony\Component\Validator\ConstraintViolationInterface;
@@ -36,13 +35,20 @@ final class RequestPayloadValueResolver implements ValueResolverInterface
3635
{
3736
private const DEFAULT_FORMAT = 'json';
3837

38+
/**
39+
* @see \Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT
40+
* @see DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS
41+
*/
3942
private const CONTEXT_DENORMALIZE = [
40-
AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true,
41-
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
43+
'disable_type_enforcement' => true,
44+
'collect_denormalization_errors' => true,
4245
];
4346

47+
/**
48+
* @see DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS
49+
*/
4450
private const CONTEXT_DESERIALIZE = [
45-
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
51+
'collect_denormalization_errors' => true,
4652
];
4753

4854
public function __construct(
@@ -59,7 +65,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
5965
];
6066

6167
foreach ($payloadMappers as $mappingAttribute => [$payloadMapper, $validationFailedCode]) {
62-
if (!$attributes = $argument->getAttributesOfType($mappingAttribute)) {
68+
if (!$attributes = $argument->getAttributesOfType($mappingAttribute, ArgumentMetadata::IS_INSTANCEOF)) {
6369
continue;
6470
}
6571

@@ -104,7 +110,9 @@ private function mapRequestPayload(Request $request, string $type, MapRequestPay
104110
return null;
105111
}
106112

107-
$format = $request->getRequestFormat(self::DEFAULT_FORMAT);
113+
if (null === $format = $request->getContentTypeFormat()) {
114+
throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, 'Unsupported format.');
115+
}
108116

109117
try {
110118
return $this->serializer->deserialize($data, $type, $format, self::CONTEXT_DESERIALIZE + $attribute->context);

src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/RequestPayloadValueResolverTest.php

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestPayloadValueResolver;
1919
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
2020
use Symfony\Component\HttpKernel\Exception\HttpException;
21-
use Symfony\Component\Serializer\Encoder\JsonEncoder;
2221
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2322
use Symfony\Component\Serializer\Serializer;
2423
use Symfony\Component\Serializer\SerializerInterface;
@@ -39,7 +38,7 @@ public function testNotTypedArgument()
3938
$argument = new ArgumentMetadata('notTyped', null, false, false, null, false, [
4039
MapRequestPayload::class => new MapRequestPayload(),
4140
]);
42-
$request = Request::create('/', 'POST', server: ['HTTP_ACCEPT' => 'application/json']);
41+
$request = Request::create('/', 'POST', server: ['HTTP_CONTENT_TYPE' => 'application/json']);
4342

4443
$this->expectException(\LogicException::class);
4544
$this->expectExceptionMessage('Could not resolve the "$notTyped" controller argument: argument should be typed.');
@@ -49,18 +48,26 @@ public function testNotTypedArgument()
4948

5049
public function testValidationNotPassed()
5150
{
52-
$serializer = new Serializer([new DummyDenormalizer()], ['json' => new JsonEncoder()]);
51+
$content = '{"quantity": 50}';
52+
$payload = new RequestPayload(50);
53+
$serializer = $this->createMock(DummySerializerDenormalizerInterface::class);
54+
$serializer->expects($this->once())
55+
->method('deserialize')
56+
->with($content)
57+
->willReturn($payload);
58+
5359
$validator = $this->createMock(ValidatorInterface::class);
5460
$validator->expects($this->once())
5561
->method('validate')
62+
->with($payload)
5663
->willReturn(new ConstraintViolationList([new ConstraintViolation('Test', null, [], '', null, '')]));
5764

5865
$resolver = new RequestPayloadValueResolver($serializer, $validator);
5966

60-
$argument = new ArgumentMetadata('invalid', \stdClass::class, false, false, null, false, [
67+
$argument = new ArgumentMetadata('invalid', RequestPayload::class, false, false, null, false, [
6168
MapRequestPayload::class => new MapRequestPayload(),
6269
]);
63-
$request = Request::create('/', 'POST', server: ['HTTP_ACCEPT' => 'application/json'], content: '["abc"]');
70+
$request = Request::create('/', 'POST', server: ['HTTP_CONTENT_TYPE' => 'application/json'], content: $content);
6471

6572
try {
6673
$resolver->resolve($request, $argument);
@@ -171,24 +178,6 @@ interface DummySerializerDenormalizerInterface extends SerializerInterface, Deno
171178
{
172179
}
173180

174-
class DummyDenormalizer implements DenormalizerInterface
175-
{
176-
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
177-
{
178-
return new \stdClass();
179-
}
180-
181-
public function getSupportedTypes(): array
182-
{
183-
return ['*' => false];
184-
}
185-
186-
public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
187-
{
188-
return true;
189-
}
190-
}
191-
192181
class RequestPayload
193182
{
194183
public function __construct(public readonly int $quantity)

src/Symfony/Component/HttpKernel/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"symfony/deprecation-contracts": "^2.5|^3",
2121
"symfony/error-handler": "^6.3",
2222
"symfony/event-dispatcher": "^5.4|^6.0",
23-
"symfony/http-foundation": "^5.4.21|^6.2.7",
23+
"symfony/http-foundation": "^6.2.7",
2424
"symfony/polyfill-ctype": "^1.8",
2525
"psr/log": "^1|^2|^3"
2626
},

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy