Skip to content

Commit d75363a

Browse files
Maxcastelsoyuka
andauthored
fix(metadata): reject duplicate GraphQL operation names instead of silently dropping them (#8713)
* fix(metadata): reject duplicate GraphQL operation names * fix(metadata): reject duplicate GraphQL operation names in YAML and XML * test(metadata): import RuntimeException --------- Co-authored-by: soyuka <soyuka@users.noreply.github.com>
1 parent e6f9316 commit d75363a

7 files changed

Lines changed: 120 additions & 0 deletions

‎src/Metadata/Resource/Factory/ExtractorResourceMetadataCollectionFactory.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ private function addGraphQlOperations(?array $data, ApiResource $resource): ApiR
177177
}
178178

179179
[$key, $operation] = $this->getOperationWithDefaults($resource, $operation);
180+
if (\array_key_exists($key, $operations)) {
181+
throw new RuntimeException(\sprintf('GraphQL operation name "%s" is declared twice on resource "%s". GraphQL operation names must be unique, set distinct names.', $key, $resource->getClass() ?? ''));
182+
}
180183
$operations[$key] = $operation;
181184
}
182185

‎src/Metadata/Resource/Factory/MetadataCollectionFactoryTrait.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ private function buildResourceOperations(array $metadataCollection, string $reso
117117
}
118118
[$key, $operation] = $this->getOperationWithDefaults($resources[$index], $metadata);
119119
$graphQlOperations = $resources[$index]->getGraphQlOperations();
120+
$this->assertGraphQlOperationNameIsUnique($graphQlOperations ?? [], $key, $resourceClass);
120121
$graphQlOperations[$key] = $operation;
121122
$resources[$index] = $resources[$index]->withGraphQlOperations($graphQlOperations);
122123
continue;
@@ -205,6 +206,7 @@ private function buildResourceOperations(array $metadataCollection, string $reso
205206
$operation = $this->mergeOperationParameters($operation, $parameters);
206207
}
207208

209+
$this->assertGraphQlOperationNameIsUnique($graphQlOperationsWithDefaults, $key, $resourceClass);
208210
$graphQlOperationsWithDefaults[$key] = $operation;
209211
}
210212

@@ -270,6 +272,18 @@ private function deduplicateShortNames(array $resources): array
270272
return $resources;
271273
}
272274

275+
/**
276+
* @param array<string, mixed> $operations
277+
*/
278+
private function assertGraphQlOperationNameIsUnique(array $operations, string $key, string $resourceClass): void
279+
{
280+
if (!\array_key_exists($key, $operations)) {
281+
return;
282+
}
283+
284+
throw new RuntimeException(\sprintf('GraphQL operation name "%s" is declared twice on resource "%s". GraphQL operation names must be unique, set distinct names.', $key, $resourceClass));
285+
}
286+
273287
/**
274288
* Two operations sharing an explicit name silently overwrite each other because
275289
* the operation name is the unique key (and the Symfony route name).

‎src/Metadata/Tests/Extractor/XmlExtractorTest.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,17 @@ public function testDuplicateOperationNameFromXmlThrows(): void
526526
$factory->create(Comment::class);
527527
}
528528

529+
public function testDuplicateGraphQlOperationNameFromXmlThrows(): void
530+
{
531+
$extractor = new XmlResourceExtractor([__DIR__.'/xml/duplicate_graphql_operation_name.xml']);
532+
$factory = new ExtractorResourceMetadataCollectionFactory($extractor, graphQlEnabled: true);
533+
534+
$this->expectException(RuntimeException::class);
535+
$this->expectExceptionMessage(\sprintf('GraphQL operation name "item_query" is declared twice on resource "%s".', Comment::class));
536+
537+
$factory->create(Comment::class);
538+
}
539+
529540
public function testOpenApiParametersAreAList(): void
530541
{
531542
$extractor = new XmlResourceExtractor([__DIR__.'/xml/openapi_parameters.xml']);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<resources xmlns="https://api-platform.com/schema/metadata/resources-3.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
5+
https://api-platform.com/schema/metadata/resources-3.0.xsd">
6+
<resource class="ApiPlatform\Metadata\Tests\Fixtures\ApiResource\Comment">
7+
<graphQlOperations>
8+
<graphQlOperation class="ApiPlatform\Metadata\GraphQl\Query"
9+
name="item_query"
10+
security="is_granted('ROLE_ADMIN')" />
11+
<graphQlOperation class="ApiPlatform\Metadata\GraphQl\Query"
12+
name="item_query" />
13+
</graphQlOperations>
14+
</resource>
15+
</resources>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Metadata\Tests\Fixtures\ApiResource;
15+
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use ApiPlatform\Metadata\GraphQl\Query;
18+
19+
#[Query(name: 'item_query', security: 'is_granted("ROLE_ADMIN")')]
20+
#[Query(name: 'item_query')]
21+
class SameNameGraphQlOperationAttributes
22+
{
23+
#[ApiProperty(identifier: true)]
24+
public ?string $id = null;
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Metadata\Tests\Fixtures\ApiResource;
15+
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\GraphQl\Query;
19+
20+
#[ApiResource(
21+
graphQlOperations: [
22+
new Query(name: 'item_query', security: 'is_granted("ROLE_ADMIN")'),
23+
new Query(name: 'item_query'),
24+
],
25+
)]
26+
class SameNameGraphQlOperations
27+
{
28+
#[ApiProperty(identifier: true)]
29+
public ?string $id = null;
30+
}

‎src/Metadata/Tests/Resource/Factory/AttributesResourceMetadataCollectionFactoryTest.php‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\ResourceClassPropagation;
4545
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\ResourceClassPropagationTarget;
4646
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\SameNameDifferentMethodOperations;
47+
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\SameNameGraphQlOperationAttributes;
48+
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\SameNameGraphQlOperations;
4749
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\WithParameter;
4850
use ApiPlatform\Metadata\Tests\Fixtures\State\AttributeResourceProcessor;
4951
use ApiPlatform\Metadata\Tests\Fixtures\State\AttributeResourceProvider;
@@ -468,4 +470,24 @@ public function testDuplicateOperationNameThrows(): void
468470

469471
$factory->create(SameNameDifferentMethodOperations::class);
470472
}
473+
474+
public function testDuplicateGraphQlOperationNameThrows(): void
475+
{
476+
$factory = new AttributesResourceMetadataCollectionFactory(graphQlEnabled: true);
477+
478+
$this->expectException(RuntimeException::class);
479+
$this->expectExceptionMessage(\sprintf('GraphQL operation name "item_query" is declared twice on resource "%s".', SameNameGraphQlOperations::class));
480+
481+
$factory->create(SameNameGraphQlOperations::class);
482+
}
483+
484+
public function testDuplicateGraphQlOperationNameOnAttributesThrows(): void
485+
{
486+
$factory = new AttributesResourceMetadataCollectionFactory(graphQlEnabled: true);
487+
488+
$this->expectException(RuntimeException::class);
489+
$this->expectExceptionMessage(\sprintf('GraphQL operation name "item_query" is declared twice on resource "%s".', SameNameGraphQlOperationAttributes::class));
490+
491+
$factory->create(SameNameGraphQlOperationAttributes::class);
492+
}
471493
}

0 commit comments

Comments
 (0)