Skip to content

Commit 80aa8df

Browse files
feature symfony#27503 [Serializer] Allow to pass a single value for the groups opt (dunglas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Serializer] Allow to pass a single value for the groups opt | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | n/a | License | MIT | Doc PR | todo Shortcut syntax similar to symfony#20509 but for the context: allows to pass a string instead of an array of string in the serialization context when only one group if used. Before: ```php $serializer->serialize($foo, 'json', ['groups' => ['myGroup']]; /** @ApiResource(normalizationContext={"groups"={"myGroup"}}) */ ``` After: ```php $serializer->serialize($foo, 'json', ['groups' => 'myGroup']; /** @ApiResource(normalizationContext={"groups"="myGroup") */ ``` Commits ------- 5b39203 [Serializer] Allow to pass a single value for the groups opt
2 parents 03de7f4 + 5b39203 commit 80aa8df

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
227227
}
228228

229229
$groups = false;
230-
if (isset($context[static::GROUPS]) && \is_array($context[static::GROUPS])) {
231-
$groups = $context[static::GROUPS];
230+
if (isset($context[static::GROUPS]) && (\is_array($context[static::GROUPS]) || is_scalar($context[static::GROUPS]))) {
231+
$groups = (array) $context[static::GROUPS];
232232
} elseif (!isset($context[static::ALLOW_EXTRA_ATTRIBUTES]) || $context[static::ALLOW_EXTRA_ATTRIBUTES]) {
233233
return false;
234234
}

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public function testGetAllowedAttributesAsObjects()
9292
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('test')), false);
9393
$this->assertEquals(array($a2, $a4), $result);
9494

95+
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => 'test'), false);
96+
$this->assertEquals(array($a2, $a4), $result);
97+
9598
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('other')), false);
9699
$this->assertEquals(array($a3, $a4), $result);
97100
}

0 commit comments

Comments
 (0)