Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] Get attributeContext after converting name #46525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2022

Conversation

zenas1210
Copy link
Contributor

@zenas1210 zenas1210 commented May 31, 2022

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.

@chalasr
Copy link
Member

chalasr commented Jun 1, 2022

Good catch, thanks @zenas1210.

@chalasr chalasr merged commit 8846f64 into symfony:5.4 Jun 1, 2022
@fabpot fabpot mentioned this pull request Jun 9, 2022
This was referenced Jun 26, 2022
fabpot added a commit that referenced this pull request Jul 4, 2022
…cedNameConverterInterface::denormalize (ogizanagi)

This PR was merged into the 6.2 branch.

Discussion
----------

[Serializer] Cannot use Context attribute context in AdvancedNameConverterInterface::denormalize

| Q             | A
| ------------- | ---
| Branch?       | 6.2 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #46680 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | N/A

I realized we can't use the `Context` attribute context in an `AdvancedNameConverterInterface::denormalize`, same as spotted already in 5.4 in #46525.

Also adds the missing CHANGELOG entry for #46680

Commits
-------

c99082c [Serializer] Cannot use @context context in AdvancedNameConverterInterface::denormalize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants