Commit 8846f64
committed
bug #46525 [Serializer] Get attributeContext after converting name (zenas1210)
This PR was merged into the 5.4 branch.
Discussion
----------
[Serializer] Get attributeContext after converting name
| Q | A
| ------------- | ---
| Branch? | 5.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets |
| License | MIT
| Doc PR |
Property specific denormalization context doesn't work when property name in the array being denormalized is different from property name in the class.
To reproduce:
```
// composer require symfony/serializer:"5.4.*" doctrine/annotations symfony/property-access
<?php
require_once __DIR__.'/vendor/autoload.php';
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\Serializer\Annotation\Context;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
class Item
{
/**
* `@Context`({ DateTimeNormalizer::FORMAT_KEY = "d/m/Y" })
*/
public \DateTime $createdAt;
}
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$objectNormalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter(), null, new ReflectionExtractor());
$normalizers = [new DateTimeNormalizer(), $objectNormalizer];
new Serializer($normalizers, []);
// outputs 2011-07-28
echo ($objectNormalizer->denormalize(['createdAt' => '28/07/2011'], Item::class))->createdAt->format('Y-m-d') . PHP_EOL;
// crashes because 'created_at' !== 'createdAt'
echo ($objectNormalizer->denormalize(['created_at' => '28/07/2011'], Item::class))->createdAt->format('Y-m-d') . PHP_EOL;
```
Theoretically this could introduce breaking changes to people who use property specific context in their custom name converters, but due to this bug that context reaches the name converter only when the source name already matches the property name, so in that case no name converting is needed anyway and the context is useless.
Moreover, I'm not sure if 'deserialization_path' changing from source name to the converted name is an issue, if it is I can modify my PR to preserve the original functionality.
Commits
-------
48c47df [Serializer] Get attributeContext after converting nameFile tree
2 files changed
+25
-3
lines changed- src/Symfony/Component/Serializer
- Normalizer
- Tests/Normalizer/Features
2 files changed
+25
-3
lines changedLines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
359 | 359 | | |
360 | 360 | | |
361 | 361 | | |
362 | | - | |
363 | | - | |
364 | 362 | | |
365 | | - | |
| 363 | + | |
366 | 364 | | |
367 | 365 | | |
| 366 | + | |
| 367 | + | |
368 | 368 | | |
369 | 369 | | |
370 | 370 | | |
| |||
Lines changed: 22 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
73 | 85 | | |
74 | 86 | | |
75 | 87 | | |
| |||
90 | 102 | | |
91 | 103 | | |
92 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
0 commit comments