Skip to content

Commit 2e07d4f

Browse files
committed
feature #31390 [Serializer] UnwrappingDenormalizer (nonanerz)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Serializer] UnwrappingDenormalizer | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a UnwrappingDenormalizer, registered with very high priority. Unwrapping the data if UNWRAP_PATH is provided. Very often some APIs give nested responses in which we need only the child object. With UnwrappingDenormalizer we can get the needed object without creating unnecessary Model class that we don't really need. Regarding to symfony/symfony#28887 and symfony/symfony#30894 Usage: `$serialiser->deserialize('{"baz": {"foo": "bar", "inner": {"title": "value", "numbers": [5,3]}}}', Object::class, ['UnwrappingDenormalizer::UNWRAP_PATH' => '[baz][inner]'])` Commits ------- 00d103d5f7 UnwrappingDenormalizer
2 parents d11d98a + 69573cd commit 2e07d4f

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

DependencyInjection/FrameworkExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
use Symfony\Component\Serializer\Encoder\EncoderInterface;
120120
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
121121
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
122+
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
122123
use Symfony\Component\Stopwatch\Stopwatch;
123124
use Symfony\Component\String\LazyString;
124125
use Symfony\Component\String\Slugger\SluggerInterface;
@@ -1471,6 +1472,10 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
14711472
$container->removeDefinition('serializer.encoder.yaml');
14721473
}
14731474

1475+
if (!class_exists(UnwrappingDenormalizer::class)) {
1476+
$container->removeDefinition('serializer.denormalizer.unwrapping');
1477+
}
1478+
14741479
$serializerLoaders = [];
14751480
if (isset($config['enable_annotations']) && $config['enable_annotations']) {
14761481
if (!$this->annotationsConfigEnabled) {

Resources/config/serializer.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@
7070
<tag name="serializer.normalizer" priority="-890" />
7171
</service>
7272

73+
<service id="serializer.denormalizer.unwrapping" class="Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer">
74+
<argument type="service" id="serializer.property_accessor" />
75+
<!-- Run before serializer.normalizer.object -->
76+
<tag name="serializer.normalizer" priority="1000" />
77+
</service>
78+
7379
<service id="serializer.normalizer.object" class="Symfony\Component\Serializer\Normalizer\ObjectNormalizer">
7480
<argument type="service" id="serializer.mapping.class_metadata_factory" />
7581
<argument type="service" id="serializer.name_converter.metadata_aware" />

0 commit comments

Comments
 (0)