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 d305b76

Browse filesBrowse files
committed
Add new feature to serializer: empty_data
You can now add empty_data to the context on deserialization of objects. This allow you to deserialize objects that have requirements in the constructor that can't be given. Basically, it helps you hydrate with value objects, so the validation component can invalid the object without the serializer send an error.
1 parent 368de4e commit d305b76
Copy full SHA for d305b76

File tree

3 files changed

+26
-0
lines changed
Filter options

3 files changed

+26
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ CHANGELOG
66

77
* added `IncompleteInputDataException` new exception for deserialization failure
88
of objects that needs data insertion in constructor
9+
* added an optional `empty_data` option of context to specify a default data in
10+
case the object is not initializable by its constructor because of data missing
911

1012
4.0.0
1113
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
3737
const GROUPS = 'groups';
3838
const ATTRIBUTES = 'attributes';
3939
const ALLOW_EXTRA_ATTRIBUTES = 'allow_extra_attributes';
40+
const EMPTY_DATA = 'empty_data';
4041

4142
/**
4243
* @var int
@@ -358,6 +359,9 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
358359
} elseif ($constructorParameter->isDefaultValueAvailable()) {
359360
$params[] = $constructorParameter->getDefaultValue();
360361
} else {
362+
if (isset($context[static::EMPTY_DATA][$class])) {
363+
return $context[static::EMPTY_DATA][$class];
364+
}
361365
throw new IncompleteInputDataException(
362366
sprintf(
363367
'Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.',

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,26 @@ public function testConstructorWithMissingData()
219219
$normalizer->denormalize($data, DummyValueObject::class);
220220
}
221221

222+
public function testFillWithEmptyDataWhenMissingData()
223+
{
224+
$data = array(
225+
'foo' => 10,
226+
);
227+
228+
$normalizer = new ObjectNormalizer();
229+
$serializer = new Serializer(array($normalizer));
230+
$normalizer->setSerializer($serializer);
231+
232+
$empty = new DummyValueObject('', '');
233+
$result = $normalizer->denormalize($data, DummyValueObject::class, 'json', array(
234+
'empty_data' => array(
235+
DummyValueObject::class => $empty,
236+
),
237+
));
238+
239+
$this->assertEquals($empty, $result);
240+
}
241+
222242
public function testGroupsNormalize()
223243
{
224244
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

0 commit comments

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