Skip to content

Commit 822c78c

Browse files
authored
Merge pull request #2348 from dunglas/string-groups
Allow strings for single serialization groups
2 parents d784897 + b9ae77f commit 822c78c

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/Metadata/Property/Factory/SerializerPropertyMetadataFactory.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,14 @@ private function transformLinkStatus(PropertyMetadata $propertyMetadata, array $
138138
* - From metadata of the given operation ("collection_operation_name" and "item_operation_name" keys).
139139
* - From metadata of the current resource.
140140
*
141-
*
142141
* @return (string[]|null)[]
143142
*/
144143
private function getEffectiveSerializerGroups(array $options, string $resourceClass): array
145144
{
146145
if (isset($options['serializer_groups'])) {
147-
return [$options['serializer_groups'], $options['serializer_groups']];
146+
$groups = (array) $options['serializer_groups'];
147+
148+
return [$groups, $groups];
148149
}
149150

150151
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
@@ -162,13 +163,15 @@ private function getEffectiveSerializerGroups(array $options, string $resourceCl
162163
$denormalizationContext = $resourceMetadata->getAttribute('denormalization_context');
163164
}
164165

165-
return [$normalizationContext['groups'] ?? null, $denormalizationContext['groups'] ?? null];
166+
return [
167+
isset($normalizationContext['groups']) ? (array) $normalizationContext['groups'] : null,
168+
isset($denormalizationContext['groups']) ? (array) $denormalizationContext['groups'] : null,
169+
];
166170
}
167171

168172
/**
169173
* Gets the serializer groups defined on a property.
170174
*
171-
*
172175
* @return string[]
173176
*/
174177
private function getPropertySerializerGroups(string $resourceClass, string $property): array
@@ -187,19 +190,17 @@ private function getPropertySerializerGroups(string $resourceClass, string $prop
187190
/**
188191
* Gets the serializer groups defined in a resource.
189192
*
190-
*
191193
* @return string[]
192194
*/
193195
private function getResourceSerializerGroups(string $resourceClass): array
194196
{
195197
$serializerClassMetadata = $this->serializerClassMetadataFactory->getMetadataFor($resourceClass);
196198

197199
$groups = [];
198-
199200
foreach ($serializerClassMetadata->getAttributesMetadata() as $serializerAttributeMetadata) {
200-
$groups = array_merge($groups, $serializerAttributeMetadata->getGroups());
201+
$groups += array_flip($serializerAttributeMetadata->getGroups());
201202
}
202203

203-
return array_unique($groups);
204+
return array_keys($groups);
204205
}
205206
}

tests/Metadata/Property/Factory/SerializerPropertyMetadataFactoryTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,19 @@ public function testConstruct()
5050
$this->assertInstanceOf(PropertyMetadataFactoryInterface::class, $serializerPropertyMetadataFactory);
5151
}
5252

53-
public function testCreate()
53+
/**
54+
* @dataProvider groupsProvider
55+
*/
56+
public function testCreate($readGroups, $writeGroups)
5457
{
5558
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
5659
$dummyResourceMetadata = (new ResourceMetadata())
5760
->withAttributes([
5861
'normalization_context' => [
59-
AbstractNormalizer::GROUPS => ['dummy_read'],
62+
AbstractNormalizer::GROUPS => $readGroups,
6063
],
6164
'denormalization_context' => [
62-
AbstractNormalizer::GROUPS => ['dummy_write'],
65+
AbstractNormalizer::GROUPS => $writeGroups,
6366
],
6467
]);
6568
$resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn($dummyResourceMetadata)->shouldBeCalled();
@@ -125,6 +128,14 @@ public function testCreate()
125128
$this->assertFalse($actual[2]->isWritable());
126129
}
127130

131+
public function groupsProvider(): array
132+
{
133+
return [
134+
[['dummy_read'], ['dummy_write']],
135+
['dummy_read', 'dummy_write'],
136+
];
137+
}
138+
128139
public function testCreateInherited()
129140
{
130141
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);

0 commit comments

Comments
 (0)