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

Skip to content

[Serializer] Fix property name usage for denormalization #34035

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Serializer\NameConverter;

use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

/**
* @author Fabien Bourigault <[email protected]>
Expand All @@ -25,11 +26,11 @@ final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
*/
private $fallbackNameConverter;

private static $normalizeCache = [];
private $normalizeCache = [];

private static $denormalizeCache = [];
private $denormalizeCache = [];

private static $attributesMetadataCache = [];
private $attributesMetadataCache = [];

public function __construct(ClassMetadataFactoryInterface $metadataFactory, NameConverterInterface $fallbackNameConverter = null)
{
Expand All @@ -46,11 +47,11 @@ public function normalize($propertyName, string $class = null, string $format =
return $this->normalizeFallback($propertyName, $class, $format, $context);
}

if (!isset(self::$normalizeCache[$class][$propertyName])) {
self::$normalizeCache[$class][$propertyName] = $this->getCacheValueForNormalization($propertyName, $class);
if (!isset($this->normalizeCache[$class][$propertyName])) {
$this->normalizeCache[$class][$propertyName] = $this->getCacheValueForNormalization($propertyName, $class);
}

return self::$normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName, $class, $format, $context);
return $this->normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName, $class, $format, $context);
}

/**
Expand All @@ -62,11 +63,11 @@ public function denormalize($propertyName, string $class = null, string $format
return $this->denormalizeFallback($propertyName, $class, $format, $context);
}

if (!isset(self::$denormalizeCache[$class][$propertyName])) {
self::$denormalizeCache[$class][$propertyName] = $this->getCacheValueForDenormalization($propertyName, $class);
if (!isset($this->denormalizeCache[$class][$propertyName])) {
$this->denormalizeCache[$class][$propertyName] = $this->getCacheValueForDenormalization($propertyName, $class, $context);
}

return self::$denormalizeCache[$class][$propertyName] ?? $this->denormalizeFallback($propertyName, $class, $format, $context);
return $this->denormalizeCache[$class][$propertyName] ?? $this->denormalizeFallback($propertyName, $class, $format, $context);
}

private function getCacheValueForNormalization($propertyName, string $class)
Expand All @@ -88,21 +89,21 @@ private function normalizeFallback($propertyName, string $class = null, string $
return $this->fallbackNameConverter ? $this->fallbackNameConverter->normalize($propertyName, $class, $format, $context) : $propertyName;
}

private function getCacheValueForDenormalization($propertyName, string $class)
private function getCacheValueForDenormalization($propertyName, string $class, $context)
{
if (!isset(self::$attributesMetadataCache[$class])) {
self::$attributesMetadataCache[$class] = $this->getCacheValueForAttributesMetadata($class);
if (!isset($this->attributesMetadataCache[$class])) {
$this->attributesMetadataCache[$class] = $this->getCacheValueForAttributesMetadata($class, $context);
}

return self::$attributesMetadataCache[$class][$propertyName] ?? null;
return $this->attributesMetadataCache[$class][$propertyName] ?? null;
}

private function denormalizeFallback($propertyName, string $class = null, string $format = null, array $context = [])
{
return $this->fallbackNameConverter ? $this->fallbackNameConverter->denormalize($propertyName, $class, $format, $context) : $propertyName;
}

private function getCacheValueForAttributesMetadata(string $class): array
private function getCacheValueForAttributesMetadata(string $class, $context): array
{
if (!$this->metadataFactory->hasMetadataFor($class)) {
return [];
Expand All @@ -116,6 +117,14 @@ private function getCacheValueForAttributesMetadata(string $class): array
continue;
}

$groups = $metadata->getGroups();
if (!$groups && ($context[AbstractNormalizer::GROUPS] ?? [])) {
continue;
}
if ($groups && !array_intersect($groups, $context[AbstractNormalizer::GROUPS] ?? [])) {
continue;
}

$cache[$metadata->getSerializedName()] = $name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
*
* @param object $object
* @param string|null $format
* @param array $context
*
* @return string[]
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures;

use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;

/**
* @author Anthony GRASSIOT <[email protected]>
*/
class OtherSerializedNameDummy
{
/**
* @Groups({"a"})
*/
private $buz;

public function setBuz($buz)
{
$this->buz = $buz;
}

public function getBuz()
{
return $this->buz;
}

/**
* @Groups({"b"})
* @SerializedName("buz")
*/
public function getBuzForExport()
{
return $this->buz.' Rocks';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
use Symfony\Component\Serializer\Tests\Fixtures\SerializedNameDummy;

/**
Expand Down Expand Up @@ -115,4 +116,26 @@ public function fallbackAttributeProvider()
[0, 0],
];
}

/**
* @dataProvider attributeAndContextProvider
*/
public function testDenormalizeWithGroups($expected, $propertyName, $context = [])
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

$nameConverter = new MetadataAwareNameConverter($classMetadataFactory);

$this->assertEquals($expected, $nameConverter->denormalize($propertyName, OtherSerializedNameDummy::class, null, $context));
}

public function attributeAndContextProvider()
{
return [
['buz', 'buz', ['groups' => ['a']]],
['buzForExport', 'buz', ['groups' => ['b']]],
['buz', 'buz', ['groups' => ['c']]],
['buz', 'buz', []],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
use Symfony\Component\Serializer\Tests\Normalizer\Features\AttributesTestTrait;
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksObject;
Expand Down Expand Up @@ -481,6 +482,23 @@ public function testGroupsDenormalizeWithNameConverter()
);
}

public function testGroupsDenormalizeWithMetaDataNameConverter()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
$this->normalizer->setSerializer($this->serializer);

$obj = new OtherSerializedNameDummy();
$obj->setBuz('Aldrin');

$this->assertEquals(
$obj,
$this->normalizer->denormalize([
'buz' => 'Aldrin',
], 'Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy', null, [ObjectNormalizer::GROUPS => ['a']])
);
}

// ignored attributes

protected function getNormalizerForIgnoredAttributes(): ObjectNormalizer
Expand Down