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 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
Prev Previous commit
Next Next commit
Add test SKIP_UNINITIALIZED_PROPERTIES
  • Loading branch information
idsulik committed Nov 3, 2020
commit c4d4a83b52be5afa37deb9d18eadeb2bc82b7795
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 @@ -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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy