diff --git a/src/JMS/Serializer/Context.php b/src/JMS/Serializer/Context.php index 0d5c262eb..c03ecb9ec 100644 --- a/src/JMS/Serializer/Context.php +++ b/src/JMS/Serializer/Context.php @@ -47,6 +47,7 @@ abstract class Context public function __construct() { $this->attributes = new Map(); + $this->metadataStack = new \SplStack(); } /** diff --git a/src/JMS/Serializer/Exclusion/GroupsExclusionStrategy.php b/src/JMS/Serializer/Exclusion/GroupsExclusionStrategy.php index 9ee861eaf..42feef9d4 100644 --- a/src/JMS/Serializer/Exclusion/GroupsExclusionStrategy.php +++ b/src/JMS/Serializer/Exclusion/GroupsExclusionStrategy.php @@ -82,8 +82,16 @@ private function shouldSkipUsingGroups(PropertyMetadata $property, $groups) return true; } - private function getGroupsFor(Context $navigatorContext) + /** + * @param Context $navigatorContext + * @return array + */ + public function getGroupsFor(Context $navigatorContext) { + if (!$this->nestedGroups) { + return array_keys($this->groups); + } + $paths = $navigatorContext->getCurrentPath(); $groups = $this->groups; diff --git a/tests/Exclusion/GroupsExclusionStrategyTest.php b/tests/Exclusion/GroupsExclusionStrategyTest.php index 356e2f6b9..b6a331830 100644 --- a/tests/Exclusion/GroupsExclusionStrategyTest.php +++ b/tests/Exclusion/GroupsExclusionStrategyTest.php @@ -49,4 +49,40 @@ public function getExclusionRules() [['foo'], [GroupsExclusionStrategy::DEFAULT_GROUP, 'foo'], false], ]; } + + /** + * @dataProvider getGroupsFor + * @param $groups + * @param $propsVisited + * @param $resultingGroups + */ + public function testGroupsFor($groups, $propsVisited, $resultingGroups) + { + $exclusion = new GroupsExclusionStrategy($groups); + $context = SerializationContext::create(); + + foreach ($propsVisited as $prop) { + $metadata = new StaticPropertyMetadata('stdClass', $prop, 'propVal'); + $context->pushPropertyMetadata($metadata); + } + + $groupsFor = $exclusion->getGroupsFor($context); + $this->assertEquals($groupsFor, $resultingGroups); + } + + public function getGroupsFor() + { + return [ + [['foo'], ['prop'], ['foo']], + [[], ['prop'], ['Default']], + + [['foo', 'prop' => ['bar']], ['prop'], ['bar']], + [['foo', 'prop' => ['bar']], ['prop2'], ['foo', 'prop' => ['bar']]], + + [['foo', 'prop' => ['bar']], ['prop', 'prop2'], ['Default']], + + [['foo', 'prop' => ['xx', 'prop2' => ['def'], 'prop3' => ['def']]], ['prop', 'prop2', 'propB'], ['Default']], + [['foo', 'prop' => ['xx', 'prop2' => ['def', 'prop3' => ['def']]]], ['prop', 'prop2'], ['def', 'prop3' => ['def']]], + ]; + } }