Skip to content

Commit

Permalink
Use serializer AbstractItemNormalizer constants when related
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka authored and abluchet committed Oct 25, 2017
1 parent ffb3d0f commit 7fc53b9
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 61 deletions.
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
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/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
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 <[email protected]>
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 <[email protected]>
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
3 changes: 2 additions & 1 deletion tests/EventListener/DeserializeListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Prophecy\Argument;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\SerializerInterface;

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

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

$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Symfony\Component\Serializer\Mapping\AttributeMetadata as SerializerAttributeMetadata;
use Symfony\Component\Serializer\Mapping\ClassMetadata as SerializerClassMetadata;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface as SerializerClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

/**
* @author Teoh Han Hui <[email protected]>
Expand Down Expand Up @@ -55,10 +56,10 @@ public function testCreate()
$dummyResourceMetadata = (new ResourceMetadata())
->withAttributes([
'normalization_context' => [
'groups' => ['dummy_read'],
AbstractNormalizer::GROUPS => ['dummy_read'],
],
'denormalization_context' => [
'groups' => ['dummy_write'],
AbstractNormalizer::GROUPS => ['dummy_write'],
],
]);
$resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn($dummyResourceMetadata)->shouldBeCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

/**
* Resource metadata provider for file configured factories tests.
Expand All @@ -40,10 +41,10 @@ public function resourceMetadataProvider()
'iri' => 'someirischema',
'attributes' => [
'normalization_context' => [
'groups' => ['default'],
AbstractNormalizer::GROUPS => ['default'],
],
'denormalization_context' => [
'groups' => ['default'],
AbstractNormalizer::GROUPS => ['default'],
],
'hydra_context' => [
'@type' => 'hydra:Operation',
Expand Down
Loading

0 comments on commit 7fc53b9

Please sign in to comment.