Skip to content

Commit 497c668

Browse files
authored
Merge pull request #1419 from soyuka/fix/object_to_populate
Replace context keys by constants
2 parents 32f65b7 + 7fc53b9 commit 497c668

File tree

17 files changed

+82
-67
lines changed

17 files changed

+82
-67
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/EventListener/DeserializeListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\HttpFoundation\Request;
1919
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
2020
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
21+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
2122
use Symfony\Component\Serializer\SerializerInterface;
2223

2324
/**
@@ -61,7 +62,7 @@ public function onKernelRequest(GetResponseEvent $event)
6162

6263
$data = $request->attributes->get('data');
6364
if (null !== $data) {
64-
$context['object_to_populate'] = $data;
65+
$context[AbstractNormalizer::OBJECT_TO_POPULATE] = $data;
6566
}
6667

6768
$request->attributes->set(

src/Hydra/Serializer/DocumentationNormalizer.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
2828
use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface;
2929
use Symfony\Component\PropertyInfo\Type;
30+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
3031
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
3132

3233
/**
@@ -157,17 +158,17 @@ private function getPropertyNameCollectionFactoryContext(ResourceMetadata $resou
157158
$attributes = $resourceMetadata->getAttributes();
158159
$context = [];
159160

160-
if (isset($attributes['normalization_context']['groups'])) {
161-
$context['serializer_groups'] = $attributes['normalization_context']['groups'];
161+
if (isset($attributes['normalization_context'][AbstractNormalizer::GROUPS])) {
162+
$context['serializer_groups'] = $attributes['normalization_context'][AbstractNormalizer::GROUPS];
162163
}
163164

164-
if (isset($attributes['denormalization_context']['groups'])) {
165+
if (isset($attributes['denormalization_context'][AbstractNormalizer::GROUPS])) {
165166
if (isset($context['serializer_groups'])) {
166-
foreach ($attributes['denormalization_context']['groups'] as $groupName) {
167+
foreach ($attributes['denormalization_context'][AbstractNormalizer::GROUPS] as $groupName) {
167168
$context['serializer_groups'][] = $groupName;
168169
}
169170
} else {
170-
$context['serializer_groups'] = $attributes['denormalization_context']['groups'];
171+
$context['serializer_groups'] = $attributes['denormalization_context'][AbstractNormalizer::GROUPS];
171172
}
172173
}
173174

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ public function supportsDenormalization($data, $type, $format = null)
9797
public function denormalize($data, $class, $format = null, array $context = [])
9898
{
9999
// Avoid issues with proxies if we populated the object
100-
if (isset($data['@id']) && !isset($context['object_to_populate'])) {
100+
if (isset($data['@id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
101101
if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
102102
throw new InvalidArgumentException('Update is not allowed for this operation.');
103103
}
104104

105-
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['@id'], $context + ['fetch_data' => true]);
105+
$context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getItemFromIri($data['@id'], $context + ['fetch_data' => true]);
106106
}
107107

108108
return parent::denormalize($data, $class, $format, $context);

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/Serializer/ItemNormalizer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class ItemNormalizer extends AbstractItemNormalizer
3030
public function denormalize($data, $class, $format = null, array $context = [])
3131
{
3232
// Avoid issues with proxies if we populated the object
33-
if (isset($data['id']) && !isset($context['object_to_populate'])) {
33+
if (isset($data['id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
3434
if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
3535
throw new InvalidArgumentException('Update is not allowed for this operation.');
3636
}
@@ -44,7 +44,7 @@ public function denormalize($data, $class, $format = null, array $context = [])
4444
private function updateObjectToPopulate(array $data, array &$context)
4545
{
4646
try {
47-
$context['object_to_populate'] = $this->iriConverter->getItemFromIri((string) $data['id'], $context + ['fetch_data' => true]);
47+
$context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getItemFromIri((string) $data['id'], $context + ['fetch_data' => true]);
4848
} catch (InvalidArgumentException $e) {
4949
$identifier = null;
5050
foreach ($this->propertyNameCollectionFactory->create($context['resource_class'], $context) as $propertyName) {
@@ -58,7 +58,7 @@ private function updateObjectToPopulate(array $data, array &$context)
5858
throw $e;
5959
}
6060

61-
$context['object_to_populate'] = $this->iriConverter->getItemFromIri(sprintf('%s/%s', $this->iriConverter->getIriFromResourceClass($context['resource_class']), $data[$identifier]), $context + ['fetch_data' => true]);
61+
$context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getItemFromIri(sprintf('%s/%s', $this->iriConverter->getIriFromResourceClass($context['resource_class']), $data[$identifier]), $context + ['fetch_data' => true]);
6262
}
6363
}
6464
}

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();

0 commit comments

Comments
 (0)