Skip to content

Commit

Permalink
Merge 7d8660b into 11fa1e7
Browse files Browse the repository at this point in the history
  • Loading branch information
mtarld committed Sep 6, 2021
2 parents 11fa1e7 + 7d8660b commit d984de8
Show file tree
Hide file tree
Showing 47 changed files with 425 additions and 423 deletions.
2 changes: 1 addition & 1 deletion src/Core/Action/ExceptionAction.php
Expand Up @@ -74,7 +74,7 @@ public function __invoke($exception, Request $request): Response

$exceptionToStatus = array_merge(
$this->exceptionToStatus,
$operation ? $operation->getExceptionToStatus() : $this->getOperationExceptionToStatus($request)
$operation ? ($operation->getExceptionToStatus() ?? []) : $this->getOperationExceptionToStatus($request)
);

foreach ($exceptionToStatus as $class => $status) {
Expand Down
Expand Up @@ -269,7 +269,7 @@ private function publishUpdate($object, array $options, string $type): void
$data = json_encode(['@id' => $object->id]);
} else {
$resourceClass = $this->getObjectClass($object);
$context = $options['normalization_context'] ?? $this->resourceMetadataFactory->create($resourceClass)->getOperation()->getNormalizationContext();
$context = $options['normalization_context'] ?? $this->resourceMetadataFactory->create($resourceClass)->getOperation()->getNormalizationContext() ?? [];

$iri = $options['topics'] ?? $this->iriConverter->getIriFromItem($object, null, UrlGeneratorInterface::ABS_URL);
$data = $options['data'] ?? $this->serializer->serialize($object, key($this->formats), $context);
Expand Down
Expand Up @@ -61,13 +61,13 @@ public function applyToCollection(Builder $aggregationBuilder, string $resourceC
if (null !== $this->resourceMetadataFactory) {
if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
if (isset($context['operation'])) {
$defaultOrder = $context['operation']->getOrder();
$defaultOrder = $context['operation']->getOrder() ?? [];
} else {
$metadata = $this->resourceMetadataFactory->create($resourceClass);
try {
$defaultOrder = isset($context['graphql_operation_name']) ? $metadata->getGraphQlOperation($operationName)->getOrder() : $metadata->getOperation($operationName)->getOrder();
$defaultOrder = isset($context['graphql_operation_name']) ? ($metadata->getGraphQlOperation($operationName)->getOrder() ?? []) : ($metadata->getOperation($operationName)->getOrder() ?? []);
} catch (OperationNotFoundException $e) {
$defaultOrder = $metadata->getOperation(null, true)->getOrder();
$defaultOrder = $metadata->getOperation(null, true)->getOrder() ?? [];
}
}
} else {
Expand Down
Expand Up @@ -135,7 +135,7 @@ private function apply(bool $collection, QueryBuilder $queryBuilder, QueryNameGe
if (null !== $this->requestStack && null !== $this->serializerContextBuilder && null !== $request = $this->requestStack->getCurrentRequest()) {
$context += $this->serializerContextBuilder->createFromRequest($request, 'normalization_context' === $contextType);
} elseif ($operation) {
$context += 'denormalization_context' === $contextType ? $operation->getDenormalizationContext() : $operation->getNormalizationContext();
$context += 'denormalization_context' === $contextType ? ($operation->getDenormalizationContext() ?? []) : ($operation->getNormalizationContext() ?? []);
} elseif (!$this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
// TODO: remove in 3.0
$context += $this->getNormalizationContext($context['resource_class'] ?? $resourceClass, $contextType, $options);
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Bridge/Doctrine/Orm/Extension/OrderExtension.php
Expand Up @@ -64,7 +64,7 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
$resourceMetadataCollection = $this->resourceMetadataFactory->create($resourceClass);
try {
$defaultOrder = isset($context['graphql_operation_name']) ? $resourceMetadataCollection->getGraphQlOperation($operationName)->getOrder() : $resourceMetadataCollection->getOperation($operationName)->getOrder();
$defaultOrder = isset($context['graphql_operation_name']) ? ($resourceMetadataCollection->getGraphQlOperation($operationName)->getOrder() ?? []) : ($resourceMetadataCollection->getOperation($operationName)->getOrder() ?? []);
} catch (OperationNotFoundException $e) {
// In some cases the operation may not exist
$defaultOrder = [];
Expand Down
Expand Up @@ -67,7 +67,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): \Generato
private function getExpectedInputClass(Request $request): ?string
{
$operation = $this->initializeOperation($request);
if (Request::METHOD_DELETE === $request->getMethod() || $request->isMethodSafe() || !$operation->canDeserialize()) {
if (Request::METHOD_DELETE === $request->getMethod() || $request->isMethodSafe() || !($operation->canDeserialize() ?? true)) {
return null;
}

Expand Down
Expand Up @@ -129,7 +129,7 @@ public function collect(Request $request, Response $response, \Throwable $except

private function setFilters(ApiResource $resourceMetadata, int $index, array &$filters, array &$counters): void
{
foreach ($resourceMetadata->getFilters() as $id) {
foreach ($resourceMetadata->getFilters() ?? [] as $id) {
if ($this->filterLocator->has($id)) {
$filters[$index][$id] = \get_class($this->filterLocator->get($id));
continue;
Expand Down
5 changes: 3 additions & 2 deletions src/Core/Bridge/Symfony/Routing/ApiLoader.php
Expand Up @@ -22,6 +22,7 @@
use ApiPlatform\Core\PathResolver\OperationPathResolverInterface;
use ApiPlatform\Exception\InvalidResourceException;
use ApiPlatform\Exception\RuntimeException;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\Loader;
Expand Down Expand Up @@ -111,7 +112,7 @@ public function load($data, $type = null): RouteCollection
$route = new Route(
($operation->getRoutePrefix() ?? '').$operation->getUriTemplate(),
[
'_controller' => $operation->getController(),
'_controller' => $operation->getController() ?: 'api_platform.action.placeholder',
'_format' => null,
'_stateless' => $operation->getStateless(),
'_api_resource_class' => $resourceClass,
Expand All @@ -122,7 +123,7 @@ public function load($data, $type = null): RouteCollection
$operation->getOptions() ?? [],
$operation->getHost() ?? '',
$operation->getSchemes() ?? [],
[$operation->getMethod()],
[$operation->getMethod() ?? Operation::METHOD_GET],
$operation->getCondition() ?? ''
);

Expand Down
2 changes: 1 addition & 1 deletion src/Core/EventListener/DeserializeListener.php
Expand Up @@ -95,7 +95,7 @@ public function onKernelRequest(RequestEvent $event): void
}

if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface &&
(!$operation || !$operation->canDeserialize() || !$attributes['receive'])
(!$operation || !($operation->canDeserialize() ?? true) || !$attributes['receive'])
) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Core/EventListener/QueryParameterValidateListener.php
Expand Up @@ -67,7 +67,7 @@ public function onKernelRequest(RequestEvent $event)
}

if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface &&
(!$operation || !$operation->canQueryParameterValidate() || !$operation->isCollection())
(!$operation || !($operation->canQueryParameterValidate() ?? true) || !$operation->isCollection())
) {
return;
}
Expand All @@ -89,7 +89,7 @@ public function onKernelRequest(RequestEvent $event)
if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
$resourceFilters = $this->resourceMetadataFactory->create($attributes['resource_class'])->getCollectionOperationAttribute($operationName, 'filters', [], true);
} elseif ($operation) {
$resourceFilters = $operation->getFilters();
$resourceFilters = $operation->getFilters() ?? [];
}
$this->queryParameterValidator->validateFilters($attributes['resource_class'], $resourceFilters, $queryParameters);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/EventListener/ReadListener.php
Expand Up @@ -97,7 +97,7 @@ public function onKernelRequest(RequestEvent $event): void
}

if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface &&
(!$operation || !$operation->canRead() || !$attributes['receive'] || (!$operation->getIdentifiers() && !$request->isMethodSafe()))
(!$operation || !($operation->canRead() ?? true) || !$attributes['receive'] || (!$operation->getIdentifiers() && !$request->isMethodSafe()))
) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/EventListener/SerializeListener.php
Expand Up @@ -88,7 +88,7 @@ public function onKernelView(ViewEvent $event): void
}

if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface &&
($operation && !$operation->canSerialize())
($operation && !($operation->canSerialize() ?? true))
) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/EventListener/WriteListener.php
Expand Up @@ -86,7 +86,7 @@ public function onKernelView(ViewEvent $event): void
}

if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface &&
(!$operation || !$operation->canWrite() || !$attributes['persist'])
(!$operation || !($operation->canWrite() ?? true) || !$attributes['persist'])
) {
return;
// TODO: 3.0 remove condition
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Hydra/Serializer/DocumentationNormalizer.php
Expand Up @@ -372,7 +372,7 @@ private function getHydraOperations(string $resourceClass, $resourceMetadata, st
private function getHydraOperation(string $resourceClass, $resourceMetadata, string $operationName, $operation, string $prefixedShortName, ?string $operationType = null, SubresourceMetadata $subresourceMetadata = null): array
{
if ($operation instanceof Operation) {
$method = $operation->getMethod();
$method = $operation->getMethod() ?? Operation::METHOD_GET;
} elseif ($this->operationMethodResolver) {
if (OperationType::COLLECTION === $operationType) {
$method = $this->operationMethodResolver->getCollectionOperationMethod($resourceClass, $operationName);
Expand All @@ -385,7 +385,7 @@ private function getHydraOperation(string $resourceClass, $resourceMetadata, str
$method = $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'method', 'GET');
}

$hydraOperation = $operation instanceof Operation ? $operation->getHydraContext() : ($operation['hydra_context'] ?? []);
$hydraOperation = $operation instanceof Operation ? ($operation->getHydraContext() ?? []) : ($operation['hydra_context'] ?? []);
if ($operation instanceof Operation ? $operation->getDeprecationReason() : $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'deprecation_reason', null, true)) {
$hydraOperation['owl:deprecated'] = true;
}
Expand Down
Expand Up @@ -94,7 +94,7 @@ public function normalize($object, $format = null, array $context = [])
$isPaginatedWithCursor = null !== $cursorPaginationAttribute = $metadata->getCollectionOperationAttribute($context['collection_operation_name'] ?? $context['subresource_operation_name'], 'pagination_via_cursor', null, true);
} elseif ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface && isset($context['resource_class']) && $paginated) {
$operation = $this->resourceMetadataFactory->create($context['resource_class'])->getOperation($context['operation_name'] ?? null);
$isPaginatedWithCursor = [] !== $cursorPaginationAttribute = $operation->getPaginationViaCursor();
$isPaginatedWithCursor = [] !== $cursorPaginationAttribute = ($operation->getPaginationViaCursor() ?? []);
}

$data['hydra:view'] = ['@id' => null, '@type' => 'hydra:PartialCollectionView'];
Expand Down
6 changes: 3 additions & 3 deletions src/Core/JsonSchema/SchemaFactory.php
Expand Up @@ -366,14 +366,14 @@ private function getSerializerContext($resourceMetadata, string $type = Schema::
return $resourceMetadata->getAttribute($attribute, []);
}

return Schema::TYPE_OUTPUT === $type ? $operation->getNormalizationContext() : $operation->getDenormalizationContext();
return Schema::TYPE_OUTPUT === $type ? ($operation->getNormalizationContext() ?? []) : ($operation->getDenormalizationContext() ?? []);
}

if ($resourceMetadata instanceof ResourceMetadata) {
return $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, $attribute, [], true);
}

return Schema::TYPE_OUTPUT === $type ? $operation->getNormalizationContext() : $operation->getDenormalizationContext();
return Schema::TYPE_OUTPUT === $type ? ($operation->getNormalizationContext() ?? []) : ($operation->getDenormalizationContext() ?? []);
}

/**
Expand All @@ -391,7 +391,7 @@ private function getValidationGroups($resourceMetadata, ?string $operationType,
return \is_array($validationGroups = $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, $attribute, [], true)) ? $validationGroups : [];
}

$groups = $resourceMetadata ? $resourceMetadata->getValidationContext()['groups'] ?? [] : [];
$groups = $resourceMetadata ? ($resourceMetadata->getValidationContext()['groups'] ?? []) : [];

return \is_array($groups) ? $groups : [$groups];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Serializer/SerializerContextBuilder.php
Expand Up @@ -55,7 +55,7 @@ public function createFromRequest(Request $request, bool $normalization, array $
&& isset($attributes['operation_name'])
) {
$operation = $attributes['operation'] ?? $this->resourceMetadataFactory->create($attributes['resource_class'])->getOperation($attributes['operation_name']);
$context = $normalization ? $operation->getNormalizationContext() : $operation->getDenormalizationContext();
$context = $normalization ? ($operation->getNormalizationContext() ?? []) : ($operation->getDenormalizationContext() ?? []);
$context['operation_name'] = $attributes['operation_name'];
$context['operation'] = $operation;
$context['resource_class'] = $attributes['resource_class'];
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Validator/EventListener/ValidateListener.php
Expand Up @@ -71,7 +71,7 @@ public function onKernelView(ViewEvent $event): void
}

if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface &&
(!$operation || !$operation->canValidate())
(!$operation || !($operation->canValidate() ?? true))
) {
return;
}
Expand All @@ -86,7 +86,7 @@ public function onKernelView(ViewEvent $event): void
return;
}

$validationContext = $operation ? $operation->getValidationContext() : [];
$validationContext = $operation ? ($operation->getValidationContext() ?? []) : [];

if (!$validationContext && $this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
$resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQl/Resolver/Stage/DeserializeStage.php
Expand Up @@ -46,7 +46,7 @@ public function __invoke($objectToPopulate, string $resourceClass, string $opera
{
$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
$operation = $resourceMetadataCollection->getGraphQlOperation($operationName);
if (!$operation->canDeserialize()) {
if (!($operation->canDeserialize() ?? true)) {
return $objectToPopulate;
}

Expand Down
2 changes: 1 addition & 1 deletion src/GraphQl/Resolver/Stage/ReadStage.php
Expand Up @@ -69,7 +69,7 @@ public function __invoke(?string $resourceClass, ?string $rootClass, string $ope
// ReadStage may be invoked without an existing operation
}

if ($operation && !$operation->canRead()) {
if ($operation && !($operation->canRead() ?? true)) {
return $context['is_collection'] ? [] : null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/GraphQl/Resolver/Stage/SerializeStage.php
Expand Up @@ -72,7 +72,7 @@ public function __invoke($itemOrCollection, string $resourceClass, string $opera
// In some cases the operation may not exist
}

if ($operation && !$operation->canSerialize()) {
if ($operation && !($operation->canSerialize()) ?? true) {
if ($isCollection) {
if ($this->pagination->isGraphQlEnabled($resourceClass, $operationName, $context)) {
return 'cursor' === $this->pagination->getGraphQlPaginationType($resourceClass, $operationName) ?
Expand Down
4 changes: 2 additions & 2 deletions src/GraphQl/Resolver/Stage/ValidateStage.php
Expand Up @@ -42,10 +42,10 @@ public function __invoke($object, string $resourceClass, string $operationName,
$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
$operation = $resourceMetadataCollection->getGraphQlOperation($operationName);

if (!$operation->canValidate()) {
if (!($operation->canValidate() ?? true)) {
return;
}

$this->validator->validate($object, $operation->getValidationContext());
$this->validator->validate($object, $operation->getValidationContext() ?? []);
}
}
2 changes: 1 addition & 1 deletion src/GraphQl/Resolver/Stage/WriteStage.php
Expand Up @@ -45,7 +45,7 @@ public function __invoke($data, string $resourceClass, string $operationName, ar
{
$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
$operation = $resourceMetadataCollection->getGraphQlOperation($operationName);
if (null === $data || !$operation->canWrite()) {
if (null === $data || !($operation->canWrite() ?? true)) {
return $data;
}

Expand Down
2 changes: 1 addition & 1 deletion src/GraphQl/Serializer/SerializerContextBuilder.php
Expand Up @@ -64,7 +64,7 @@ public function create(?string $resourceClass, string $operationName, array $res
if ($operation) {
$context['input'] = $operation->getInput();
$context['output'] = $operation->getOutput();
$context = $normalization ? array_merge($operation->getNormalizationContext(), $context) : array_merge($operation->getDenormalizationContext(), $context);
$context = $normalization ? array_merge($operation->getNormalizationContext() ?? [], $context) : array_merge($operation->getDenormalizationContext() ?? [], $context);
}

if ($normalization) {
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQl/Type/FieldsBuilder.php
Expand Up @@ -403,7 +403,7 @@ private function getFilterArgs(array $args, ?string $resourceClass, $operation,
return $args;
}

foreach ($operation->getFilters() as $filterId) {
foreach ($operation->getFilters() ?? [] as $filterId) {
if (null === $this->filterLocator || !$this->filterLocator->has($filterId)) {
continue;
}
Expand Down

0 comments on commit d984de8

Please sign in to comment.