Skip to content

Commit 613d42b

Browse files
committed
Merge branch '6.3' into 6.4
* 6.3: fix typo CS fix [Serializer] Keep stack trace for enum value denormalizer error add the MailPace transport to the UnsupportedSchemeException [Serializer] Fix partial denormalization with missing constructor arguments [Serializer] Moves dummy to fixtures folder [HttpKernel] Don't validate partially denormalized object
2 parents 51c41ac + 9f69174 commit 613d42b

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

Controller/ArgumentResolver/RequestPayloadValueResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event): vo
119119
$payload = $e->getData();
120120
}
121121

122-
if (null !== $payload) {
122+
if (null !== $payload && !\count($violations)) {
123123
$violations->addAll($this->validator->validate($payload, null, $argument->validationGroups ?? null));
124124
}
125125

Tests/Controller/ArgumentResolver/RequestPayloadValueResolverTest.php

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
2929
use Symfony\Component\Serializer\Serializer;
3030
use Symfony\Component\Validator\Constraints as Assert;
31-
use Symfony\Component\Validator\ConstraintViolation;
3231
use Symfony\Component\Validator\ConstraintViolationList;
3332
use Symfony\Component\Validator\Exception\ValidationFailedException;
3433
use Symfony\Component\Validator\Validator\ValidatorInterface;
@@ -227,14 +226,11 @@ public function testWithoutValidatorAndCouldNotDenormalize()
227226
public function testValidationNotPassed()
228227
{
229228
$content = '{"price": 50, "title": ["not a string"]}';
230-
$payload = new RequestPayload(50);
231229
$serializer = new Serializer([new ObjectNormalizer()], ['json' => new JsonEncoder()]);
232230

233231
$validator = $this->createMock(ValidatorInterface::class);
234-
$validator->expects($this->once())
235-
->method('validate')
236-
->with($payload)
237-
->willReturn(new ConstraintViolationList([new ConstraintViolation('Test', null, [], '', null, '')]));
232+
$validator->expects($this->never())
233+
->method('validate');
238234

239235
$resolver = new RequestPayloadValueResolver($serializer, $validator);
240236

@@ -255,7 +251,36 @@ public function testValidationNotPassed()
255251
$this->assertSame(422, $e->getStatusCode());
256252
$this->assertInstanceOf(ValidationFailedException::class, $validationFailedException);
257253
$this->assertSame(sprintf('This value should be of type %s.', class_exists(InvalidTypeException::class) ? 'string' : 'unknown'), $validationFailedException->getViolations()[0]->getMessage());
258-
$this->assertSame('Test', $validationFailedException->getViolations()[1]->getMessage());
254+
}
255+
}
256+
257+
public function testValidationNotPerformedWhenPartialDenormalizationReturnsViolation()
258+
{
259+
$content = '{"password": "abc"}';
260+
$serializer = new Serializer([new ObjectNormalizer()], ['json' => new JsonEncoder()]);
261+
262+
$validator = $this->createMock(ValidatorInterface::class);
263+
$validator->expects($this->never())
264+
->method('validate');
265+
266+
$resolver = new RequestPayloadValueResolver($serializer, $validator);
267+
268+
$argument = new ArgumentMetadata('invalid', User::class, false, false, null, false, [
269+
MapRequestPayload::class => new MapRequestPayload(),
270+
]);
271+
$request = Request::create('/', 'POST', server: ['CONTENT_TYPE' => 'application/json'], content: $content);
272+
273+
$kernel = $this->createMock(HttpKernelInterface::class);
274+
$arguments = $resolver->resolve($request, $argument);
275+
$event = new ControllerArgumentsEvent($kernel, function () {}, $arguments, $request, HttpKernelInterface::MAIN_REQUEST);
276+
277+
try {
278+
$resolver->onKernelControllerArguments($event);
279+
$this->fail(sprintf('Expected "%s" to be thrown.', HttpException::class));
280+
} catch (HttpException $e) {
281+
$validationFailedException = $e->getPrevious();
282+
$this->assertInstanceOf(ValidationFailedException::class, $validationFailedException);
283+
$this->assertSame('This value should be of type unknown.', $validationFailedException->getViolations()[0]->getMessage());
259284
}
260285
}
261286

@@ -688,3 +713,24 @@ public function __construct(public readonly float $page)
688713
{
689714
}
690715
}
716+
717+
class User
718+
{
719+
public function __construct(
720+
#[Assert\NotBlank, Assert\Email]
721+
private string $email,
722+
#[Assert\NotBlank]
723+
private string $password,
724+
) {
725+
}
726+
727+
public function getEmail(): string
728+
{
729+
return $this->email;
730+
}
731+
732+
public function getPassword(): string
733+
{
734+
return $this->password;
735+
}
736+
}

0 commit comments

Comments
 (0)