Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fe454e4

Browse filesBrowse files
committed
feature #20509 [Serializer] Allow to specify a single value in @groups (dunglas)
This PR was squashed before being merged into the 3.3-dev branch (closes #20509). Discussion ---------- [Serializer] Allow to specify a single value in @groups | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #19374 (comment) | License | MIT | Doc PR | todo Tiny DX improvement: Before: ```php use Symfony\Component\Serializer\Annotation\Groups; class Product { /** * @groups({"admins"}) */ public $itemsSold; } ``` Now allowed: ```php use Symfony\Component\Serializer\Annotation\Groups; class Product { /** * @groups("admins") */ public $itemsSold; } ``` Commits ------- 926aa48 [Serializer] Allow to specify a single value in @groups
2 parents 6a0ee38 + 926aa48 commit fe454e4
Copy full SHA for fe454e4

File tree

2 files changed

+13
-10
lines changed
Filter options

2 files changed

+13
-10
lines changed

‎src/Symfony/Component/Serializer/Annotation/Groups.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Annotation/Groups.php
+6-9Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class Groups
2525
{
2626
/**
27-
* @var array
27+
* @var string[]
2828
*/
2929
private $groups;
3030

@@ -39,23 +39,20 @@ public function __construct(array $data)
3939
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', get_class($this)));
4040
}
4141

42-
if (!is_array($data['value'])) {
43-
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be an array of strings.', get_class($this)));
44-
}
45-
46-
foreach ($data['value'] as $group) {
42+
$value = (array) $data['value'];
43+
foreach ($value as $group) {
4744
if (!is_string($group)) {
48-
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be an array of strings.', get_class($this)));
45+
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a string or an array of strings.', get_class($this)));
4946
}
5047
}
5148

52-
$this->groups = $data['value'];
49+
$this->groups = $value;
5350
}
5451

5552
/**
5653
* Gets groups.
5754
*
58-
* @return array
55+
* @return string[]
5956
*/
6057
public function getGroups()
6158
{

‎src/Symfony/Component/Serializer/Tests/Annotation/GroupsTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Annotation/GroupsTest.php
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testEmptyGroupsParameter()
3131
*/
3232
public function testNotAnArrayGroupsParameter()
3333
{
34-
new Groups(array('value' => 'coopTilleuls'));
34+
new Groups(array('value' => 12));
3535
}
3636

3737
/**
@@ -49,4 +49,10 @@ public function testGroupsParameters()
4949
$groups = new Groups(array('value' => $validData));
5050
$this->assertEquals($validData, $groups->getGroups());
5151
}
52+
53+
public function testSingleGroup()
54+
{
55+
$groups = new Groups(array('value' => 'a'));
56+
$this->assertEquals(array('a'), $groups->getGroups());
57+
}
5258
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.