Skip to content
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

feat(exclusion): add new option "inheritedGroups" into GroupExclusionStrategy #1474

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
49 changes: 47 additions & 2 deletions doc/cookbook/exclusion_strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,51 @@ This would result in the following json::
]
}

You can, also take inherited groups::

use JMS\Serializer\SerializationContext;

$context = SerializationContext::create()->setGroups(array(
'Default', // Serialize John's name
'manager_group', // Serialize John's manager
'friends_group', // Serialize John's friends

'manager' => array( // Override the groups for the manager of John
'Default', // Serialize John manager's name
'friends_group', // Serialize John manager's friends. If you do not override the groups for the friends, it will default to Default.
),
));
$context->enableInheritGroups();
$serializer->serialize($john, 'json', $context);

This would result in the following json::

{
"name": "John",
"manager": {
"name": "John Manager",
"friends": [
{
"name": "John Manager friend 1"
}
]
},
"friends": [
{
"name": "John friend 1",
"manager": {
"name": "John friend 1 manager"
},
},
{
"name": "John friend 2",
"manager": {
"name": "John friend 2 manager"
},
},
]
}

Deserialization Exclusion Strategy with Groups
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can use ``@Groups`` to cut off unwanted properties while deserialization.
Expand Down Expand Up @@ -345,15 +390,15 @@ This also works on class level, but is only evaluated during ``serialze`` and do

``true`` is just a generic expression, you can use any expression allowed by the Symfony Expression Language

To enable this feature you have to set the Expression Evaluator when initializing the serializer.
To enable this feature you have to set the Expression Evaluator when initializing the serializer.

.. code-block :: php

<?php
use JMS\Serializer\Expression\ExpressionEvaluator;
use JMS\Serializer\Expression\SerializerBuilder;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

$serializer = SerializerBuilder::create()
->setExpressionEvaluator(new ExpressionEvaluator(new ExpressionLanguage()))
->build();
Expand Down
18 changes: 17 additions & 1 deletion src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ public function initialize(string $format, VisitorInterface $visitor, GraphNavig
$this->metadataStack = new \SplStack();

if (isset($this->attributes['groups'])) {
$this->addExclusionStrategy(new GroupsExclusionStrategy($this->attributes['groups']));
$strategy = new GroupsExclusionStrategy($this->attributes['groups']);
Copy link
Collaborator

@goetas goetas Feb 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not

$context = // ... get the context somehow
$g = new GroupsExclusionStrategy(['a', 'b'], true);
$context->addExclusionStrategy($g);

?

In that way we do not need to add the enableInheritGroups method in the context. Ideally the context should know the less possible about the various exclusion strategies.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, i see, but i can't add 2 GroupsExclusionStrategy, one with inheritedGroups = false and an another one with inheritedGroups = true.

How can i configure this option with JMSerializerBundle for example ?

Thanks for you help


if (isset($this->attributes['inherited_groups'])) {
$strategy->setInheritedGroups($this->attributes['inherited_groups']);
}

$this->addExclusionStrategy($strategy);
}

if (isset($this->attributes['version'])) {
Expand Down Expand Up @@ -204,6 +210,16 @@ public function enableMaxDepthChecks(): self
return $this;
}

/**
* @return $this
*/
public function enableInheritGroups(): self
{
$this->attributes['inherited_groups'] = true;

return $this;
}

public function getFormat(): string
{
return $this->format;
Expand Down
11 changes: 10 additions & 1 deletion src/Exclusion/GroupsExclusionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ final class GroupsExclusionStrategy implements ExclusionStrategyInterface
*/
private $nestedGroups = false;

private $inheritedGroups = false;

public function __construct(array $groups)
{
if (empty($groups)) {
Expand All @@ -44,6 +46,13 @@ public function __construct(array $groups)
}
}

public function setInheritedGroups(bool $inheritedGroups): self
{
$this->inheritedGroups = $inheritedGroups;

return $this;
}

public function shouldSkipClass(ClassMetadata $metadata, Context $navigatorContext): bool
{
return false;
Expand Down Expand Up @@ -96,7 +105,7 @@ public function getGroupsFor(Context $navigatorContext): array
foreach ($paths as $index => $path) {
if (!array_key_exists($path, $groups)) {
if ($index > 0) {
$groups = [self::DEFAULT_GROUP];
$groups = $this->inheritedGroups ? $groups : [self::DEFAULT_GROUP];
} else {
$groups = array_filter($groups, 'is_string') ?: [self::DEFAULT_GROUP];
}
Expand Down
27 changes: 17 additions & 10 deletions tests/Exclusion/GroupsExclusionStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getExclusionRules()
/**
* @dataProvider getGroupsFor
*/
public function testGroupsFor(array $groups, array $propsVisited, array $resultingGroups)
public function testGroupsFor(array $groups, array $propsVisited, bool $inheritGroups, array $resultingGroups)
{
$exclusion = new GroupsExclusionStrategy($groups);
$context = SerializationContext::create();
Expand All @@ -67,27 +67,34 @@ public function testGroupsFor(array $groups, array $propsVisited, array $resulti
$context->pushPropertyMetadata($metadata);
}

$exclusion->setInheritedGroups($inheritGroups);
$groupsFor = $exclusion->getGroupsFor($context);
self::assertEquals($groupsFor, $resultingGroups);
}

public function getGroupsFor()
{
return [
[['foo'], ['prop'], ['foo']],
[[], ['prop'], ['Default']],
[['foo'], ['prop'], false, ['foo']],
[[], ['prop'], false, ['Default']],

[['foo', 'prop' => ['bar']], ['prop'], ['bar']],
[['foo', 'prop' => ['bar']], ['prop2'], ['foo']],
[['foo', 'prop' => ['bar']], ['prop'], false, ['bar']],
[['foo', 'prop' => ['bar']], ['prop2'], false, ['foo']],

[['prop' => ['bar']],['prop2'],['Default']],
[['prop' => ['bar']],['prop2'], false, ['Default']],

[['foo', 'prop' => ['bar']], ['prop', 'prop2'], ['Default']],
[['foo', 'prop' => ['bar']], ['prop', 'prop2'], false, ['Default']],

[['foo', 'prop' => ['xx', 'prop2' => ['def'], 'prop3' => ['def']]], ['prop', 'prop2', 'propB'], ['Default']],
[['foo', 'prop' => ['xx', 'prop2' => ['def', 'prop3' => ['def']]]], ['prop', 'prop2'], ['def', 'prop3' => ['def']]],
[['foo', 'prop' => ['xx', 'prop2' => ['def'], 'prop3' => ['def']]], ['prop', 'prop2', 'propB'], false, ['Default']],
[['foo', 'prop' => ['xx', 'prop2' => ['def', 'prop3' => ['def']]]], ['prop', 'prop2'], false, ['def', 'prop3' => ['def']]],

[['foo', 'prop' => ['prop2' => ['prop3' => ['def']]]], ['prop', 'prop2'], ['Default', 'prop3' => ['def']]],
[['foo', 'prop' => ['prop2' => ['prop3' => ['def']]]], ['prop', 'prop2'], false, ['Default', 'prop3' => ['def']]],

[['foo'], ['prop'], true, ['foo']],
[[], ['prop'], true, ['Default']],
[['foo', 'prop' => ['xx', 'prop2' => ['def'], 'prop3' => ['def']]], ['prop', 'prop2', 'propB'], true, ['def']],
[['foo', 'prop' => ['xx', 'prop2' => ['def', 'prop3' => ['def']]]], ['prop', 'prop2'], true, ['def', 'prop3' => ['def']]],
[['foo', 'prop' => ['prop2' => ['prop3' => ['def']]]], ['prop', 'prop2'], true, ['Default', 'prop3' => ['def']]],
];
}
}