Skip to content

Navigation Menu

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 7b8b165

Browse filesBrowse files
committed
Add new feature to serializer: default_constructor_arguments
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 a3b779f commit 7b8b165
Copy full SHA for 7b8b165

File tree

3 files changed

+22
-0
lines changed
Filter options

3 files changed

+22
-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 `MissingConstructorArgumentsException` new exception for deserialization failure
88
of objects that needs data insertion in constructor
9+
* added an optional `default_constructor_arguments` 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
+3Lines changed: 3 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 DEFAULT_CONSTRUCTOR_ARGUMENTS = 'default_constructor_arguments';
4041

4142
/**
4243
* @var int
@@ -355,6 +356,8 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
355356
// Don't run set for a parameter passed to the constructor
356357
$params[] = $parameterData;
357358
unset($data[$key]);
359+
} elseif (isset($context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key])) {
360+
$params[] = $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
358361
} elseif ($constructorParameter->isDefaultValueAvailable()) {
359362
$params[] = $constructorParameter->getDefaultValue();
360363
} else {

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

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

221+
public function testFillWithEmptyDataWhenMissingData()
222+
{
223+
$data = array(
224+
'foo' => 10,
225+
);
226+
227+
$normalizer = new ObjectNormalizer();
228+
229+
$result = $normalizer->denormalize($data, DummyValueObject::class, 'json', array(
230+
'default_constructor_arguments' => array(
231+
DummyValueObject::class => array('foo' => '', 'bar' => ''),
232+
),
233+
));
234+
235+
$this->assertEquals(new DummyValueObject(10, ''), $result);
236+
}
237+
221238
public function testGroupsNormalize()
222239
{
223240
$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.