|
15 | 15 | use PHPUnit\Framework\TestCase;
|
16 | 16 | use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
|
17 | 17 | use Symfony\Component\PropertyInfo\Type;
|
| 18 | +use Symfony\Component\Serializer\Annotation\SerializedName; |
18 | 19 | use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
19 | 20 | use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
|
20 | 21 | use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
|
|
24 | 25 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
25 | 26 | use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
26 | 27 | use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
| 28 | +use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter; |
27 | 29 | use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
|
28 | 30 | use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
29 | 31 | use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
@@ -227,6 +229,26 @@ public function hasMetadataFor($value): bool
|
227 | 229 | $this->assertInstanceOf(DummySecondChildQuux::class, $normalizedData->quux);
|
228 | 230 | }
|
229 | 231 |
|
| 232 | + public function testDenormalizeXmlStringNodeWithoutAttributesToObject() |
| 233 | + { |
| 234 | + $denormalizer = $this->getDenormalizerForStringNode(); |
| 235 | + // if an xml-node can have children which should be deserialized as string[] |
| 236 | + // and only one child exists |
| 237 | + $object = $denormalizer->denormalize('string-value', DummyObjectWithOptionalAttributes::class, 'xml'); |
| 238 | + $this->assertInstanceOf(DummyObjectWithOptionalAttributes::class, $object); |
| 239 | + $this->assertEquals('string-value', $object->value); |
| 240 | + $this->assertNull($object->foo); |
| 241 | + } |
| 242 | + |
| 243 | + public function getDenormalizerForStringNode() |
| 244 | + { |
| 245 | + $denormalizer = new AbstractObjectNormalizerWithMetadataAndNameConverter(); |
| 246 | + $serializer = new Serializer([$denormalizer]); |
| 247 | + $denormalizer->setSerializer($serializer); |
| 248 | + |
| 249 | + return $denormalizer; |
| 250 | + } |
| 251 | + |
230 | 252 | /**
|
231 | 253 | * Test that additional attributes throw an exception if no metadata factory is specified.
|
232 | 254 | */
|
@@ -302,6 +324,44 @@ class StringCollection
|
302 | 324 | public $children;
|
303 | 325 | }
|
304 | 326 |
|
| 327 | +class AbstractObjectNormalizerWithMetadataAndNameConverter extends AbstractObjectNormalizer |
| 328 | +{ |
| 329 | + public function __construct() |
| 330 | + { |
| 331 | + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); |
| 332 | + parent::__construct($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory)); |
| 333 | + } |
| 334 | + |
| 335 | + protected function extractAttributes($object, $format = null, array $context = []) |
| 336 | + { |
| 337 | + } |
| 338 | + |
| 339 | + protected function getAttributeValue($object, $attribute, $format = null, array $context = []) |
| 340 | + { |
| 341 | + } |
| 342 | + |
| 343 | + protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = []) |
| 344 | + { |
| 345 | + $object->$attribute = $value; |
| 346 | + } |
| 347 | +} |
| 348 | + |
| 349 | +class DummyObjectWithOptionalAttributes |
| 350 | +{ |
| 351 | + /** |
| 352 | + * @SerializedName("#") |
| 353 | + * |
| 354 | + * @var string |
| 355 | + */ |
| 356 | + public $value; |
| 357 | + /** |
| 358 | + * @SerializedName("@foo") |
| 359 | + * |
| 360 | + * @var string |
| 361 | + */ |
| 362 | + public $foo = null; |
| 363 | +} |
| 364 | + |
305 | 365 | class DummyCollection
|
306 | 366 | {
|
307 | 367 | /** @var DummyChild[] */
|
|
0 commit comments