Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f735e2d

Browse files
Merge branch '6.4' into 7.3
* 6.4: [Serializer] Fix #[Ignore] on same-name properties <-> methods
2 parents 5dfbe28 + 711ffd6 commit f735e2d

2 files changed

Lines changed: 69 additions & 3 deletions

File tree

Mapping/Loader/AttributeLoader.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
172172

173173
$attributeMetadata->setSerializedPath($attribute->getSerializedPath());
174174
} elseif ($attribute instanceof Ignore) {
175-
if (!$accessorOrMutator && !$hasProperty) {
176-
$attributeMetadata->setIgnore(true);
177-
}
175+
$attributeMetadata->setIgnore(true);
178176
} elseif ($attribute instanceof Context) {
179177
if (!$accessorOrMutator && !$hasProperty) {
180178
throw new MappingException(\sprintf('Context on "%s::%s()" cannot be added. Context can only be added on methods beginning with "get", "is", "has", "can" or "set".', $className, $method->name));

Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,28 @@ public function testNormalizeObjectWithMethodSameNameAsProperty()
11561156
$this->assertSame([], $normalizer->normalize($object, null, ['groups' => 'bar']));
11571157
}
11581158

1159+
public function testIgnoreAttributeOnMethodWithSameNameAsProperty()
1160+
{
1161+
$normalizer = new ObjectNormalizer(new ClassMetadataFactory(new AttributeLoader()));
1162+
1163+
$object = new ObjectWithIgnoredMethodSameNameAsProperty('should_be_ignored', 'should_be_serialized');
1164+
1165+
$this->assertSame(['visible' => 'should_be_serialized'], $normalizer->normalize($object));
1166+
}
1167+
1168+
public function testIgnoreAttributeOnMethodWithSameNameAsPropertyWithGroups()
1169+
{
1170+
$normalizer = new ObjectNormalizer(new ClassMetadataFactory(new AttributeLoader()));
1171+
1172+
$object = new ObjectWithIgnoredMethodSameNameAsPropertyWithGroups('ignored', 'visible_default', 'visible_group');
1173+
1174+
// without groups - should include both visible properties
1175+
$this->assertSame(['visibleDefault' => 'visible_default', 'visibleGroup' => 'visible_group'], $normalizer->normalize($object));
1176+
1177+
// with groups - should only include group-specific property, ignored method should never appear
1178+
$this->assertSame(['visibleGroup' => 'visible_group'], $normalizer->normalize($object, null, ['groups' => ['group1']]));
1179+
}
1180+
11591181
/**
11601182
* Priority of accessor methods is defined by the PropertyReadInfoExtractorInterface passed to the PropertyAccessor
11611183
* component. By default ReflectionExtractor::$defaultAccessorPrefixes are used.
@@ -1718,3 +1740,49 @@ public function shouldDoThing()
17181740
return $this->shouldDoThing;
17191741
}
17201742
}
1743+
1744+
class ObjectWithIgnoredMethodSameNameAsProperty
1745+
{
1746+
public string $visible;
1747+
1748+
private $ignored;
1749+
1750+
public function __construct(string $ignored, string $visible)
1751+
{
1752+
$this->ignored = $ignored;
1753+
$this->visible = $visible;
1754+
}
1755+
1756+
#[Ignore]
1757+
public function ignored()
1758+
{
1759+
return $this->ignored;
1760+
}
1761+
}
1762+
1763+
class ObjectWithIgnoredMethodSameNameAsPropertyWithGroups
1764+
{
1765+
public string $visibleDefault;
1766+
public string $visibleGroup;
1767+
1768+
private $ignored;
1769+
1770+
public function __construct(string $ignored, string $visibleDefault, string $visibleGroup)
1771+
{
1772+
$this->ignored = $ignored;
1773+
$this->visibleDefault = $visibleDefault;
1774+
$this->visibleGroup = $visibleGroup;
1775+
}
1776+
1777+
#[Ignore]
1778+
public function ignored()
1779+
{
1780+
return $this->ignored;
1781+
}
1782+
1783+
#[Groups(['group1'])]
1784+
public function visibleGroup()
1785+
{
1786+
return $this->visibleGroup;
1787+
}
1788+
}

0 commit comments

Comments
 (0)