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 368de4e

Browse filesBrowse files
committed
Add new IncompleteInputDataException
Before: impossible to catch value object hydratation failure After: catch the new exception No BC break.
1 parent a19afcd commit 368de4e
Copy full SHA for 368de4e

File tree

4 files changed

+57
-1
lines changed
Filter options

4 files changed

+57
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/CHANGELOG.md
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
4.1.0
5+
-----
6+
7+
* added `IncompleteInputDataException` new exception for deserialization failure
8+
of objects that needs data insertion in constructor
9+
410
4.0.0
511
-----
612

+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Exception;
13+
14+
/**
15+
* IncompleteInputDataException.
16+
*
17+
* @author Maxime VEBER <maxime.veber@nekland.fr>
18+
*/
19+
class IncompleteInputDataException extends RuntimeException
20+
{
21+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Normalizer;
1313

1414
use Symfony\Component\Serializer\Exception\CircularReferenceException;
15+
use Symfony\Component\Serializer\Exception\IncompleteInputDataException;
1516
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
1617
use Symfony\Component\Serializer\Exception\LogicException;
1718
use Symfony\Component\Serializer\Exception\RuntimeException;
@@ -308,6 +309,7 @@ protected function getConstructor(array &$data, $class, array &$context, \Reflec
308309
* @return object
309310
*
310311
* @throws RuntimeException
312+
* @throws IncompleteInputDataException
311313
*/
312314
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, string $format = null)
313315
{
@@ -356,7 +358,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
356358
} elseif ($constructorParameter->isDefaultValueAvailable()) {
357359
$params[] = $constructorParameter->getDefaultValue();
358360
} else {
359-
throw new RuntimeException(
361+
throw new IncompleteInputDataException(
360362
sprintf(
361363
'Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.',
362364
$class,

‎src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,22 @@ public function testConstructorWithUnknownObjectTypeHintDenormalize()
203203
$normalizer->denormalize($data, DummyWithConstructorInexistingObject::class);
204204
}
205205

206+
public function testConstructorWithMissingData()
207+
{
208+
$data = array(
209+
'foo' => 10,
210+
);
211+
212+
$this->expectException(\Symfony\Component\Serializer\Exception\IncompleteInputDataException::class);
213+
$this->expectExceptionMessage('Cannot create an instance of Symfony\Component\Serializer\Tests\Normalizer\DummyValueObject from serialized data because its constructor requires parameter "bar" to be present.');
214+
215+
$normalizer = new ObjectNormalizer();
216+
$serializer = new Serializer(array($normalizer));
217+
$normalizer->setSerializer($serializer);
218+
219+
$normalizer->denormalize($data, DummyValueObject::class);
220+
}
221+
206222
public function testGroupsNormalize()
207223
{
208224
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
@@ -1025,6 +1041,17 @@ public function __construct($id, Unknown $unknown)
10251041
{
10261042
}
10271043
}
1044+
class DummyValueObject
1045+
{
1046+
private $foo;
1047+
private $bar;
1048+
1049+
public function __construct($foo, $bar)
1050+
{
1051+
$this->foo = $foo;
1052+
$this->bar = $bar;
1053+
}
1054+
}
10281055

10291056
class JsonNumber
10301057
{

0 commit comments

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