Permalink
Browse files

feature #27503 [Serializer] Allow to pass a single value for the grou…

…ps 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 #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
  • Loading branch information...
nicolas-grekas committed Jul 1, 2018
2 parents 03de7f4 + 5b39203 commit 80aa8dfcfd2f6e791b43da85acc64d0990fb48ef
@@ -227,8 +227,8 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
}
$groups = false;
if (isset($context[static::GROUPS]) && \is_array($context[static::GROUPS])) {
$groups = $context[static::GROUPS];
if (isset($context[static::GROUPS]) && (\is_array($context[static::GROUPS]) || is_scalar($context[static::GROUPS]))) {
$groups = (array) $context[static::GROUPS];
} elseif (!isset($context[static::ALLOW_EXTRA_ATTRIBUTES]) || $context[static::ALLOW_EXTRA_ATTRIBUTES]) {
return false;
}
@@ -92,6 +92,9 @@ public function testGetAllowedAttributesAsObjects()
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('test')), false);
$this->assertEquals(array($a2, $a4), $result);
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => 'test'), false);
$this->assertEquals(array($a2, $a4), $result);
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('other')), false);
$this->assertEquals(array($a3, $a4), $result);
}

0 comments on commit 80aa8df

Please sign in to comment.