Skip to content

Commit 7fc53b9

Browse files
soyukaabluchet
authored andcommitted
Use serializer AbstractItemNormalizer constants when related
1 parent ffb3d0f commit 7fc53b9

File tree

14 files changed

+75
-61
lines changed

14 files changed

+75
-61
lines changed

src/Bridge/Doctrine/Orm/Extension/EagerLoadingExtension.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
use Doctrine\ORM\QueryBuilder;
2727
use Symfony\Component\HttpFoundation\RequestStack;
2828
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
29+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
30+
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
2931

3032
/**
3133
* Eager loads relations.
@@ -99,8 +101,8 @@ public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterf
99101
$contextType = isset($context['api_denormalize']) ? 'denormalization_context' : 'normalization_context';
100102
$serializerContext = $this->getSerializerContext($context['resource_class'] ?? $resourceClass, $contextType, $options);
101103

102-
if (isset($context['groups'])) {
103-
$groups = ['serializer_groups' => $context['groups']];
104+
if (isset($context[AbstractNormalizer::GROUPS])) {
105+
$groups = ['serializer_groups' => $context[AbstractNormalizer::GROUPS]];
104106
} else {
105107
$groups = $this->getSerializerGroups($options, $serializerContext);
106108
}
@@ -137,7 +139,7 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt
137139

138140
foreach ($classMetadata->associationMappings as $association => $mapping) {
139141
//Don't join if max depth is enabled and the current depth limit is reached
140-
if (isset($context['enable_max_depth']) && 0 === $currentDepth) {
142+
if (isset($context[AbstractObjectNormalizer::ENABLE_MAX_DEPTH]) && 0 === $currentDepth) {
141143
continue;
142144
}
143145

@@ -280,10 +282,10 @@ private function getSerializerContext(string $resourceClass, string $contextType
280282
*/
281283
private function getSerializerGroups(array $options, array $context): array
282284
{
283-
if (empty($context['groups'])) {
285+
if (empty($context[AbstractNormalizer::GROUPS])) {
284286
return $options;
285287
}
286288

287-
return ['serializer_groups' => $context['groups']];
289+
return ['serializer_groups' => $context[AbstractNormalizer::GROUPS]];
288290
}
289291
}

src/Bridge/NelmioApiDoc/Parser/ApiPlatformParser.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Nelmio\ApiDocBundle\Parser\ParserInterface;
2424
use Symfony\Component\PropertyInfo\Type;
2525
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
26+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
2627

2728
/**
2829
* Extract input and output information for the NelmioApiDocBundle.
@@ -113,15 +114,15 @@ private function parseResource(ResourceMetadata $resourceMetadata, string $resou
113114
$options = [];
114115
$attributes = $resourceMetadata->getAttributes();
115116

116-
if (isset($attributes['normalization_context']['groups'])) {
117-
$options['serializer_groups'] = $attributes['normalization_context']['groups'];
117+
if (isset($attributes['normalization_context'][AbstractNormalizer::GROUPS])) {
118+
$options['serializer_groups'] = $attributes['normalization_context'][AbstractNormalizer::GROUPS];
118119
}
119120

120-
if (isset($attributes['denormalization_context']['groups'])) {
121+
if (isset($attributes['denormalization_context'][AbstractNormalizer::GROUPS])) {
121122
if (isset($options['serializer_groups'])) {
122-
$options['serializer_groups'] += $attributes['denormalization_context']['groups'];
123+
$options['serializer_groups'] += $attributes['denormalization_context'][AbstractNormalizer::GROUPS];
123124
} else {
124-
$options['serializer_groups'] = $attributes['denormalization_context']['groups'];
125+
$options['serializer_groups'] = $attributes['denormalization_context'][AbstractNormalizer::GROUPS];
125126
}
126127
}
127128

@@ -131,12 +132,12 @@ private function parseResource(ResourceMetadata $resourceMetadata, string $resou
131132
private function getGroupsContext(ResourceMetadata $resourceMetadata, string $operationName, bool $isNormalization)
132133
{
133134
$groupsContext = $isNormalization ? 'normalization_context' : 'denormalization_context';
134-
$itemOperationAttribute = $resourceMetadata->getItemOperationAttribute($operationName, $groupsContext, ['groups' => []], true)['groups'];
135-
$collectionOperationAttribute = $resourceMetadata->getCollectionOperationAttribute($operationName, $groupsContext, ['groups' => []], true)['groups'];
135+
$itemOperationAttribute = $resourceMetadata->getItemOperationAttribute($operationName, $groupsContext, [AbstractNormalizer::GROUPS => []], true)[AbstractNormalizer::GROUPS];
136+
$collectionOperationAttribute = $resourceMetadata->getCollectionOperationAttribute($operationName, $groupsContext, [AbstractNormalizer::GROUPS => []], true)[AbstractNormalizer::GROUPS];
136137

137138
return [
138139
$groupsContext => [
139-
'groups' => array_merge($itemOperationAttribute ?? [], $collectionOperationAttribute ?? []),
140+
AbstractNormalizer::GROUPS => array_merge($itemOperationAttribute ?? [], $collectionOperationAttribute ?? []),
140141
],
141142
];
142143
}
@@ -157,13 +158,13 @@ private function getGroupsForItemAndCollectionOperation(ResourceMetadata $resour
157158

158159
if (self::OUT_PREFIX === $io) {
159160
return [
160-
'serializer_groups' => !empty($operation['normalization_context']) ? $operation['normalization_context']['groups'] : [],
161+
'serializer_groups' => !empty($operation['normalization_context']) ? $operation['normalization_context'][AbstractNormalizer::GROUPS] : [],
161162
];
162163
}
163164

164165
if (self::IN_PREFIX === $io) {
165166
return [
166-
'serializer_groups' => !empty($operation['denormalization_context']) ? $operation['denormalization_context']['groups'] : [],
167+
'serializer_groups' => !empty($operation['denormalization_context']) ? $operation['denormalization_context'][AbstractNormalizer::GROUPS] : [],
167168
];
168169
}
169170

src/Hydra/Serializer/DocumentationNormalizer.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2727
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
2828
use Symfony\Component\PropertyInfo\Type;
29+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
2930
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
3031

3132
/**
@@ -154,17 +155,17 @@ private function getPropertyNameCollectionFactoryContext(ResourceMetadata $resou
154155
$attributes = $resourceMetadata->getAttributes();
155156
$context = [];
156157

157-
if (isset($attributes['normalization_context']['groups'])) {
158-
$context['serializer_groups'] = $attributes['normalization_context']['groups'];
158+
if (isset($attributes['normalization_context'][AbstractNormalizer::GROUPS])) {
159+
$context['serializer_groups'] = $attributes['normalization_context'][AbstractNormalizer::GROUPS];
159160
}
160161

161-
if (isset($attributes['denormalization_context']['groups'])) {
162+
if (isset($attributes['denormalization_context'][AbstractNormalizer::GROUPS])) {
162163
if (isset($context['serializer_groups'])) {
163-
foreach ($attributes['denormalization_context']['groups'] as $groupName) {
164+
foreach ($attributes['denormalization_context'][AbstractNormalizer::GROUPS] as $groupName) {
164165
$context['serializer_groups'][] = $groupName;
165166
}
166167
} else {
167-
$context['serializer_groups'] = $attributes['denormalization_context']['groups'];
168+
$context['serializer_groups'] = $attributes['denormalization_context'][AbstractNormalizer::GROUPS];
168169
}
169170
}
170171

src/Serializer/AbstractItemNormalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ protected function getFactoryOptions(array $context): array
336336
{
337337
$options = [];
338338

339-
if (isset($context['groups'])) {
340-
$options['serializer_groups'] = $context['groups'];
339+
if (isset($context[self::GROUPS])) {
340+
$options['serializer_groups'] = $context[self::GROUPS];
341341
}
342342

343343
if (isset($context['collection_operation_name'])) {

src/Serializer/Filter/GroupFilter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Core\Serializer\Filter;
1515

1616
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
1718

1819
/**
1920
* Group filter.
@@ -44,11 +45,11 @@ public function apply(Request $request, bool $normalization, array $attributes,
4445
if (null !== $this->whitelist) {
4546
$groups = array_intersect($this->whitelist, $groups);
4647
}
47-
if (!$this->overrideDefaultGroups && isset($context['groups'])) {
48-
$groups = array_merge((array) $context['groups'], $groups);
48+
if (!$this->overrideDefaultGroups && isset($context[AbstractNormalizer::GROUPS])) {
49+
$groups = array_merge((array) $context[AbstractNormalizer::GROUPS], $groups);
4950
}
5051

51-
$context['groups'] = $groups;
52+
$context[AbstractNormalizer::GROUPS] = $groups;
5253
}
5354

5455
/**

src/Serializer/Filter/PropertyFilter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Core\Serializer\Filter;
1515

1616
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
1718

1819
/**
1920
* Property filter.
@@ -46,11 +47,11 @@ public function apply(Request $request, bool $normalization, array $attributes,
4647
$properties = $this->getProperties($properties, $this->whitelist);
4748
}
4849

49-
if (!$this->overrideDefaultProperties && isset($context['attributes'])) {
50-
$properties = array_merge_recursive((array) $context['attributes'], $properties);
50+
if (!$this->overrideDefaultProperties && isset($context[AbstractNormalizer::ATTRIBUTES])) {
51+
$properties = array_merge_recursive((array) $context[AbstractNormalizer::ATTRIBUTES], $properties);
5152
}
5253

53-
$context['attributes'] = $properties;
54+
$context[AbstractNormalizer::ATTRIBUTES] = $properties;
5455
}
5556

5657
/**

src/Swagger/Serializer/DocumentationNormalizer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Psr\Container\ContainerInterface;
3131
use Symfony\Component\PropertyInfo\Type;
3232
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
33+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
3334
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
3435

3536
/**
@@ -441,7 +442,7 @@ private function updateDeleteOperation(\ArrayObject $pathOperation, string $reso
441442
*/
442443
private function getDefinition(\ArrayObject $definitions, ResourceMetadata $resourceMetadata, string $resourceClass, array $serializerContext = null): string
443444
{
444-
$definitionKey = $this->getDefinitionKey($resourceMetadata->getShortName(), (array) ($serializerContext['groups'] ?? []));
445+
$definitionKey = $this->getDefinitionKey($resourceMetadata->getShortName(), (array) ($serializerContext[AbstractNormalizer::GROUPS] ?? []));
445446

446447
if (!isset($definitions[$definitionKey])) {
447448
$definitions[$definitionKey] = []; // Initialize first to prevent infinite loop
@@ -480,7 +481,7 @@ private function getDefinitionSchema(string $resourceClass, ResourceMetadata $re
480481
$definitionSchema['externalDocs'] = ['url' => $iri];
481482
}
482483

483-
$options = isset($serializerContext['groups']) ? ['serializer_groups' => $serializerContext['groups']] : [];
484+
$options = isset($serializerContext[AbstractNormalizer::GROUPS]) ? ['serializer_groups' => $serializerContext[AbstractNormalizer::GROUPS]] : [];
484485
foreach ($this->propertyNameCollectionFactory->create($resourceClass, $options) as $propertyName) {
485486
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
486487
$normalizedPropertyName = $this->nameConverter ? $this->nameConverter->normalize($propertyName) : $propertyName;

tests/Bridge/Doctrine/Orm/Extension/EagerLoadingExtensionTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
4141
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
4242
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
43+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
4344

4445
/**
4546
* @author Amrouche Hamza <[email protected]>
@@ -315,7 +316,7 @@ public function testDenormalizeItemWithExistingGroups()
315316
$queryBuilderProphecy->getEntityManager()->willReturn($emProphecy);
316317

317318
$eagerExtensionTest = new EagerLoadingExtension($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), 30, false, null, null, true);
318-
$eagerExtensionTest->applyToItem($queryBuilderProphecy->reveal(), new QueryNameGenerator(), RelatedDummy::class, ['id' => 1], 'item_operation', ['groups' => 'some_groups']);
319+
$eagerExtensionTest->applyToItem($queryBuilderProphecy->reveal(), new QueryNameGenerator(), RelatedDummy::class, ['id' => 1], 'item_operation', [AbstractNormalizer::GROUPS => 'some_groups']);
319320
}
320321

321322
/**
@@ -442,7 +443,7 @@ public function testMaxDepth()
442443
public function testForceEager()
443444
{
444445
$resourceMetadata = new ResourceMetadata();
445-
$resourceMetadata = $resourceMetadata->withAttributes(['normalization_context' => ['groups' => 'foobar']]);
446+
$resourceMetadata = $resourceMetadata->withAttributes(['normalization_context' => [AbstractNormalizer::GROUPS => 'foobar']]);
446447
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
447448
$resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn($resourceMetadata);
448449

@@ -617,7 +618,7 @@ public function testApplyToCollectionWithSerializerContextBuilder()
617618
$requestStack->push($request);
618619

619620
$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
620-
$serializerContextBuilderProphecy->createFromRequest($request, true)->shouldBeCalled()->willReturn(['groups' => ['foo']]);
621+
$serializerContextBuilderProphecy->createFromRequest($request, true)->shouldBeCalled()->willReturn([AbstractNormalizer::GROUPS => ['foo']]);
621622

622623
$queryBuilder = $queryBuilderProphecy->reveal();
623624
$eagerExtensionTest = new EagerLoadingExtension($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), 30, false, $requestStack, $serializerContextBuilderProphecy->reveal(), true);

tests/Bridge/NelmioApiDoc/Parser/ApiPlatformParserTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Prophecy\Argument;
3131
use Symfony\Component\PropertyInfo\Type;
3232
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
33+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
3334

3435
/**
3536
* @author Teoh Han Hui <[email protected]>
@@ -94,8 +95,8 @@ public function testSupportsAttributeNormalization()
9495
{
9596
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
9697
$resourceMetadataFactoryProphecy->create('Acme\CustomAttributeDummy')->willReturn(new ResourceMetadata('dummy', 'dummy', null, [
97-
'get' => ['method' => 'GET', 'normalization_context' => ['groups' => ['custom_attr_dummy_get']]],
98-
'put' => ['method' => 'PUT', 'denormalization_context' => ['groups' => ['custom_attr_dummy_put']]],
98+
'get' => ['method' => 'GET', 'normalization_context' => [AbstractNormalizer::GROUPS => ['custom_attr_dummy_get']]],
99+
'put' => ['method' => 'PUT', 'denormalization_context' => [AbstractNormalizer::GROUPS => ['custom_attr_dummy_put']]],
99100
'delete' => ['method' => 'DELETE'],
100101
], []))->shouldBeCalled();
101102
$resourceMetadataFactory = $resourceMetadataFactoryProphecy->reveal();
@@ -189,9 +190,9 @@ public function testParse()
189190
{
190191
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
191192
$resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata('dummy', 'dummy', null, [
192-
'get' => ['method' => 'GET', 'normalization_context' => ['groups' => ['custom_attr_dummy_get']]],
193-
'put' => ['method' => 'PUT', 'denormalization_context' => ['groups' => ['custom_attr_dummy_put']]],
194-
'gerard' => ['method' => 'get', 'path' => '/gerard', 'denormalization_context' => ['groups' => ['custom_attr_dummy_put']]],
193+
'get' => ['method' => 'GET', 'normalization_context' => [AbstractNormalizer::GROUPS => ['custom_attr_dummy_get']]],
194+
'put' => ['method' => 'PUT', 'denormalization_context' => [AbstractNormalizer::GROUPS => ['custom_attr_dummy_put']]],
195+
'gerard' => ['method' => 'get', 'path' => '/gerard', 'denormalization_context' => [AbstractNormalizer::GROUPS => ['custom_attr_dummy_put']]],
195196
'delete' => ['method' => 'DELETE'],
196197
], []))->shouldBeCalled();
197198
$resourceMetadataFactory = $resourceMetadataFactoryProphecy->reveal();

tests/EventListener/DeserializeListenerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Prophecy\Argument;
2020
use Symfony\Component\HttpFoundation\Request;
2121
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
22+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
2223
use Symfony\Component\Serializer\SerializerInterface;
2324

2425
/**
@@ -115,7 +116,7 @@ public function testDeserialize(string $method, bool $populateObject)
115116
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
116117

117118
$serializerProphecy = $this->prophesize(SerializerInterface::class);
118-
$context = $populateObject ? ['object_to_populate' => $populateObject] : [];
119+
$context = $populateObject ? [AbstractNormalizer::OBJECT_TO_POPULATE => $populateObject] : [];
119120
$serializerProphecy->deserialize('{}', 'Foo', 'json', $context)->willReturn($result)->shouldBeCalled();
120121

121122
$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);

tests/Metadata/Property/Factory/SerializerPropertyMetadataFactoryTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\Serializer\Mapping\AttributeMetadata as SerializerAttributeMetadata;
2828
use Symfony\Component\Serializer\Mapping\ClassMetadata as SerializerClassMetadata;
2929
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface as SerializerClassMetadataFactoryInterface;
30+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
3031

3132
/**
3233
* @author Teoh Han Hui <[email protected]>
@@ -55,10 +56,10 @@ public function testCreate()
5556
$dummyResourceMetadata = (new ResourceMetadata())
5657
->withAttributes([
5758
'normalization_context' => [
58-
'groups' => ['dummy_read'],
59+
AbstractNormalizer::GROUPS => ['dummy_read'],
5960
],
6061
'denormalization_context' => [
61-
'groups' => ['dummy_write'],
62+
AbstractNormalizer::GROUPS => ['dummy_write'],
6263
],
6364
]);
6465
$resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn($dummyResourceMetadata)->shouldBeCalled();

tests/Metadata/Resource/Factory/FileConfigurationMetadataFactoryProvider.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
1717
use PHPUnit\Framework\TestCase;
18+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
1819

1920
/**
2021
* Resource metadata provider for file configured factories tests.
@@ -40,10 +41,10 @@ public function resourceMetadataProvider()
4041
'iri' => 'someirischema',
4142
'attributes' => [
4243
'normalization_context' => [
43-
'groups' => ['default'],
44+
AbstractNormalizer::GROUPS => ['default'],
4445
],
4546
'denormalization_context' => [
46-
'groups' => ['default'],
47+
AbstractNormalizer::GROUPS => ['default'],
4748
],
4849
'hydra_context' => [
4950
'@type' => 'hydra:Operation',

0 commit comments

Comments
 (0)