Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0ff624c

Browse files
committed
[Serializer] Fix SerializedPath not working with constructor arguments
1 parent2407467 commit0ff624c

File tree

10 files changed

+291
-19
lines changed

10 files changed

+291
-19
lines changed

‎src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -226,19 +226,8 @@ public function normalize(mixed $object, string $format = null, array $context =
226226

227227
protectedfunctioninstantiateObject(array &$data,string$class,array &$context,\ReflectionClass$reflectionClass,array|bool$allowedAttributes,string$format =null)
228228
{
229-
if ($this->classDiscriminatorResolver &&$mapping =$this->classDiscriminatorResolver->getMappingForClass($class)) {
230-
if (!isset($data[$mapping->getTypeProperty()])) {
231-
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Type property "%s" not found for the abstract object "%s".',$mapping->getTypeProperty(),$class),null, ['string'],isset($context['deserialization_path']) ?$context['deserialization_path'].'.'.$mapping->getTypeProperty() :$mapping->getTypeProperty(),false);
232-
}
233-
234-
$type =$data[$mapping->getTypeProperty()];
235-
if (null === ($mappedClass =$mapping->getClassForType($type))) {
236-
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type "%s" is not a valid value.',$type),$type, ['string'],isset($context['deserialization_path']) ?$context['deserialization_path'].'.'.$mapping->getTypeProperty() :$mapping->getTypeProperty(),true);
237-
}
238-
239-
if ($mappedClass !==$class) {
240-
return$this->instantiateObject($data,$mappedClass,$context,new \ReflectionClass($mappedClass),$allowedAttributes,$format);
241-
}
229+
if ($class !==$mappedClass =$this->getMappedClass($data,$class,$context)) {
230+
return$this->instantiateObject($data,$mappedClass,$context,new \ReflectionClass($mappedClass),$allowedAttributes,$format);
242231
}
243232

244233
returnparent::instantiateObject($data,$class,$context,$reflectionClass,$allowedAttributes,$format);
@@ -319,11 +308,9 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
319308
$normalizedData =$this->prepareForDenormalization($data);
320309
$extraAttributes = [];
321310

322-
$reflectionClass =new \ReflectionClass($type);
323-
$object =$this->instantiateObject($normalizedData,$type,$context,$reflectionClass,$allowedAttributes,$format);
324-
$resolvedClass =$this->objectClassResolver ? ($this->objectClassResolver)($object) :$object::class;
311+
$mappedClass =$this->getMappedClass($normalizedData,$type,$context);
325312

326-
$nestedAttributes =$this->getNestedAttributes($resolvedClass);
313+
$nestedAttributes =$this->getNestedAttributes($mappedClass);
327314
$nestedData = [];
328315
$propertyAccessor = PropertyAccess::createPropertyAccessor();
329316
foreach ($nestedAttributesas$property =>$serializedPath) {
@@ -336,6 +323,9 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
336323

337324
$normalizedData =array_merge($normalizedData,$nestedData);
338325

326+
$object =$this->instantiateObject($normalizedData,$mappedClass,$context,new \ReflectionClass($mappedClass),$allowedAttributes,$format);
327+
$resolvedClass =$this->objectClassResolver ? ($this->objectClassResolver)($object) :$object::class;
328+
339329
foreach ($normalizedDataas$attribute =>$value) {
340330
if ($this->nameConverter) {
341331
$notConverted =$attribute;
@@ -792,4 +782,25 @@ private function removeNestedValue(array $path, array $data): array
792782

793783
return$data;
794784
}
785+
786+
/**
787+
* @return class-string
788+
*/
789+
privatefunctiongetMappedClass(array$data,string$class,array$context):string
790+
{
791+
if ($this->classDiscriminatorResolver &&$mapping =$this->classDiscriminatorResolver->getMappingForClass($class)) {
792+
if (!isset($data[$mapping->getTypeProperty()])) {
793+
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Type property "%s" not found for the abstract object "%s".',$mapping->getTypeProperty(),$class),null, ['string'],isset($context['deserialization_path']) ?$context['deserialization_path'].'.'.$mapping->getTypeProperty() :$mapping->getTypeProperty(),false);
794+
}
795+
796+
$type =$data[$mapping->getTypeProperty()];
797+
if (null ===$mappedClass =$mapping->getClassForType($type)) {
798+
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type "%s" is not a valid value.',$type),$type, ['string'],isset($context['deserialization_path']) ?$context['deserialization_path'].'.'.$mapping->getTypeProperty() :$mapping->getTypeProperty(),true);
799+
}
800+
801+
return$mappedClass;
802+
}
803+
804+
return$class;
805+
}
795806
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Serializer\Tests\Fixtures\Annotations;
13+
14+
useSymfony\Component\Serializer\Annotation\SerializedPath;
15+
16+
class SerializedPathInConstructorDummy
17+
{
18+
publicfunction__construct(
19+
/**
20+
* @SerializedPath("[one][two]")
21+
*/
22+
public$three,
23+
) {
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Serializer\Tests\Fixtures\Attributes;
13+
14+
useSymfony\Component\Serializer\Annotation\SerializedPath;
15+
16+
class SerializedPathInConstructorDummy
17+
{
18+
publicfunction__construct(
19+
#[SerializedPath('[one][two]')]public$three,
20+
) {
21+
}
22+
}

‎src/Symfony/Component/Serializer/Tests/Fixtures/serialization.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
<attributename="seven"serialized-path="[three][four]" />
3131
</class>
3232

33+
<classname="Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedPathInConstructorDummy">
34+
<attributename="three"serialized-path="[one][two]" />
35+
</class>
36+
3337
<classname="Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy">
3438
<discriminator-maptype-property="type">
3539
<mappingtype="first"class="Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyFirstChild" />

‎src/Symfony/Component/Serializer/Tests/Fixtures/serialization.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
serialized_path:'[one][two]'
2323
seven:
2424
serialized_path:'[three][four]'
25+
'Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedPathInConstructorDummy':
26+
attributes:
27+
three:
28+
serialized_path:'[one][two]'
2529
'Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy':
2630
discriminator_map:
2731
type_property:type

‎src/Symfony/Component/Serializer/Tests/Mapping/Factory/ClassMetadataFactoryCompilerTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
useSymfony\Component\Serializer\Tests\Fixtures\Annotations\MaxDepthDummy;
2020
useSymfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedNameDummy;
2121
useSymfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedPathDummy;
22+
useSymfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedPathInConstructorDummy;
2223
useSymfony\Component\Serializer\Tests\Fixtures\Dummy;
2324

2425
finalclass ClassMetadataFactoryCompilerTestextends TestCase
@@ -46,18 +47,20 @@ public function testItDumpMetadata()
4647
$maxDepthDummyMetadata =$classMetatadataFactory->getMetadataFor(MaxDepthDummy::class);
4748
$serializedNameDummyMetadata =$classMetatadataFactory->getMetadataFor(SerializedNameDummy::class);
4849
$serializedPathDummyMetadata =$classMetatadataFactory->getMetadataFor(SerializedPathDummy::class);
50+
$serializedPathInConstructorDummyMetadata =$classMetatadataFactory->getMetadataFor(SerializedPathInConstructorDummy::class);
4951

5052
$code = (newClassMetadataFactoryCompiler())->compile([
5153
$dummyMetadata,
5254
$maxDepthDummyMetadata,
5355
$serializedNameDummyMetadata,
5456
$serializedPathDummyMetadata,
57+
$serializedPathInConstructorDummyMetadata,
5558
]);
5659

5760
file_put_contents($this->dumpPath,$code);
5861
$compiledMetadata =require$this->dumpPath;
5962

60-
$this->assertCount(4,$compiledMetadata);
63+
$this->assertCount(5,$compiledMetadata);
6164

6265
$this->assertArrayHasKey(Dummy::class,$compiledMetadata);
6366
$this->assertEquals([
@@ -99,5 +102,13 @@ public function testItDumpMetadata()
99102
],
100103
null,
101104
],$compiledMetadata[SerializedPathDummy::class]);
105+
106+
$this->assertArrayHasKey(SerializedPathInConstructorDummy::class,$compiledMetadata);
107+
$this->assertEquals([
108+
[
109+
'three' => [[],null,null,'[one][two]'],
110+
],
111+
null,
112+
],$compiledMetadata[SerializedPathInConstructorDummy::class]);
102113
}
103114
}

‎src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTestCase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ public function testLoadSerializedPath()
103103
$this->assertEquals(newPropertyPath('[three][four]'),$attributesMetadata['seven']->getSerializedPath());
104104
}
105105

106+
publicfunctiontestLoadSerializedPathInConstructor()
107+
{
108+
$classMetadata =newClassMetadata($this->getNamespace().'\SerializedPathInConstructorDummy');
109+
$this->loader->loadClassMetadata($classMetadata);
110+
111+
$attributesMetadata =$classMetadata->getAttributesMetadata();
112+
$this->assertEquals(newPropertyPath('[one][two]'),$attributesMetadata['three']->getSerializedPath());
113+
}
114+
106115
publicfunctiontestLoadClassMetadataAndMerge()
107116
{
108117
$classMetadata =newClassMetadata($this->getNamespace().'\GroupDummy');

‎src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ public function testSerializedPath()
9494
$this->assertEquals('[three][four]',$attributesMetadata['seven']->getSerializedPath());
9595
}
9696

97+
publicfunctiontestSerializedPathInConstructor()
98+
{
99+
$classMetadata =newClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedPathInConstructorDummy');
100+
$this->loader->loadClassMetadata($classMetadata);
101+
102+
$attributesMetadata =$classMetadata->getAttributesMetadata();
103+
$this->assertEquals('[one][two]',$attributesMetadata['three']->getSerializedPath());
104+
}
105+
97106
publicfunctiontestLoadDiscriminatorMap()
98107
{
99108
$classMetadata =newClassMetadata(AbstractDummy::class);

‎src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ public function testSerializedPath()
108108
$this->assertEquals(newPropertyPath('[three][four]'),$attributesMetadata['seven']->getSerializedPath());
109109
}
110110

111+
publicfunctiontestSerializedPathInConstructor()
112+
{
113+
$classMetadata =newClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\Annotations\SerializedPathInConstructorDummy');
114+
$this->loader->loadClassMetadata($classMetadata);
115+
116+
$attributesMetadata =$classMetadata->getAttributesMetadata();
117+
$this->assertEquals(newPropertyPath('[one][two]'),$attributesMetadata['three']->getSerializedPath());
118+
}
119+
111120
publicfunctiontestLoadDiscriminatorMap()
112121
{
113122
$classMetadata =newClassMetadata(AbstractDummy::class);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp