Skip to content

[Serializer] Allow to denormalize objects that have constructor checks without using constructor #45508

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
feat: Allow to denormalize objects that have constructor checks witho…
…ut using constructor
  • Loading branch information
Frederic Biesse committed Feb 22, 2022
commit 2b68bfb139a4cce7359b860d48da384f12f3b78d
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CHANGELOG

* Remove `ArrayDenormalizer::setSerializer()`, call `setDenormalizer()` instead
* Remove the ability to create instances of the annotation classes by passing an array of parameters, use named arguments instead
* Add support for denormalizing objects without using constructor

5.4
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
*/
public const DEFAULT_CONSTRUCTOR_ARGUMENTS = 'default_constructor_arguments';

/**
* Force to create new instance without using constructor.
*/
public const CREATE_INSTANCE_WITHOUT_CONSTRUCTOR = 'create_instance_without_constructor';

/**
* Hashmap of field name => callable to (de)normalize this field.
*
Expand Down Expand Up @@ -325,7 +330,12 @@ protected function instantiateObject(array &$data, string $class, array &$contex

$constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes);
if ($constructor) {
if (true !== $constructor->isPublic()) {
$createInstanceWithoutConstructor = (
!$constructor->isPublic()
|| (
$context[self::CREATE_INSTANCE_WITHOUT_CONSTRUCTOR] ?? $this->defaultContext[self::CREATE_INSTANCE_WITHOUT_CONSTRUCTOR] ?? false)
);
if ($createInstanceWithoutConstructor) {
return $reflectionClass->newInstanceWithoutConstructor();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,25 @@ public function testConstructorDenormalizeWithNullArgument()
$this->assertEquals('bar', $obj->getBar());
}

public function testConstructorDenormalizeWithInvalidArgument()
{
$obj = $this->normalizer->denormalize(
['foo' => 'foo', 'bar' => 'bar'],
PropertyConstructorWithValidationDummy::class,
null,
['create_instance_without_constructor' => true]
);
$this->assertSame('foo', $obj->getFoo());
$this->assertSame('bar', $obj->getBar());

$this->expectException(\LogicException::class);
$this->expectErrorMessage(sprintf('Argument "%s" is not acceptable as first argument of "%s"', 'foo', PropertyConstructorWithValidationDummy::class));
$this->normalizer->denormalize(
['foo' => 'foo', 'bar' => 'bar'],
PropertyConstructorWithValidationDummy::class
);
}

protected function getNormalizerForCallbacks(): PropertyNormalizer
{
return new PropertyNormalizer();
Expand Down Expand Up @@ -516,6 +535,34 @@ public function getBar()
}
}

class PropertyConstructorWithValidationDummy
{
protected $foo;
private $bar;

public function __construct($foo, $bar)
{
if ('foo' === $foo) {
throw new \LogicException(sprintf('Argument "%s" is not acceptable as first argument of "%s"', $foo, self::class));
}
if ('bar' === $bar) {
throw new \LogicException(sprintf('Argument "%s" is not acceptable as second argument of "%s"', $bar, self::class));
}
$this->foo = $foo;
$this->bar = $bar;
}

public function getFoo()
{
return $this->foo;
}

public function getBar()
{
return $this->bar;
}
}

class StaticPropertyDummy
{
private static $property = 'value';
Expand Down