Skip to content

Commit b14e1b2

Browse files
lukaslueckealanpoulain
authored andcommitted
Check item resource class in mutation (#2441)
This prevents passing IRIs belonging to different resource classes
1 parent 78cd201 commit b14e1b2

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

features/graphql/mutation.feature

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,21 @@ Feature: GraphQL mutation support
122122
And the JSON node "data.deleteFoo.id" should be equal to "/foos/1"
123123
And the JSON node "data.deleteFoo.clientMutationId" should be equal to "anotherId"
124124

125+
Scenario: Trigger an error trying to delete item of different resource
126+
When I send the following GraphQL request:
127+
"""
128+
mutation {
129+
deleteFoo(input: {id: "/dummies/1", clientMutationId: "myId"}) {
130+
id
131+
clientMutationId
132+
}
133+
}
134+
"""
135+
Then the response status code should be 200
136+
And the response should be in JSON
137+
And the header "Content-Type" should be equal to "application/json"
138+
And the JSON node "errors[0].message" should be equal to 'Item "/dummies/1" did not match expected type "ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Foo".'
139+
125140
Scenario: Delete an item with composite identifiers through a mutation
126141
Given there are Composite identifier objects
127142
When I send the following GraphQL request:

src/GraphQl/Resolver/Factory/ItemMutationResolverFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2424
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
2525
use ApiPlatform\Core\Security\ResourceAccessCheckerInterface;
26+
use ApiPlatform\Core\Util\ClassInfoTrait;
2627
use ApiPlatform\Core\Validator\Exception\ValidationException;
2728
use ApiPlatform\Core\Validator\ValidatorInterface;
2829
use GraphQL\Error\Error;
@@ -39,6 +40,7 @@
3940
*/
4041
final class ItemMutationResolverFactory implements ResolverFactoryInterface
4142
{
43+
use ClassInfoTrait;
4244
use FieldsToAttributesTrait;
4345
use ResourceAccessCheckerTrait;
4446

@@ -83,6 +85,10 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
8385
} catch (ItemNotFoundException $e) {
8486
throw Error::createLocatedError(sprintf('Item "%s" not found.', $args['input']['id']), $info->fieldNodes, $info->path);
8587
}
88+
89+
if ($resourceClass !== $this->getObjectClass($item)) {
90+
throw Error::createLocatedError(sprintf('Item "%s" did not match expected type "%s".', $args['input']['id'], $resourceClass), $info->fieldNodes, $info->path);
91+
}
8692
}
8793

8894
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);

0 commit comments

Comments
 (0)