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,28 @@ 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
+
236
+ // if an xml-node can have children which should be deserialized as string[]
237
+ // and only one child exists
238
+ $ object = $ denormalizer ->denormalize ("string-value " , DummyObjectWithOptionalAttributes::class, 'xml ' );
239
+
240
+ $ this ->assertInstanceOf (DummyObjectWithOptionalAttributes::class, $ object );
241
+ $ this ->assertEquals ("string-value " , $ object ->value );
242
+ $ this ->assertNull ($ object ->foo );
243
+ }
244
+
245
+ public function getDenormalizerForStringNode ()
246
+ {
247
+ $ denormalizer = new AbstractObjectNormalizerWithMetadataAndNameConverter ();
248
+ $ serializer = new Serializer ([$ denormalizer ]);
249
+ $ denormalizer ->setSerializer ($ serializer );
250
+
251
+ return $ denormalizer ;
252
+ }
253
+
230
254
/**
231
255
* Test that additional attributes throw an exception if no metadata factory is specified.
232
256
*/
@@ -296,6 +320,28 @@ protected function setAttributeValue($object, $attribute, $value, $format = null
296
320
}
297
321
}
298
322
323
+ class AbstractObjectNormalizerWithMetadataAndNameConverter extends AbstractObjectNormalizer
324
+ {
325
+ public function __construct ()
326
+ {
327
+ $ classMetadataFactory = new ClassMetadataFactory (new AnnotationLoader (new AnnotationReader ()));
328
+ parent ::__construct ($ classMetadataFactory , new MetadataAwareNameConverter ($ classMetadataFactory ));
329
+ }
330
+
331
+ protected function extractAttributes ($ object , $ format = null , array $ context = [])
332
+ {
333
+ }
334
+
335
+ protected function getAttributeValue ($ object , $ attribute , $ format = null , array $ context = [])
336
+ {
337
+ }
338
+
339
+ protected function setAttributeValue ($ object , $ attribute , $ value , $ format = null , array $ context = [])
340
+ {
341
+ $ object ->$ attribute = $ value ;
342
+ }
343
+ }
344
+
299
345
class StringCollection
300
346
{
301
347
/** @var string[] */
@@ -313,6 +359,21 @@ class DummyChild
313
359
public $ bar ;
314
360
}
315
361
362
+ class DummyObjectWithOptionalAttributes
363
+ {
364
+ /**
365
+ * @SerializedName("#")
366
+ * @var string
367
+ */
368
+ public $ value ;
369
+
370
+ /**
371
+ * @SerializedName("@foo")
372
+ * @var string
373
+ */
374
+ public $ foo = null ;
375
+ }
376
+
316
377
class SerializerCollectionDummy implements SerializerInterface, DenormalizerInterface
317
378
{
318
379
private $ normalizers ;
0 commit comments