diff --git a/src/Symfony/Component/Serializer/Serializer.php b/src/Symfony/Component/Serializer/Serializer.php index b8c33a2fe5..6414caf900 100644 --- a/src/Symfony/Component/Serializer/Serializer.php +++ b/src/Symfony/Component/Serializer/Serializer.php @@ -189,7 +189,10 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface */ public function denormalize($data, string $type, string $format = null, array $context = []) { - if (isset(self::SCALAR_TYPES[$type])) { + $normalizer = $this->getDenormalizer($data, $type, $format, $context); + + // Check for a denormalizer first, e.g. the data is wrapped + if (!$normalizer && isset(self::SCALAR_TYPES[$type])) { if (!('is_'.$type)($data)) { throw new NotNormalizableValueException(sprintf('Data expected to be of type "%s" ("%s" given).', $type, get_debug_type($data))); } @@ -201,7 +204,7 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface throw new LogicException('You must register at least one normalizer to be able to denormalize objects.'); } - if ($normalizer = $this->getDenormalizer($data, $type, $format, $context)) { + if ($normalizer) { return $normalizer->denormalize($data, $type, $format, $context); } diff --git a/src/Symfony/Component/Serializer/Tests/SerializerTest.php b/src/Symfony/Component/Serializer/Tests/SerializerTest.php index 36cd098af8..3f27877840 100644 --- a/src/Symfony/Component/Serializer/Tests/SerializerTest.php +++ b/src/Symfony/Component/Serializer/Tests/SerializerTest.php @@ -613,6 +613,13 @@ class SerializerTest extends TestCase $serializer->deserialize('["42"]', 'int[]', 'json'); } + public function testDeserializeWrappedScalar() + { + $serializer = new Serializer([new UnwrappingDenormalizer()], ['json' => new JsonEncoder()]); + + $this->assertSame(42, $serializer->deserialize('{"wrapper": 42}', 'int', 'json', [UnwrappingDenormalizer::UNWRAP_PATH => '[wrapper]'])); + } + private function serializerWithClassDiscriminator() { $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));