Skip to content

Commit

Permalink
fix(graphql): allow search filter to use an int for its value (api-pl…
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa authored and LoicBoursin committed Aug 18, 2021
1 parent 100ba69 commit d89fae6
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 7 deletions.
32 changes: 31 additions & 1 deletion features/graphql/filters.feature
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,36 @@ Feature: Collections filtering
Then the JSON node "data.dummies.edges" should have 1 element
And the JSON node "data.dummies.edges[0].node.id" should be equal to "/dummies/2"

@createSchema
Scenario: Retrieve a collection filtered using the search filter with an int
Given there are 4 dummy objects having each 3 relatedDummies
When I send the following GraphQL request:
"""
{
dummies(name: "Dummy #1") {
totalCount
edges {
node {
name
relatedDummies(age: 31) {
totalCount
edges {
node {
id
name
age
}
}
}
}
}
}
}
"""
Then the JSON node "data.dummies.totalCount" should be equal to 1
And the JSON node "data.dummies.edges[0].node.relatedDummies.totalCount" should be equal to 1
And the JSON node "data.dummies.edges[0].node.relatedDummies.edges[0].node.age" should be equal to "31"

@createSchema
Scenario: Retrieve a collection filtered using the search filter and a name converter
Given there are 10 dummy objects
Expand Down Expand Up @@ -130,7 +160,7 @@ Feature: Collections filtering
And the JSON node "data.convertedOwners.edges[1].node.name_converted.name_converted" should be equal to "Converted 20"

@createSchema
Scenario: Retrieve a collection filtered using the search filter
Scenario: Retrieve a nested collection filtered using the search filter
Given there are 3 dummy objects having each 3 relatedDummies
When I send the following GraphQL request:
"""
Expand Down
28 changes: 26 additions & 2 deletions features/main/subresource.feature
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Feature: Subresource support
"hydra:totalItems": 2,
"hydra:search": {
"@type": "hydra:IriTemplate",
"hydra:template": "/dummies/1/related_dummies{?relatedToDummyFriend.dummyFriend,relatedToDummyFriend.dummyFriend[],name}",
"hydra:template": "/dummies/1/related_dummies{?relatedToDummyFriend.dummyFriend,relatedToDummyFriend.dummyFriend[],name,age,age[]}",
"hydra:variableRepresentation": "BasicRepresentation",
"hydra:mapping": [
{
Expand All @@ -124,6 +124,18 @@ Feature: Subresource support
"variable": "name",
"property": "name",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "age",
"property": "age",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "age[]",
"property": "age",
"required": false
}
]
}
Expand Down Expand Up @@ -167,7 +179,7 @@ Feature: Subresource support
},
"hydra:search": {
"@type": "hydra:IriTemplate",
"hydra:template": "/dummies/1/related_dummies{?relatedToDummyFriend.dummyFriend,relatedToDummyFriend.dummyFriend[],name}",
"hydra:template": "/dummies/1/related_dummies{?relatedToDummyFriend.dummyFriend,relatedToDummyFriend.dummyFriend[],name,age,age[]}",
"hydra:variableRepresentation": "BasicRepresentation",
"hydra:mapping": [
{
Expand All @@ -187,6 +199,18 @@ Feature: Subresource support
"variable": "name",
"property": "name",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "age",
"property": "age",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "age[]",
"property": "age",
"required": false
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Doctrine/Common/Filter/SearchFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected function getIdFromValue(string $value)
protected function normalizeValues(array $values, string $property): ?array
{
foreach ($values as $key => $value) {
if (!\is_int($key) || !\is_string($value)) {
if (!\is_int($key) || !(\is_string($value) || \is_int($value))) {
unset($values[$key]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Behat/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ public function thereAreDummyObjectsWithRelatedDummies(int $nb, int $nbrelated)
for ($j = 1; $j <= $nbrelated; ++$j) {
$relatedDummy = $this->buildRelatedDummy();
$relatedDummy->setName('RelatedDummy'.$j.$i);

$relatedDummy->setAge((int) ($j.$i));
$this->manager->persist($relatedDummy);

$dummy->addRelatedDummy($relatedDummy);
Expand Down
8 changes: 8 additions & 0 deletions tests/Bridge/Doctrine/Common/Filter/SearchFilterTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,14 @@ private function provideApplyTestArguments(): array
'relatedDummy.symfony' => [],
],
],
'integer value' => [
[
'age' => 'exact',
],
[
'age' => 46,
],
],
];
}
}
15 changes: 15 additions & 0 deletions tests/Bridge/Doctrine/MongoDbOdm/Filter/SearchFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,21 @@ public function provideApplyTestData(): array
[],
$filterFactory,
],
'integer value' => [
[
[
'$match' => [
'age' => [
'$in' => [
'46',
],
],
],
],
],
$filterFactory,
RelatedDummy::class,
],
]
);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/Bridge/Doctrine/Orm/Filter/SearchFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,12 @@ public function provideApplyTestData(): array
[],
$filterFactory,
],
'integer value' => [
sprintf('SELECT %s FROM %s %1$s WHERE %1$s.age = :age_p1', $this->alias, RelatedDummy::class),
['age_p1' => 46],
$filterFactory,
RelatedDummy::class,
],
]
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/app/config/config_mongodb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ services:
tags: [ { name: 'api_platform.filter', id: 'my_dummy.mongodb.search' } ]
app.related_dummy_resource.mongodb.search_filter:
parent: 'api_platform.doctrine_mongodb.odm.search_filter'
arguments: [ { 'relatedToDummyFriend.dummyFriend': 'exact', 'name': 'partial' } ]
arguments: [ { 'relatedToDummyFriend.dummyFriend': 'exact', 'name': 'partial', 'age': 'exact' } ]
tags: [ { name: 'api_platform.filter', id: 'related_dummy.mongodb.friends' } ]
app.my_dummy_date_resource.mongodb.date_filter:
parent: 'api_platform.doctrine_mongodb.odm.date_filter'
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/app/config/config_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ services:

app.related_dummy_resource.search_filter:
parent: 'api_platform.doctrine.orm.search_filter'
arguments: [ { 'relatedToDummyFriend.dummyFriend': 'exact', 'name': 'partial' } ]
arguments: [ { 'relatedToDummyFriend.dummyFriend': 'exact', 'name': 'partial', 'age': 'exact' } ]
tags: [ { name: 'api_platform.filter', id: 'related_dummy.friends' } ]

app.related_dummy_resource.complex_sub_query_filter:
Expand Down

0 comments on commit d89fae6

Please sign in to comment.