Skip to content

Fix #944 - allows to filter by relation by keeping EagerLoading enabled #950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2017
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
75 changes: 69 additions & 6 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeLabel;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCarColor;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyFriend;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FileConfigDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedToDummyFriend;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelationEmbedder;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\UuidIdentifierDummy;
use Behat\Behat\Context\Context;
Expand Down Expand Up @@ -135,18 +139,21 @@ public function thereIsDummyObjectsWithRelatedDummy($nb)
}

/**
* @Given there is :nb dummy objects with relatedDummies
* @Given there is :nb dummy objects having each :nbrelated relatedDummies
*/
public function thereIsDummyObjectsWithRelatedDummies($nb)
public function thereIsDummyObjectsWithRelatedDummies($nb, $nbrelated)
{
for ($i = 1; $i <= $nb; ++$i) {
$relatedDummy = new RelatedDummy();
$relatedDummy->setName('RelatedDummy #'.$i);

$dummy = new Dummy();
$dummy->setName('Dummy #'.$i);
$dummy->setAlias('Alias #'.($nb - $i));
$dummy->addRelatedDummy($relatedDummy);

for ($j = 1; $j <= $nbrelated; ++$j) {
$relatedDummy = new RelatedDummy();
$relatedDummy->setName('RelatedDummy'.$j.$i);
$this->manager->persist($relatedDummy);
$dummy->addRelatedDummy($relatedDummy);
}

$this->manager->persist($relatedDummy);
$this->manager->persist($dummy);
Expand Down Expand Up @@ -319,4 +326,60 @@ public function thereIsAFileConfigDummyObject()
$this->manager->persist($fileConfigDummy);
$this->manager->flush();
}

/**
* @Given there is a DummyCar entity with related colors
*/
public function thereIsAFooEntityWithRelatedBars()
{
$foo = new DummyCar();
$this->manager->persist($foo);

$bar1 = new DummyCarColor();
$bar1->setProp('red');
$bar1->setCar($foo);
$this->manager->persist($bar1);

$bar2 = new DummyCarColor();
$bar2->setProp('blue');
$bar2->setCar($foo);
$this->manager->persist($bar2);

$foo->setColors([$bar1, $bar2]);
$this->manager->persist($foo);

$this->manager->flush();
}

/**
* @Given there is a RelatedDummy with :nb friends
*/
public function thereIsARelatedDummyWithFriends($nb)
{
$relatedDummy = new RelatedDummy();
$relatedDummy->setName('RelatedDummy with friends');
$this->manager->persist($relatedDummy);

for ($i = 1; $i <= $nb; ++$i) {
$friend = new DummyFriend();
$friend->setName('Friend-'.$i);

$this->manager->persist($friend);

$relation = new RelatedToDummyFriend();
$relation->setName('Relation-'.$i);
$relation->setDummyFriend($friend);
$relation->setRelatedDummy($relatedDummy);

$relatedDummy->addRelatedToDummyFriend($relation);

$this->manager->persist($relation);
}

$relatedDummy2 = new RelatedDummy();
$relatedDummy2->setName('RelatedDummy without friends');
$this->manager->persist($relatedDummy2);

$this->manager->flush();
}
}
14 changes: 10 additions & 4 deletions features/doctrine/date_filter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -395,18 +395,18 @@ Feature: Date filter on collections
And the JSON should be equal to:
"""
{
"@context": "\/contexts\/Dummy",
"@id": "\/dummies",
"@context": "/contexts/Dummy",
"@id": "/dummies",
"@type": "hydra:Collection",
"hydra:member": [],
"hydra:totalItems": 0,
"hydra:view": {
"@id": "\/dummies?relatedDummy.dummyDate%5Bafter%5D=2015-04-28",
"@id": "/dummies?relatedDummy.dummyDate%5Bafter%5D=2015-04-28",
"@type": "hydra:PartialCollectionView"
},
"hydra:search": {
"@type": "hydra:IriTemplate",
"hydra:template": "\/dummies{?id,id[],name,alias,description,relatedDummy.name,relatedDummy.name[],relatedDummies,relatedDummies[],dummy,order[id],order[name],order[relatedDummy.symfony],dummyDate[before],dummyDate[after],relatedDummy.dummyDate[before],relatedDummy.dummyDate[after],dummyFloat[between],dummyFloat[gt],dummyFloat[gte],dummyFloat[lt],dummyFloat[lte],dummyPrice[between],dummyPrice[gt],dummyPrice[gte],dummyPrice[lt],dummyPrice[lte],dummyBoolean,dummyFloat,dummyPrice}",
"hydra:template": "/dummies{?id,id[],name,alias,description,relatedDummy.name,relatedDummy.name[],relatedDummies,relatedDummies[],dummy,relatedDummies.name,order[id],order[name],order[relatedDummy.symfony],dummyDate[before],dummyDate[after],relatedDummy.dummyDate[before],relatedDummy.dummyDate[after],dummyFloat[between],dummyFloat[gt],dummyFloat[gte],dummyFloat[lt],dummyFloat[lte],dummyPrice[between],dummyPrice[gt],dummyPrice[gte],dummyPrice[lt],dummyPrice[lte],dummyBoolean,dummyFloat,dummyPrice}",
"hydra:variableRepresentation": "BasicRepresentation",
"hydra:mapping": [
{
Expand Down Expand Up @@ -469,6 +469,12 @@ Feature: Date filter on collections
"property": "dummy",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "relatedDummies.name",
"property": "relatedDummies.name",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "order[id]",
Expand Down
75 changes: 74 additions & 1 deletion features/doctrine/search_filter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,66 @@ Feature: Search filter on collections
I need to search for collections properties

@createSchema
@dropSchema
Scenario: Test ManyToMany with filter on join table
Given there is a RelatedDummy with 4 friends
When I add "Accept" header equal to "application/hal+json"
And I send a "GET" request to "/related_dummies?relatedToDummyFriend.dummyFriend=/dummy_friends/4"
Then the response status code should be 200
And the JSON node "_embedded.item" should have 1 element
And the JSON node "_embedded.item[0]._links.relatedToDummyFriend" should have 4 elements
And the JSON node "_embedded.item[0]._embedded.relatedToDummyFriend" should have 4 elements

@createSchema
Scenario: Test #944
Given there is a DummyCar entity with related colors
When I send a "GET" request to "/dummy_cars?colors.prop=red"
Then the response status code should be 200
And the JSON should be equal to:
"""
{
"@context": "/contexts/DummyCar",
"@id": "/dummy_cars",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "/dummy_cars/1",
"@type": "DummyCar",
"colors": [
{
"@id": "/dummy_car_colors/1",
"@type": "DummyCarColor",
"prop": "red"
},
{
"@id": "/dummy_car_colors/2",
"@type": "DummyCarColor",
"prop": "blue"
}
]
}
],
"hydra:totalItems": 1,
"hydra:view": {
"@id": "/dummy_cars?colors.prop=red",
"@type": "hydra:PartialCollectionView"
},
"hydra:search": {
"@type": "hydra:IriTemplate",
"hydra:template": "/dummy_cars{?colors.prop}",
"hydra:variableRepresentation": "BasicRepresentation",
"hydra:mapping": [
{
"@type": "IriTemplateMapping",
"variable": "colors.prop",
"property": "colors.prop",
"required": false
}
]
}
}
"""

Scenario: Search collection by name (partial)
Given there is "30" dummy objects
When I send a "GET" request to "/dummies?name=my"
Expand Down Expand Up @@ -45,7 +105,6 @@ Feature: Search filter on collections
"""

Scenario: Search collection by name (partial case insensitive)
Given there is "30" dummy objects
When I send a "GET" request to "/dummies?dummy=somedummytest1"
Then the response status code should be 200
And the response should be in JSON
Expand Down Expand Up @@ -179,3 +238,17 @@ Feature: Search filter on collections
}
}
"""

@createSchema
@dropSchema
Scenario: Search related collection by name
Given there is 3 dummy objects having each 3 relatedDummies
When I add "Accept" header equal to "application/hal+json"
And I send a "GET" request to "/dummies?relatedDummies.name=RelatedDummy1"
Then the response status code should be 200
And the response should be in JSON
And the JSON node "_embedded.item" should have 3 elements
And the JSON node "_embedded.item[0]._links.relatedDummies" should have 3 elements
And the JSON node "_embedded.item[1]._links.relatedDummies" should have 3 elements
And the JSON node "_embedded.item[2]._links.relatedDummies" should have 3 elements

8 changes: 7 additions & 1 deletion features/main/crud.feature
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Feature: Create-Retrieve-Update-Delete
"hydra:totalItems": 1,
"hydra:search": {
"@type": "hydra:IriTemplate",
"hydra:template": "/dummies{?id,id[],name,alias,description,relatedDummy.name,relatedDummy.name[],relatedDummies,relatedDummies[],dummy,order[id],order[name],order[relatedDummy.symfony],dummyDate[before],dummyDate[after],relatedDummy.dummyDate[before],relatedDummy.dummyDate[after],dummyFloat[between],dummyFloat[gt],dummyFloat[gte],dummyFloat[lt],dummyFloat[lte],dummyPrice[between],dummyPrice[gt],dummyPrice[gte],dummyPrice[lt],dummyPrice[lte],dummyBoolean,dummyFloat,dummyPrice}",
"hydra:template": "/dummies{?id,id[],name,alias,description,relatedDummy.name,relatedDummy.name[],relatedDummies,relatedDummies[],dummy,relatedDummies.name,order[id],order[name],order[relatedDummy.symfony],dummyDate[before],dummyDate[after],relatedDummy.dummyDate[before],relatedDummy.dummyDate[after],dummyFloat[between],dummyFloat[gt],dummyFloat[gte],dummyFloat[lt],dummyFloat[lte],dummyPrice[between],dummyPrice[gt],dummyPrice[gte],dummyPrice[lt],dummyPrice[lte],dummyBoolean,dummyFloat,dummyPrice}",
"hydra:variableRepresentation": "BasicRepresentation",
"hydra:mapping": [
{
Expand Down Expand Up @@ -186,6 +186,12 @@ Feature: Create-Retrieve-Update-Delete
"property": "dummy",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "relatedDummies.name",
"property": "relatedDummies.name",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "order[id]",
Expand Down
6 changes: 3 additions & 3 deletions features/main/relation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ Feature: Relations support
"@context": "/contexts/RelatedDummy",
"@id": "/related_dummies/1",
"@type": "https://schema.org/Product",
"id": 1,
"name": null,
"symfony": "symfony",
"dummyDate": null,
"thirdLevel": "/third_levels/1",
"relatedToDummyFriend": null,
"relatedToDummyFriend": [],
"dummyBoolean": null,
"id": 1,
"symfony": "symfony",
"age": null
}
"""
Expand Down
Loading