Skip to content

[Serializer] Allow (de)normalization of empty objects in PropertyNormalizer #46417

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

Open
wants to merge 2 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[Serializer] Allow (de)normalization of empty objects in PropertyNorm…
…alizer and GetSetMethodNormalizer
  • Loading branch information
olsavmic committed Aug 8, 2022
commit fbfef8965fe20a27d940555b28461a239289365f
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

namespace Symfony\Component\Serializer\Normalizer;

use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

/**
* Converts between objects with getter and setter methods and arrays.
*
Expand All @@ -36,6 +41,21 @@ class GetSetMethodNormalizer extends AbstractObjectNormalizer
{
private static $setterAccessibleCache = [];

private $allowNormalizationOfObjectsWithoutAnyGetters;

public function __construct(
ClassMetadataFactoryInterface $classMetadataFactory = null,
NameConverterInterface $nameConverter = null,
PropertyTypeExtractorInterface $propertyTypeExtractor = null,
ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null,
callable $objectClassResolver = null,
array $defaultContext = [],
bool $allowNormalizationOfObjectsWithoutAnyGetters = false,
) {
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext);
$this->allowNormalizationOfObjectsWithoutAnyGetters = $allowNormalizationOfObjectsWithoutAnyGetters;
}

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -69,6 +89,10 @@ public function hasCacheableSupportsMethod(): bool
*/
private function supports(string $class): bool
{
if ($this->allowNormalizationOfObjectsWithoutAnyGetters) {
return true;
}

$class = new \ReflectionClass($class);
$methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
foreach ($methods as $method) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@ class PropertyNormalizer extends AbstractObjectNormalizer
*/
public const NORMALIZE_VISIBILITY = 'normalize_visibility';

public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = [])
private $allowNormalizationOfObjectsWithoutAnyProperties;

public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = [], bool $allowNormalizationOfObjectsWithoutAnyProperties = false)
{
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext);

if (!isset($this->defaultContext[self::NORMALIZE_VISIBILITY])) {
$this->defaultContext[self::NORMALIZE_VISIBILITY] = self::NORMALIZE_PUBLIC | self::NORMALIZE_PROTECTED | self::NORMALIZE_PRIVATE;
}

$this->allowNormalizationOfObjectsWithoutAnyProperties = $allowNormalizationOfObjectsWithoutAnyProperties;
}

/**
Expand Down Expand Up @@ -87,6 +91,10 @@ public function hasCacheableSupportsMethod(): bool
*/
private function supports(string $class): bool
{
if ($this->allowNormalizationOfObjectsWithoutAnyProperties) {
return true;
}

$class = new \ReflectionClass($class);

// We look for at least one non-static property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,36 @@ public function testRejectInvalidKey()
$this->markTestSkipped('This test makes no sense with the GetSetMethodNormalizer');
}

protected function getNormalizerAllowingObjectsWithoutGetters(): GetSetMethodNormalizer
{
return new GetSetMethodNormalizer(null, null, null, null, null, [], true);
}

public function testNormalizeObjectWithoutAnyProperties()
{
$normalizer = $this->getNormalizerAllowingObjectsWithoutGetters();
$obj = new EmptyObjectDummy();

$this->assertTrue($normalizer->supportsNormalization($obj));

$this->assertEquals(
[],
$normalizer->normalize($obj),
);
}

public function testDenormalizeObjectWithoutAnyProperties()
{
$normalizer = $this->getNormalizerAllowingObjectsWithoutGetters();
$obj = new EmptyObjectDummy();

$this->assertTrue($normalizer->supportsDenormalization($obj, \get_class($obj)));
$this->assertEquals(
$obj,
$normalizer->denormalize([], \get_class($obj)),
);
}

protected function getNormalizerForIgnoredAttributes(): GetSetMethodNormalizer
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
Expand Down Expand Up @@ -722,3 +752,7 @@ public function hasFoo()
return $this->foo;
}
}

class EmptyObjectDummy
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,36 @@ protected function getDenormalizerForGroups(): PropertyNormalizer
return new PropertyNormalizer($classMetadataFactory);
}

protected function getNormalizerAllowingObjectsWithoutProperties(): PropertyNormalizer
{
return new PropertyNormalizer(null, null, null, null, null, [], true);
}

public function testNormalizeObjectWithoutAnyProperties()
{
$normalizer = $this->getNormalizerAllowingObjectsWithoutProperties();
$obj = new StaticPropertyDummy();

$this->assertTrue($normalizer->supportsNormalization($obj));

$this->assertEquals(
[],
$normalizer->normalize($obj),
);
}

public function testDenormalizeObjectWithoutAnyProperties()
{
$normalizer = $this->getNormalizerAllowingObjectsWithoutProperties();
$obj = new StaticPropertyDummy();

$this->assertTrue($normalizer->supportsDenormalization($obj, \get_class($obj)));
$this->assertEquals(
$obj,
$normalizer->denormalize([], \get_class($obj)),
);
}

public function testGroupsNormalizeWithNameConverter()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
Expand Down