Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e5bbf99

Browse filesBrowse files
committed
[Serializer] Fix access to private when Ignore
1 parent 48be4b3 commit e5bbf99
Copy full SHA for e5bbf99

File tree

3 files changed

+26
-11
lines changed
Filter options

3 files changed

+26
-11
lines changed

‎src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+6-10Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,22 +302,18 @@ protected function getAttributes(object $object, ?string $format, array $context
302302
return $this->attributesCache[$key];
303303
}
304304

305-
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
306-
307-
if (false !== $allowedAttributes) {
308-
if ($context['cache_key']) {
309-
$this->attributesCache[$key] = $allowedAttributes;
310-
}
311-
312-
return $allowedAttributes;
313-
}
314-
315305
$attributes = $this->extractAttributes($object, $format, $context);
316306

317307
if ($this->classDiscriminatorResolver && $mapping = $this->classDiscriminatorResolver->getMappingForMappedObject($object)) {
318308
array_unshift($attributes, $mapping->getTypeProperty());
319309
}
320310

311+
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
312+
313+
if (false !== $allowedAttributes) {
314+
$attributes = array_intersect($attributes, $allowedAttributes);
315+
}
316+
321317
if ($context['cache_key'] && \stdClass::class !== $class) {
322318
$this->attributesCache[$key] = $attributes;
323319
}

‎src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1717
use Symfony\Component\PropertyInfo\Type;
18+
use Symfony\Component\Serializer\Annotation\Ignore;
1819
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
1920
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
2021
use Symfony\Component\Serializer\Exception\LogicException;
@@ -444,6 +445,14 @@ public function testNormalizeEmptyObject()
444445
$normalizedData = $normalizer->normalize(new EmptyDummy(), 'any', ['preserve_empty_objects' => true]);
445446
$this->assertEquals(new \ArrayObject(), $normalizedData);
446447
}
448+
449+
public function testNormalizeWithIgnoreAnnotationAndPrivateProperties()
450+
{
451+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
452+
$serializer = new Serializer([new ObjectNormalizer($classMetadataFactory)]);
453+
454+
$this->assertSame(['foo' => 'foo'], $serializer->normalize(new ObjectDummyWithIgnoreAnnotationAndPrivateProperty()));
455+
}
447456
}
448457

449458
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
@@ -484,6 +493,16 @@ class EmptyDummy
484493
{
485494
}
486495

496+
class ObjectDummyWithIgnoreAnnotationAndPrivateProperty
497+
{
498+
public $foo = 'foo';
499+
500+
/** @Ignore */
501+
public $ignored = 'ignored';
502+
503+
private $private = 'private';
504+
}
505+
487506
class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer
488507
{
489508
public function __construct()

‎src/Symfony/Component/Serializer/Tests/SerializerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/SerializerTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ public function testDeserializeAndSerializeInterfacedObjectsWithTheClassMetadata
471471
'groups' => ['two'],
472472
]);
473473

474-
$this->assertEquals('{"two":2,"type":"one"}', $serialized);
474+
$this->assertEquals('{"type":"one","two":2}', $serialized);
475475
}
476476

477477
public function testDeserializeAndSerializeNestedInterfacedObjectsWithTheClassMetadataDiscriminator()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.