Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #31390 [Serializer] UnwrappingDenormalizer (nonanerz)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Serializer] UnwrappingDenormalizer | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a UnwrappingDenormalizer, registered with very high priority. Unwrapping the data if UNWRAP_PATH is provided. Very often some APIs give nested responses in which we need only the child object. With UnwrappingDenormalizer we can get the needed object without creating unnecessary Model class that we don't really need. Regarding to #28887 and #30894 Usage: `$serialiser->deserialize('{"baz": {"foo": "bar", "inner": {"title": "value", "numbers": [5,3]}}}', Object::class, ['UnwrappingDenormalizer::UNWRAP_PATH' => '[baz][inner]'])` Commits ------- 00d103d UnwrappingDenormalizer
- Loading branch information
Showing
5 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/Symfony/Component/Serializer/Normalizer/UnwrappingDenormalizer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Normalizer; | ||
|
||
use Symfony\Component\PropertyAccess\PropertyAccess; | ||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | ||
use Symfony\Component\Serializer\SerializerAwareInterface; | ||
use Symfony\Component\Serializer\SerializerAwareTrait; | ||
|
||
/** | ||
* @author Eduard Bulava <bulavaeduard@gmail.com> | ||
*/ | ||
final class UnwrappingDenormalizer implements DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface | ||
{ | ||
use SerializerAwareTrait; | ||
|
||
const UNWRAP_PATH = 'unwrap_path'; | ||
|
||
private $propertyAccessor; | ||
|
||
public function __construct(PropertyAccessorInterface $propertyAccessor = null) | ||
{ | ||
$this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function denormalize($data, $class, string $format = null, array $context = []) | ||
{ | ||
$propertyPath = $context[self::UNWRAP_PATH]; | ||
$context['unwrapped'] = true; | ||
|
||
if ($propertyPath) { | ||
if (!$this->propertyAccessor->isReadable($data, $propertyPath)) { | ||
return null; | ||
} | ||
|
||
$data = $this->propertyAccessor->getValue($data, $propertyPath); | ||
} | ||
|
||
return $this->serializer->denormalize($data, $class, $format, $context); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDenormalization($data, $type, string $format = null, array $context = []) | ||
{ | ||
return \array_key_exists(self::UNWRAP_PATH, $context) && !isset($context['unwrapped']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasCacheableSupportsMethod(): bool | ||
{ | ||
return $this->serializer instanceof CacheableSupportsMethodInterface && $this->serializer->hasCacheableSupportsMethod(); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/Symfony/Component/Serializer/Tests/Normalizer/UnwrappinDenormalizerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Normalizer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer; | ||
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectDummy; | ||
|
||
/** | ||
* @author Eduard Bulava <bulavaeduard@gmail.com> | ||
*/ | ||
class UnwrappinDenormalizerTest extends TestCase | ||
{ | ||
private $denormalizer; | ||
|
||
private $serializer; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->serializer = $this->getMockBuilder('Symfony\Component\Serializer\Serializer')->getMock(); | ||
$this->denormalizer = new UnwrappingDenormalizer(); | ||
$this->denormalizer->setSerializer($this->serializer); | ||
} | ||
|
||
public function testSupportsNormalization() | ||
{ | ||
$this->assertTrue($this->denormalizer->supportsDenormalization([], new \stdClass(), 'any', [UnwrappingDenormalizer::UNWRAP_PATH => '[baz][inner]'])); | ||
$this->assertFalse($this->denormalizer->supportsDenormalization([], new \stdClass(), 'any', [UnwrappingDenormalizer::UNWRAP_PATH => '[baz][inner]', 'unwrapped' => true])); | ||
$this->assertFalse($this->denormalizer->supportsDenormalization([], new \stdClass(), 'any', [])); | ||
} | ||
|
||
public function testDenormalize() | ||
{ | ||
$expected = new ObjectDummy(); | ||
$expected->setBaz(true); | ||
$expected->bar = 'bar'; | ||
$expected->setFoo('foo'); | ||
|
||
$this->serializer->expects($this->exactly(1)) | ||
->method('denormalize') | ||
->with(['foo' => 'foo', 'bar' => 'bar', 'baz' => true]) | ||
->willReturn($expected); | ||
|
||
$result = $this->denormalizer->denormalize( | ||
['data' => ['foo' => 'foo', 'bar' => 'bar', 'baz' => true]], | ||
ObjectDummy::class, | ||
'any', | ||
[UnwrappingDenormalizer::UNWRAP_PATH => '[data]'] | ||
); | ||
|
||
$this->assertEquals('foo', $result->getFoo()); | ||
$this->assertEquals('bar', $result->bar); | ||
$this->assertTrue($result->isBaz()); | ||
} | ||
|
||
public function testDenormalizeInvalidPath() | ||
{ | ||
$this->serializer->expects($this->exactly(1)) | ||
->method('denormalize') | ||
->with(null) | ||
->willReturn(new ObjectDummy()); | ||
|
||
$obj = $this->denormalizer->denormalize( | ||
['data' => ['foo' => 'foo', 'bar' => 'bar', 'baz' => true]], | ||
ObjectDummy::class, | ||
'any', | ||
[UnwrappingDenormalizer::UNWRAP_PATH => '[invalid]'] | ||
); | ||
|
||
$this->assertNull($obj->getFoo()); | ||
$this->assertNull($obj->bar); | ||
$this->assertNull($obj->isBaz()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters