Skip to content

[Serializer] Add ability to skip uninitialized properties #38972

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

Closed
Closed
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* added `FormErrorNormalizer`
* added `MimeMessageNormalizer`
* serializer mapping can be configured using php attributes
* added `skip_uninitialized_properties` context option to not serialize uninitialized properties

5.1.0
-----
Expand Down
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 fields should be skipped
* when normalizing.
*/
public const SKIP_UNINITIALIZED_PROPERTIES = 'skip_uninitialized_properties';

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

$attributeValue = $this->getAttributeValue($object, $attribute, $format, $context);
$attributeValue = null;
try {
$attributeValue = $this->getAttributeValue($object, $attribute, $format, $context);
} catch (UninitializedPropertyException $e) {
if (isset($context[self::SKIP_UNINITIALIZED_PROPERTIES])) {
continue;
}

throw $e;
}

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

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

use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;

class ObjectDummy2
{
public $bar;

//simulate typed property access
public function getFakeUninitializedProperty()
{
throw new UninitializedPropertyException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

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

/**
* Test AbstractObjectNormalizer::SKIP_UNINITIALIZED_PROPERTIES.
*/
trait SkipUninitializedPropertiesTestTrait
{
abstract protected function getNormalizerForSkipUninitializedProperties(): NormalizerInterface;

public function testSkipUninitializedProperties()
{
$dummy = new ObjectDummy2();
$dummy->bar = 'present';

$normalizer = $this->getNormalizerForSkipUninitializedProperties();
$result = $normalizer->normalize($dummy, null, ['skip_uninitialized_properties' => true]);
$this->assertSame(['bar' => 'present'], $result);
}

public function testWithoutSkipUninitializedProperties()
{
$this->expectException(UninitializedPropertyException::class);

$normalizer = $this->getNormalizerForSkipUninitializedProperties();
$normalizer->normalize(new ObjectDummy2(), null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\GroupDummy;
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
use Symfony\Component\Serializer\Tests\Fixtures\OtherSerializedNameDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
Expand All @@ -43,6 +43,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\SkipUninitializedPropertiesTestTrait;
use Symfony\Component\Serializer\Tests\Normalizer\Features\TypeEnforcementTestTrait;

/**
Expand All @@ -59,6 +60,7 @@ class ObjectNormalizerTest extends TestCase
use MaxDepthTestTrait;
use ObjectToPopulateTestTrait;
use SkipNullValuesTestTrait;
use SkipUninitializedPropertiesTestTrait;
use TypeEnforcementTestTrait;

/**
Expand Down Expand Up @@ -506,6 +508,13 @@ protected function getNormalizerForSkipNullValues(): ObjectNormalizer
return new ObjectNormalizer();
}

// skip uninitialized

protected function getNormalizerForSkipUninitializedProperties(): ObjectNormalizer
{
return new ObjectNormalizer();
}

// type enforcement

protected function getDenormalizerForTypeEnforcement(): ObjectNormalizer
Expand Down