Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 73490be

Browse filesBrowse files
committed
[Serializer] Save missing arguments class in MissingConstructorArgumentsException
1 parent 5bf96bd commit 73490be
Copy full SHA for 73490be

File tree

6 files changed

+113
-6
lines changed
Filter options

6 files changed

+113
-6
lines changed

‎UPGRADE-6.3.md

Copy file name to clipboardExpand all lines: UPGRADE-6.3.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ Validator
6666
---------
6767

6868
* Implementing the `ConstraintViolationInterface` without implementing the `getConstraint()` method is deprecated
69+
70+
Serializer
71+
----------
72+
73+
* Deprecate passing an array of missing arguments as the fourth argument of the `Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException` constructor, pass the missing arguments class name instead and the missing arguments name as the fifth argument.

‎src/Symfony/Component/Serializer/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add `XmlEncoder::SAVE_OPTIONS` context option
8+
* Save missing arguments class in `MissingConstructorArgumentsException`
89

910
6.2
1011
---

‎src/Symfony/Component/Serializer/Exception/MissingConstructorArgumentsException.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Exception/MissingConstructorArgumentsException.php
+38-2Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,55 @@
1717
class MissingConstructorArgumentsException extends RuntimeException
1818
{
1919
/**
20+
* @deprecated since Symfony 6.3
21+
*
2022
* @var string[]
2123
*/
2224
private $missingArguments;
2325

24-
public function __construct(string $message, int $code = 0, \Throwable $previous = null, array $missingArguments = [])
26+
private ?string $class;
27+
private ?string $missingArgument;
28+
29+
/**
30+
* @param class-string|null $class
31+
*/
32+
public function __construct(string $message, int $code = 0, \Throwable $previous = null, /* string */ $class = null, string $missingArgument = null)
2533
{
26-
$this->missingArguments = $missingArguments;
34+
if (\is_array($class)) {
35+
trigger_deprecation('symfony/serializer', '6.3', 'Passing an array of missing arguments as the fourth argument of the "%s" constructor is deprecated, pass the missing arguments class name instead and the missing arguments name as the fifth argument.', __CLASS__);
36+
37+
$this->missingArguments = $class;
38+
39+
$class = null;
40+
$missingArgument = $this->missingArguments[0] ?? null;
41+
}
42+
43+
if (null !== $class && !\is_string($class)) {
44+
throw new \TypeError(sprintf('Argument 4 passed to "%s()" must be a string or null, "%s" given.', __METHOD__, get_debug_type($class)));
45+
}
46+
47+
$this->class = $class;
48+
$this->missingArgument = $missingArgument;
49+
50+
$this->missingArguments ??= null !== $this->missingArgument ? [$this->missingArgument] : [];
2751

2852
parent::__construct($message, $code, $previous);
2953
}
3054

55+
public function getClass(): ?string
56+
{
57+
return $this->class;
58+
}
59+
60+
public function getMissingArgument(): ?string
61+
{
62+
return $this->missingArgument;
63+
}
64+
3165
/**
3266
* @return string[]
67+
*
68+
* @deprecated since Symfony 6.3, use {@see getMissingArgument()} instead
3369
*/
3470
public function getMissingConstructorArguments(): array
3571
{

‎src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex
381381
$params[] = null;
382382
} else {
383383
if (!isset($context['not_normalizable_value_exceptions'])) {
384-
throw new MissingConstructorArgumentsException(sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires parameter "%s" to be present.', $class, $constructorParameter->name), 0, null, [$constructorParameter->name]);
384+
throw new MissingConstructorArgumentsException(sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires parameter "%s" to be present.', $class, $constructorParameter->name), 0, null, $class, $constructorParameter->name);
385385
}
386386

387387
$exception = NotNormalizableValueException::createForUnexpectedDataType(
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Exception;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
16+
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
17+
18+
/**
19+
* @group legacy
20+
*/
21+
class MissingConstructorArgumentsExceptionTest extends TestCase
22+
{
23+
use ExpectDeprecationTrait;
24+
25+
/**
26+
* @dataProvider provideValidArguments
27+
*/
28+
public function testValidArguments(
29+
array|string|null $class,
30+
?string $missingArgument,
31+
?string $expectedClass,
32+
?string $expectedMissingArgument,
33+
array $expectedMissingConstructorArguments,
34+
) {
35+
if (\is_array($class)) {
36+
$this->expectDeprecation(sprintf('Since symfony/serializer 6.3: Passing an array of missing arguments as the fourth argument of the "%s" constructor is deprecated, pass the missing arguments class name instead and the missing arguments name as the fifth argument.', MissingConstructorArgumentsException::class));
37+
}
38+
39+
$exception = new MissingConstructorArgumentsException('', 0, null, $class, $missingArgument);
40+
41+
self::assertSame($expectedClass, $exception->getClass());
42+
self::assertSame($expectedMissingArgument, $exception->getMissingArgument());
43+
self::assertSame($expectedMissingConstructorArguments, $exception->getMissingConstructorArguments());
44+
}
45+
46+
public static function provideValidArguments(): iterable
47+
{
48+
yield 'Without class and missingArgument' => [null, null, null, null, []];
49+
yield 'With class and missingArgument' => ['Foo', 'bar', 'Foo', 'bar', ['bar']];
50+
yield 'With missingArguments' => [['foo', 'bar'], null, null, 'foo', ['foo', 'bar']];
51+
}
52+
53+
public function testInvalidFourthArgument()
54+
{
55+
$this->expectException(\TypeError::class);
56+
$this->expectExceptionMessage(sprintf('Argument 4 passed to "%s::__construct()" must be a string or null, "bool" given.', MissingConstructorArgumentsException::class));
57+
58+
new MissingConstructorArgumentsException('', 0, null, true);
59+
}
60+
}

‎src/Symfony/Component/Serializer/Tests/Normalizer/Features/ConstructorArgumentsTestTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/Features/ConstructorArgumentsTestTrait.php
+8-3Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@ public function testConstructorWithMissingData()
6363

6464
$normalizer = $this->getDenormalizerForConstructArguments();
6565

66-
$this->expectException(MissingConstructorArgumentsException::class);
67-
$this->expectExceptionMessage('Cannot create an instance of "'.ConstructorArgumentsObject::class.'" from serialized data because its constructor requires parameter "bar" to be present.');
68-
$normalizer->denormalize($data, ConstructorArgumentsObject::class);
66+
try {
67+
$normalizer->denormalize($data, ConstructorArgumentsObject::class);
68+
self::fail(sprintf('Failed asserting that exception of type "%s" is thrown.', MissingConstructorArgumentsException::class));
69+
} catch (MissingConstructorArgumentsException $e) {
70+
self::assertSame(sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires parameter "bar" to be present.', ConstructorArgumentsObject::class), $e->getMessage());
71+
self::assertSame(ConstructorArgumentsObject::class, $e->getClass());
72+
self::assertSame('bar', $e->getMissingArgument());
73+
}
6974
}
7075
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.