diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php index b27b1985eb8ef..c564f36054f95 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php @@ -209,11 +209,6 @@ public function normalize(mixed $object, ?string $format = null, array $context foreach ($stack as $attribute => $attributeValue) { $attributeContext = $this->getAttributeNormalizationContext($object, $attribute, $context); - if (null === $attributeValue || \is_scalar($attributeValue)) { - $data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $attributeContext, $attributesMetadata, $classMetadata); - continue; - } - if (!$this->serializer instanceof NormalizerInterface) { throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer.', $attribute)); } diff --git a/src/Symfony/Component/Serializer/Normalizer/ScalarNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ScalarNormalizer.php new file mode 100644 index 0000000000000..6373a2104f130 --- /dev/null +++ b/src/Symfony/Component/Serializer/Normalizer/ScalarNormalizer.php @@ -0,0 +1,41 @@ +createNormalizer(); + } + + + protected function createNormalizer(): void + { + $classMetadataFactory = new ClassMetadataFactory(new AttributeLoader()); + $normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory)); + $scalarNormalizer = new ScalarNormalizer(); + $xmlEncoder = new XmlEncoder(); + $this->serializer = new Serializer([$normalizer, $scalarNormalizer], [$xmlEncoder]); + } + + public function testBoolWithoutContext() + { + self::assertXmlStringEqualsXmlString(' +10', + $this->serializer->serialize(new ObjectWithBool(), 'xml')); + } + + public function testBoolWithContext() + { + self::assertXmlStringEqualsXmlString(' +truefalse', + $this->serializer->serialize(new ObjectWithBool(), 'xml', [ + ScalarNormalizer::TRUE_VALUE_KEY => 'true', + ScalarNormalizer::FALSE_VALUE_KEY => 'false' + ])); + } +} + + +class ObjectWithBool +{ + public bool $fooTrue = true; + public bool $fooFalse = false; +}