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
48 changes: 32 additions & 16 deletions src/GraphQl/Type/FieldsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,20 +512,16 @@ private function getParameterArgs(Operation $operation, array $args = []): array
$name = key($leafs);

$filterLeafs = [];
if (($filterId = $parameter->getFilter()) && $this->filterLocator->has($filterId)) {
$filter = $this->filterLocator->get($filterId);

if ($filter instanceof FilterInterface) {
$property = $parameter->getProperty() ?? $name;
$property = str_replace('.', $this->nestingSeparator, $property);
$description = $filter->getDescription($operation->getClass());

foreach ($description as $descKey => $descValue) {
$descKey = str_replace('.', $this->nestingSeparator, $descKey);
parse_str($descKey, $descValues);
if (isset($descValues[$property]) && \is_array($descValues[$property])) {
$filterLeafs = array_merge($filterLeafs, $descValues[$property]);
}
if ($filter = $this->resolveFilter($parameter->getFilter())) {
$property = $parameter->getProperty() ?? $name;
$property = str_replace('.', $this->nestingSeparator, $property);
$description = $filter->getDescription($operation->getClass());

foreach ($description as $descKey => $descValue) {
$descKey = str_replace('.', $this->nestingSeparator, $descKey);
parse_str($descKey, $descValues);
if (isset($descValues[$property]) && \is_array($descValues[$property])) {
$filterLeafs = array_merge($filterLeafs, $descValues[$property]);
}
}
}
Expand Down Expand Up @@ -612,12 +608,12 @@ private function getFilterArgs(array $args, ?string $resourceClass, string $root
}

foreach ($resourceOperation->getFilters() ?? [] as $filterId) {
if (!$this->filterLocator->has($filterId)) {
if (!($filter = $this->resolveFilter($filterId))) {
continue;
}

$entityClass = $this->getStateOptionsClass($resourceOperation, $resourceOperation->getClass());
foreach ($this->filterLocator->get($filterId)->getDescription($entityClass) as $key => $description) {
foreach ($filter->getDescription($entityClass) as $key => $description) {
$filterType = \in_array($description['type'], TypeIdentifier::values(), true) ? Type::builtin($description['type']) : Type::object($description['type']);
if (!($description['required'] ?? false)) {
$filterType = Type::nullable($filterType);
Expand Down Expand Up @@ -751,4 +747,24 @@ private function normalizePropertyName(string $property, string $resourceClass):

return $this->nameConverter->normalize($property, $resourceClass);
}

/**
* Resolves a filter reference to a {@see FilterInterface} instance, supporting
* both a string service id (legacy/locator path) and an object form
* (`new QueryParameter(filter: new SortFilter())`).
*/
private function resolveFilter(mixed $filter): ?FilterInterface
{
if ($filter instanceof FilterInterface) {
return $filter;
}

if (\is_string($filter) && $this->filterLocator->has($filter)) {
$resolved = $this->filterLocator->get($filter);

return $resolved instanceof FilterInterface ? $resolved : null;
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7966;

use ApiPlatform\Doctrine\Orm\Filter\SortFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GraphQl\QueryCollection;
use ApiPlatform\Metadata\QueryParameter;

#[ApiResource(
operations: [],
graphQlOperations: [
new QueryCollection(
provider: [self::class, 'provide'],
paginationEnabled: false,
parameters: [
'order[:property]' => new QueryParameter(filter: new SortFilter()),
],
),
],
)]
final class SortFilterParameterDummy
{
public ?string $id = null;
public ?string $name = null;

public static function provide(): array
{
return [];
}
}
52 changes: 52 additions & 0 deletions tests/Functional/GraphQl/Issue7966Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Functional\GraphQl;

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7966\SortFilterParameterDummy;
use ApiPlatform\Tests\SetupClassResourcesTrait;

/**
* Object-form filter (FilterInterface instance) combined with a bracketed parameter
* key crashed the GraphQL schema build because `filterLocator->has()` was called
* with the instance rather than a string service id.
*
* @see https://github.com/api-platform/core/issues/7966
*/
final class Issue7966Test extends ApiTestCase
{
use SetupClassResourcesTrait;

protected static ?bool $alwaysBootKernel = false;

/**
* @return class-string[]
*/
public static function getResources(): array
{
return [SortFilterParameterDummy::class];
}

public function testSchemaBuildsWithObjectFormFilterAndBracketedKey(): void
{
$response = self::createClient()->request('POST', '/graphql', ['json' => [
'query' => '{ __type(name: "SortFilterParameterDummy") { name } }',
]]);

$this->assertResponseIsSuccessful();
$json = $response->toArray(false);
$this->assertArrayNotHasKey('errors', $json, json_encode($json['errors'] ?? null));
$this->assertSame('SortFilterParameterDummy', $json['data']['__type']['name']);
}
}
Loading