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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
* Delete the `api_platform.doctrine.listener.view.write` event listener service.
* Add a data persistence layer with a new `ApiPlatform\Core\DataPersister\DataPersisterInterface` interface.

## 2.1.3

* Don't use dynamic values in Varnish-related service keys (improves Symfony 3.3 compatibility)
* Hydra: Fix the value of `owl:allValuesFrom` in the API documentation
* Swagger: Include the context even when the type is `null`
* Minor code and PHPDoc cleanups

## 2.1.2

* PHP 7.2 compatibility
Expand Down
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]];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
$queryBuilderClone->andWhere($queryBuilderClone->expr()->in($originAlias, $in->getDQL()));
} else {
// Because Doctrine doesn't support WHERE ( foo, bar ) IN () (https://github.com/doctrine/doctrine2/issues/5238), we are building as many subqueries as they are identifiers
foreach ($classMetadata->identifier as $identifier) {
foreach ($classMetadata->getIdentifier() as $identifier) {
if (!$classMetadata->hasAssociation($identifier)) {
continue;
}

$replacementAlias = $queryNameGenerator->generateJoinAlias($originAlias);
$in = $this->getQueryBuilderWithNewAliases($queryBuilder, $queryNameGenerator, $originAlias, $replacementAlias);
$in->select("IDENTITY($replacementAlias.$identifier)");
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Doctrine/Orm/Util/EagerLoadingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private function hasFetchEagerAssociation(EntityManager $em, ClassMetadataInfo $
{
$checked[] = $classMetadata->name;

foreach ($classMetadata->associationMappings as $mapping) {
foreach ($classMetadata->getAssociationMappings() as $mapping) {
if (ClassMetadataInfo::FETCH_EAGER === $mapping['fetch']) {
return true;
}
Expand Down
10 changes: 7 additions & 3 deletions src/Bridge/Doctrine/Orm/Util/QueryChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,16 @@ public static function hasOrderByOnToManyJoin(QueryBuilder $queryBuilder, Manage
if (!isset($orderByAliases[$alias])) {
continue;
}

$relationship = QueryJoinParser::getJoinRelationship($join);

if (false !== strpos($relationship, '.')) {
$metadata = QueryJoinParser::getClassMetadataFromJoinAlias($alias, $queryBuilder, $managerRegistry);
if ($metadata->isCollectionValuedAssociation($relationship)) {
/*
* We select the parent alias because it may differ from the origin alias given above
* @see https://github.com/api-platform/core/issues/1313
*/
list($relationAlias, $association) = explode('.', $relationship);
$metadata = QueryJoinParser::getClassMetadataFromJoinAlias($relationAlias, $queryBuilder, $managerRegistry);
if ($metadata->isCollectionValuedAssociation($association)) {
return true;
}
} else {
Expand Down
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 @@ -117,15 +118,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 @@ -135,12 +136,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 @@ -161,13 +162,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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Validator\Validator\ValidatorInterface;
Expand Down Expand Up @@ -470,17 +469,15 @@ private function registerHttpCache(ContainerBuilder $container, array $config, X

$loader->load('http_cache_tags.xml');

$references = [];
foreach ($config['http_cache']['invalidation']['varnish_urls'] as $url) {
$id = sprintf('api_platform.http_cache.purger.varnish_client.%s', $url);
$references[] = new Reference($id);

$definitions = [];
foreach ($config['http_cache']['invalidation']['varnish_urls'] as $key => $url) {
$definition = new ChildDefinition('api_platform.http_cache.purger.varnish_client');
$definition->addArgument(['base_uri' => $url]);
$container->setDefinition($id, $definition);

$definitions[] = $definition;
}

$container->getDefinition('api_platform.http_cache.purger.varnish')->addArgument($references);
$container->getDefinition('api_platform.http_cache.purger.varnish')->addArgument($definitions);
$container->setAlias('api_platform.http_cache.purger', 'api_platform.http_cache.purger.varnish');
}

Expand Down
3 changes: 2 additions & 1 deletion src/DataProvider/PaginatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
namespace ApiPlatform\Core\DataProvider;

/**
* Paginator Interface.
* The \Countable implementation should return the number of items on the
* current page, as an integer.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
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
17 changes: 9 additions & 8 deletions src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
Expand Down Expand Up @@ -101,11 +102,11 @@ private function populateEntrypointProperties(string $resourceClass, ResourceMet
'domain' => '#Entrypoint',
'rdfs:label' => "The collection of $shortName resources",
'rdfs:range' => [
'hydra:Collection',
['@id' => 'hydra:Collection'],
[
'owl:equivalentClass' => [
'owl:onProperty' => 'hydra:member',
'owl:allValuesFrom' => "#$shortName",
'owl:onProperty' => ['@id' => 'hydra:member'],
'owl:allValuesFrom' => ['@id' => $prefixedShortName],
],
],
],
Expand Down Expand Up @@ -157,17 +158,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 @@ -352,8 +352,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 @@ -52,11 +53,11 @@ public function apply(Request $request, bool $normalization, array $attributes,
$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 @@ -54,11 +55,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]);
}
}
}
Loading