Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Bridge/Doctrine/Orm/Extension/EagerLoadingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

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

if (isset($context['groups'])) {
$groups = ['serializer_groups' => $context['groups']];
if (isset($context[AbstractNormalizer::GROUPS])) {
$groups = ['serializer_groups' => $context[AbstractNormalizer::GROUPS]];
} else {
$groups = $this->getSerializerGroups($options, $serializerContext);
}
Expand Down Expand Up @@ -137,7 +139,7 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt

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

Expand Down Expand Up @@ -280,10 +282,10 @@ private function getSerializerContext(string $resourceClass, string $contextType
*/
private function getSerializerGroups(array $options, array $context): array
{
if (empty($context['groups'])) {
if (empty($context[AbstractNormalizer::GROUPS])) {
return $options;
}

return ['serializer_groups' => $context['groups']];
return ['serializer_groups' => $context[AbstractNormalizer::GROUPS]];
}
}
21 changes: 11 additions & 10 deletions src/Bridge/NelmioApiDoc/Parser/ApiPlatformParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Nelmio\ApiDocBundle\Parser\ParserInterface;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

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

if (isset($attributes['normalization_context']['groups'])) {
$options['serializer_groups'] = $attributes['normalization_context']['groups'];
if (isset($attributes['normalization_context'][AbstractNormalizer::GROUPS])) {
$options['serializer_groups'] = $attributes['normalization_context'][AbstractNormalizer::GROUPS];
}

if (isset($attributes['denormalization_context']['groups'])) {
if (isset($attributes['denormalization_context'][AbstractNormalizer::GROUPS])) {
if (isset($options['serializer_groups'])) {
$options['serializer_groups'] += $attributes['denormalization_context']['groups'];
$options['serializer_groups'] += $attributes['denormalization_context'][AbstractNormalizer::GROUPS];
} else {
$options['serializer_groups'] = $attributes['denormalization_context']['groups'];
$options['serializer_groups'] = $attributes['denormalization_context'][AbstractNormalizer::GROUPS];
}
}

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

return [
$groupsContext => [
'groups' => array_merge($itemOperationAttribute ?? [], $collectionOperationAttribute ?? []),
AbstractNormalizer::GROUPS => array_merge($itemOperationAttribute ?? [], $collectionOperationAttribute ?? []),
],
];
}
Expand All @@ -157,13 +158,13 @@ private function getGroupsForItemAndCollectionOperation(ResourceMetadata $resour

if (self::OUT_PREFIX === $io) {
return [
'serializer_groups' => !empty($operation['normalization_context']) ? $operation['normalization_context']['groups'] : [],
'serializer_groups' => !empty($operation['normalization_context']) ? $operation['normalization_context'][AbstractNormalizer::GROUPS] : [],
];
}

if (self::IN_PREFIX === $io) {
return [
'serializer_groups' => !empty($operation['denormalization_context']) ? $operation['denormalization_context']['groups'] : [],
'serializer_groups' => !empty($operation['denormalization_context']) ? $operation['denormalization_context'][AbstractNormalizer::GROUPS] : [],
];
}

Expand Down
3 changes: 2 additions & 1 deletion src/EventListener/DeserializeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\SerializerInterface;

/**
Expand Down Expand Up @@ -61,7 +62,7 @@ public function onKernelRequest(GetResponseEvent $event)

$data = $request->attributes->get('data');
if (null !== $data) {
$context['object_to_populate'] = $data;
$context[AbstractNormalizer::OBJECT_TO_POPULATE] = $data;
}

$request->attributes->set(
Expand Down
11 changes: 6 additions & 5 deletions src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
Expand Down Expand Up @@ -154,17 +155,17 @@ private function getPropertyNameCollectionFactoryContext(ResourceMetadata $resou
$attributes = $resourceMetadata->getAttributes();
$context = [];

if (isset($attributes['normalization_context']['groups'])) {
$context['serializer_groups'] = $attributes['normalization_context']['groups'];
if (isset($attributes['normalization_context'][AbstractNormalizer::GROUPS])) {
$context['serializer_groups'] = $attributes['normalization_context'][AbstractNormalizer::GROUPS];
}

if (isset($attributes['denormalization_context']['groups'])) {
if (isset($attributes['denormalization_context'][AbstractNormalizer::GROUPS])) {
if (isset($context['serializer_groups'])) {
foreach ($attributes['denormalization_context']['groups'] as $groupName) {
foreach ($attributes['denormalization_context'][AbstractNormalizer::GROUPS] as $groupName) {
$context['serializer_groups'][] = $groupName;
}
} else {
$context['serializer_groups'] = $attributes['denormalization_context']['groups'];
$context['serializer_groups'] = $attributes['denormalization_context'][AbstractNormalizer::GROUPS];
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/JsonLd/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ public function supportsDenormalization($data, $type, $format = null)
public function denormalize($data, $class, $format = null, array $context = [])
{
// Avoid issues with proxies if we populated the object
if (isset($data['@id']) && !isset($context['object_to_populate'])) {
if (isset($data['@id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
throw new InvalidArgumentException('Update is not allowed for this operation.');
}

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

return parent::denormalize($data, $class, $format, $context);
Expand Down
4 changes: 2 additions & 2 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ protected function getFactoryOptions(array $context): array
{
$options = [];

if (isset($context['groups'])) {
$options['serializer_groups'] = $context['groups'];
if (isset($context[self::GROUPS])) {
$options['serializer_groups'] = $context[self::GROUPS];
}

if (isset($context['collection_operation_name'])) {
Expand Down
7 changes: 4 additions & 3 deletions src/Serializer/Filter/GroupFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Core\Serializer\Filter;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

/**
* Group filter.
Expand Down Expand Up @@ -44,11 +45,11 @@ public function apply(Request $request, bool $normalization, array $attributes,
if (null !== $this->whitelist) {
$groups = array_intersect($this->whitelist, $groups);
}
if (!$this->overrideDefaultGroups && isset($context['groups'])) {
$groups = array_merge((array) $context['groups'], $groups);
if (!$this->overrideDefaultGroups && isset($context[AbstractNormalizer::GROUPS])) {
$groups = array_merge((array) $context[AbstractNormalizer::GROUPS], $groups);
}

$context['groups'] = $groups;
$context[AbstractNormalizer::GROUPS] = $groups;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Serializer/Filter/PropertyFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Core\Serializer\Filter;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

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

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

$context['attributes'] = $properties;
$context[AbstractNormalizer::ATTRIBUTES] = $properties;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class ItemNormalizer extends AbstractItemNormalizer
public function denormalize($data, $class, $format = null, array $context = [])
{
// Avoid issues with proxies if we populated the object
if (isset($data['id']) && !isset($context['object_to_populate'])) {
if (isset($data['id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
throw new InvalidArgumentException('Update is not allowed for this operation.');
}
Expand All @@ -44,7 +44,7 @@ public function denormalize($data, $class, $format = null, array $context = [])
private function updateObjectToPopulate(array $data, array &$context)
{
try {
$context['object_to_populate'] = $this->iriConverter->getItemFromIri((string) $data['id'], $context + ['fetch_data' => true]);
$context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getItemFromIri((string) $data['id'], $context + ['fetch_data' => true]);
} catch (InvalidArgumentException $e) {
$identifier = null;
foreach ($this->propertyNameCollectionFactory->create($context['resource_class'], $context) as $propertyName) {
Expand All @@ -58,7 +58,7 @@ private function updateObjectToPopulate(array $data, array &$context)
throw $e;
}

$context['object_to_populate'] = $this->iriConverter->getItemFromIri(sprintf('%s/%s', $this->iriConverter->getIriFromResourceClass($context['resource_class']), $data[$identifier]), $context + ['fetch_data' => true]);
$context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getItemFromIri(sprintf('%s/%s', $this->iriConverter->getIriFromResourceClass($context['resource_class']), $data[$identifier]), $context + ['fetch_data' => true]);
}
}
}
5 changes: 3 additions & 2 deletions src/Swagger/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Psr\Container\ContainerInterface;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

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

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

$options = isset($serializerContext['groups']) ? ['serializer_groups' => $serializerContext['groups']] : [];
$options = isset($serializerContext[AbstractNormalizer::GROUPS]) ? ['serializer_groups' => $serializerContext[AbstractNormalizer::GROUPS]] : [];
foreach ($this->propertyNameCollectionFactory->create($resourceClass, $options) as $propertyName) {
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
$normalizedPropertyName = $this->nameConverter ? $this->nameConverter->normalize($propertyName) : $propertyName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

/**
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
Expand Down Expand Up @@ -315,7 +316,7 @@ public function testDenormalizeItemWithExistingGroups()
$queryBuilderProphecy->getEntityManager()->willReturn($emProphecy);

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

/**
Expand Down Expand Up @@ -442,7 +443,7 @@ public function testMaxDepth()
public function testForceEager()
{
$resourceMetadata = new ResourceMetadata();
$resourceMetadata = $resourceMetadata->withAttributes(['normalization_context' => ['groups' => 'foobar']]);
$resourceMetadata = $resourceMetadata->withAttributes(['normalization_context' => [AbstractNormalizer::GROUPS => 'foobar']]);
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn($resourceMetadata);

Expand Down Expand Up @@ -617,7 +618,7 @@ public function testApplyToCollectionWithSerializerContextBuilder()
$requestStack->push($request);

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

$queryBuilder = $queryBuilderProphecy->reveal();
$eagerExtensionTest = new EagerLoadingExtension($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), 30, false, $requestStack, $serializerContextBuilderProphecy->reveal(), true);
Expand Down
11 changes: 6 additions & 5 deletions tests/Bridge/NelmioApiDoc/Parser/ApiPlatformParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Prophecy\Argument;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

/**
* @author Teoh Han Hui <teohhanhui@gmail.com>
Expand Down Expand Up @@ -94,8 +95,8 @@ public function testSupportsAttributeNormalization()
{
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('Acme\CustomAttributeDummy')->willReturn(new ResourceMetadata('dummy', 'dummy', null, [
'get' => ['method' => 'GET', 'normalization_context' => ['groups' => ['custom_attr_dummy_get']]],
'put' => ['method' => 'PUT', 'denormalization_context' => ['groups' => ['custom_attr_dummy_put']]],
'get' => ['method' => 'GET', 'normalization_context' => [AbstractNormalizer::GROUPS => ['custom_attr_dummy_get']]],
'put' => ['method' => 'PUT', 'denormalization_context' => [AbstractNormalizer::GROUPS => ['custom_attr_dummy_put']]],
'delete' => ['method' => 'DELETE'],
], []))->shouldBeCalled();
$resourceMetadataFactory = $resourceMetadataFactoryProphecy->reveal();
Expand Down Expand Up @@ -189,9 +190,9 @@ public function testParse()
{
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata('dummy', 'dummy', null, [
'get' => ['method' => 'GET', 'normalization_context' => ['groups' => ['custom_attr_dummy_get']]],
'put' => ['method' => 'PUT', 'denormalization_context' => ['groups' => ['custom_attr_dummy_put']]],
'gerard' => ['method' => 'get', 'path' => '/gerard', 'denormalization_context' => ['groups' => ['custom_attr_dummy_put']]],
'get' => ['method' => 'GET', 'normalization_context' => [AbstractNormalizer::GROUPS => ['custom_attr_dummy_get']]],
'put' => ['method' => 'PUT', 'denormalization_context' => [AbstractNormalizer::GROUPS => ['custom_attr_dummy_put']]],
'gerard' => ['method' => 'get', 'path' => '/gerard', 'denormalization_context' => [AbstractNormalizer::GROUPS => ['custom_attr_dummy_put']]],
'delete' => ['method' => 'DELETE'],
], []))->shouldBeCalled();
$resourceMetadataFactory = $resourceMetadataFactoryProphecy->reveal();
Expand Down
Loading