Skip to content

[Serializer] Support canners in object normalizer #45282

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
Apr 27, 2022
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CHANGELOG
* Deprecate `ContextAwareDecoderInterface`, use `DecoderInterface` instead
* Deprecate supporting denormalization for `AbstractUid` in `UidNormalizer`, use one of `AbstractUid` child class instead
* Deprecate denormalizing to an abstract class in `UidNormalizer`
* Add support for `can*()` methods to `ObjectNormalizer`

6.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ protected function extractAttributes(object $object, string $format = null, arra
$name = $reflMethod->name;
$attributeName = null;

if (str_starts_with($name, 'get') || str_starts_with($name, 'has')) {
// getters and hassers
if (str_starts_with($name, 'get') || str_starts_with($name, 'has') || str_starts_with($name, 'can')) {
// getters, hassers and canners
$attributeName = substr($name, 3);

if (!$reflClass->hasProperty($attributeName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ObjectDummy
private $baz;
protected $camelCase;
protected $object;
private $go;

public function getFoo()
{
Expand Down Expand Up @@ -72,4 +73,14 @@ public function getObject()
{
return $this->object;
}

public function setGo($go)
{
$this->go = $go;
}

public function canGo()
{
return $this->go;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function testNormalize()
$obj->setBaz(true);
$obj->setCamelCase('camelcase');
$obj->setObject($object);
$obj->setGo(true);

$this->serializer
->expects($this->once())
Expand All @@ -123,6 +124,7 @@ public function testNormalize()
'fooBar' => 'foobar',
'camelCase' => 'camelcase',
'object' => 'string_object',
'go' => true,
],
$this->normalizer->normalize($obj, 'any')
);
Expand Down Expand Up @@ -669,6 +671,7 @@ public function testNormalizeNotSerializableContext()
'camelCase' => null,
'object' => null,
'bar' => null,
'go' => null,
];

$this->assertEquals($expected, $this->normalizer->normalize($objectDummy, null, ['not_serializable' => function () {
Expand Down Expand Up @@ -805,6 +808,7 @@ public function testDefaultObjectClassResolver()
$obj->setBaz(true);
$obj->setCamelCase('camelcase');
$obj->unwantedProperty = 'notwanted';
$obj->setGo(false);

$this->assertEquals(
[
Expand All @@ -814,6 +818,7 @@ public function testDefaultObjectClassResolver()
'fooBar' => 'foobar',
'camelCase' => 'camelcase',
'object' => null,
'go' => false,
],
$normalizer->normalize($obj, 'any')
);
Expand Down Expand Up @@ -842,6 +847,7 @@ public function testObjectClassResolver()
'fooBar' => 'foobar',
'camelCase' => 'camelcase',
'object' => null,
'go' => null,
],
$normalizer->normalize($obj, 'any')
);
Expand Down