Skip to content

Commit ea4446d

Browse files
lukaslueckedunglas
authored andcommitted
Check item resource class in mutation (#2441)
This prevents passing IRIs belonging to different resource classes
1 parent 5b074dc commit ea4446d

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"symfony/twig-bundle": "^3.1 || ^4.0",
6767
"symfony/validator": "^3.3 || ^4.0",
6868
"symfony/yaml": "^3.3 || ^4.0",
69-
"webonyx/graphql-php": ">=0.12 <1.0"
69+
"webonyx/graphql-php": "^0.12"
7070
},
7171
"conflict": {
7272
"symfony/dependency-injection": "<3.3"

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
@dropSchema
126141
Scenario: Delete an item with composite identifiers through a mutation
127142
Given there are Composite identifier objects

src/GraphQl/Resolver/Factory/ItemMutationResolverFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2323
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
2424
use ApiPlatform\Core\Security\ResourceAccessCheckerInterface;
25+
use ApiPlatform\Core\Util\ClassInfoTrait;
2526
use ApiPlatform\Core\Validator\Exception\ValidationException;
2627
use ApiPlatform\Core\Validator\ValidatorInterface;
2728
use GraphQL\Error\Error;
@@ -38,6 +39,7 @@
3839
*/
3940
final class ItemMutationResolverFactory implements ResolverFactoryInterface
4041
{
42+
use ClassInfoTrait;
4143
use ResourceAccessCheckerTrait;
4244

4345
private $iriConverter;
@@ -81,6 +83,10 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
8183
} catch (ItemNotFoundException $e) {
8284
throw Error::createLocatedError(sprintf('Item "%s" not found.', $args['input']['id']), $info->fieldNodes, $info->path);
8385
}
86+
87+
if ($resourceClass !== $this->getObjectClass($item)) {
88+
throw Error::createLocatedError(sprintf('Item "%s" did not match expected type "%s".', $args['input']['id'], $resourceClass), $info->fieldNodes, $info->path);
89+
}
8490
}
8591

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

0 commit comments

Comments
 (0)