@@ -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