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

Skip to content

[Serializer] Add option to skip uninitialized typed properties #41615

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
Sep 8, 2021
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 @@ -13,6 +13,7 @@

use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\Encoder\CsvEncoder;
Expand Down Expand Up @@ -58,6 +59,12 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
*/
public const SKIP_NULL_VALUES = 'skip_null_values';

/**
* Flag to control whether uninitialized PHP>=7.4 typed class properties
* should be excluded when normalizing.
*/
public const SKIP_UNINITIALIZED_VALUES = 'skip_uninitialized_values';

/**
* Callback to allow to set a value for an attribute when the max depth has
* been reached.
Expand Down Expand Up @@ -180,7 +187,16 @@ public function normalize($object, string $format = null, array $context = [])
}

$attributeContext = $this->getAttributeNormalizationContext($object, $attribute, $context);
$attributeValue = $this->getAttributeValue($object, $attribute, $format, $attributeContext);

try {
$attributeValue = $this->getAttributeValue($object, $attribute, $format, $attributeContext);
} catch (UninitializedPropertyException $e) {
if ($context[self::SKIP_UNINITIALIZED_VALUES] ?? $this->defaultContext[self::SKIP_UNINITIALIZED_VALUES] ?? false) {
continue;
}
throw $e;
}

if ($maxDepthReached) {
$attributeValue = $maxDepthHandler($attributeValue, $object, $attribute, $format, $attributeContext);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Symfony\Component\Serializer\Tests\Normalizer\Features;

use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* Test AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES.
*/
trait SkipUninitializedValuesTestTrait
{
abstract protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface;

/**
* @requires PHP 7.4
*/
public function testSkipUninitializedValues()
{
$object = new TypedPropertiesObject();

$normalizer = $this->getNormalizerForSkipUninitializedValues();
$result = $normalizer->normalize($object, null, ['skip_uninitialized_values' => true, 'groups' => ['foo']]);
$this->assertSame(['initialized' => 'value'], $result);
}

/**
* @requires PHP 7.4
*/
public function testWithoutSkipUninitializedValues()
{
$object = new TypedPropertiesObject();

$normalizer = $this->getNormalizerForSkipUninitializedValues();
$this->expectException(UninitializedPropertyException::class);
$normalizer->normalize($object, null, ['groups' => ['foo']]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Symfony\Component\Serializer\Tests\Normalizer\Features;

use Symfony\Component\Serializer\Annotation\Groups;

class TypedPropertiesObject
{
/**
* @Groups({"foo"})
*/
public string $unInitialized;

/**
* @Groups({"foo"})
*/
public string $initialized = 'value';

/**
* @Groups({"bar"})
*/
public string $initialized2 = 'value';
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectDummy;
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectToPopulateTestTrait;
use Symfony\Component\Serializer\Tests\Normalizer\Features\SkipNullValuesTestTrait;
use Symfony\Component\Serializer\Tests\Normalizer\Features\SkipUninitializedValuesTestTrait;
use Symfony\Component\Serializer\Tests\Normalizer\Features\TypeEnforcementTestTrait;

/**
Expand All @@ -66,6 +67,7 @@ class ObjectNormalizerTest extends TestCase
use MaxDepthTestTrait;
use ObjectToPopulateTestTrait;
use SkipNullValuesTestTrait;
use SkipUninitializedValuesTestTrait;
use TypeEnforcementTestTrait;

/**
Expand Down Expand Up @@ -534,6 +536,15 @@ protected function getNormalizerForSkipNullValues(): ObjectNormalizer
return new ObjectNormalizer();
}

// skip uninitialized

protected function getNormalizerForSkipUninitializedValues(): ObjectNormalizer
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

return new ObjectNormalizer($classMetadataFactory);
}

// type enforcement

protected function getDenormalizerForTypeEnforcement(): ObjectNormalizer
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Serializer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"symfony/http-foundation": "^4.4|^5.0|^6.0",
"symfony/http-kernel": "^4.4|^5.0|^6.0",
"symfony/mime": "^4.4|^5.0|^6.0",
"symfony/property-access": "^4.4.9|^5.0.9|^6.0",
"symfony/property-access": "^5.1|^6.0",
"symfony/property-info": "^5.3|^6.0",
"symfony/uid": "^5.1|^6.0",
"symfony/validator": "^4.4|^5.0|^6.0",
Expand All @@ -46,7 +46,7 @@
"phpdocumentor/reflection-docblock": "<3.2.2",
"phpdocumentor/type-resolver": "<1.4.0",
"symfony/dependency-injection": "<4.4",
"symfony/property-access": "<4.4",
"symfony/property-access": "<5.1",
"symfony/property-info": "<5.3",
"symfony/yaml": "<4.4"
},
Expand Down