Skip to content

[DEFAULT ORDER] #1246: Support default order for a specific custom operation #3784

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 2 commits into from
Nov 8, 2020
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
112 changes: 112 additions & 0 deletions features/main/default_order.feature
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,115 @@ Feature: Default order
}
}
"""

Scenario: Override custom order asc
When I send a "GET" request to "/custom_collection_asc_foos?itemsPerPage=10"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be equal to:
"""
{
"@context": "/contexts/Foo",
"@id": "/foos",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "/foos/5",
"@type": "Foo",
"id": 5,
"name": "Balbo",
"bar": "Amet"
},
{
"@id": "/foos/3",
"@type": "Foo",
"id": 3,
"name": "Ephesian",
"bar": "Dolor"
},
{
"@id": "/foos/1",
"@type": "Foo",
"id": 1,
"name": "Hawsepipe",
"bar": "Lorem"
},
{
"@id": "/foos/4",
"@type": "Foo",
"id": 4,
"name": "Separativeness",
"bar": "Sit"
},
{
"@id": "/foos/2",
"@type": "Foo",
"id": 2,
"name": "Sthenelus",
"bar": "Ipsum"
}
],
"hydra:totalItems": 5,
"hydra:view": {
"@id": "/custom_collection_asc_foos?itemsPerPage=10",
"@type": "hydra:PartialCollectionView"
}
}
"""

Scenario: Override custom order desc
When I send a "GET" request to "/custom_collection_desc_foos?itemsPerPage=10"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be equal to:
"""
{
"@context": "/contexts/Foo",
"@id": "/foos",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "/foos/2",
"@type": "Foo",
"id": 2,
"name": "Sthenelus",
"bar": "Ipsum"
},
{
"@id": "/foos/4",
"@type": "Foo",
"id": 4,
"name": "Separativeness",
"bar": "Sit"
},
{
"@id": "/foos/1",
"@type": "Foo",
"id": 1,
"name": "Hawsepipe",
"bar": "Lorem"
},
{
"@id": "/foos/3",
"@type": "Foo",
"id": 3,
"name": "Ephesian",
"bar": "Dolor"
},
{
"@id": "/foos/5",
"@type": "Foo",
"id": 5,
"name": "Balbo",
"bar": "Amet"
}
],
"hydra:totalItems": 5,
"hydra:view": {
"@id": "/custom_collection_desc_foos?itemsPerPage=10",
"@type": "hydra:PartialCollectionView"
}
}
"""
8 changes: 6 additions & 2 deletions src/Bridge/Doctrine/MongoDbOdm/Extension/OrderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use ApiPlatform\Core\Bridge\Doctrine\Common\PropertyHelperTrait;
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\PropertyHelperTrait as MongoDbOdmPropertyHelperTrait;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Doctrine\Persistence\ManagerRegistry;

/**
* Applies selected ordering while querying resource collection.
Expand Down Expand Up @@ -53,7 +53,11 @@ public function applyToCollection(Builder $aggregationBuilder, string $resourceC
$classMetaData = $this->getClassMetadata($resourceClass);
$identifiers = $classMetaData->getIdentifier();
if (null !== $this->resourceMetadataFactory) {
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order');
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)
->getCollectionOperationAttribute($operationName, 'order', [], true);
if (empty($defaultOrder)) {
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order');
}
if (\is_array($defaultOrder)) {
foreach ($defaultOrder as $field => $order) {
if (\is_int($field)) {
Expand Down
6 changes: 5 additions & 1 deletion src/Bridge/Doctrine/Orm/Extension/OrderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
$classMetaData = $queryBuilder->getEntityManager()->getClassMetadata($resourceClass);
$identifiers = $classMetaData->getIdentifier();
if (null !== $this->resourceMetadataFactory) {
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order');
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)
->getCollectionOperationAttribute($operationName, 'order', [], true);
if (empty($defaultOrder)) {
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order');
}
if (\is_array($defaultOrder)) {
foreach ($defaultOrder as $field => $order) {
if (\is_int($field)) {
Expand Down
5 changes: 5 additions & 0 deletions tests/Fixtures/TestBundle/Document/Foo.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
* "collection_query"={"pagination_enabled"=false},
* "create",
* "delete"
* },
* collectionOperations={
* "get",
* "get_desc_custom"={"method"="GET", "path"="custom_collection_desc_foos", "order"={"name"="DESC"}},
* "get_asc_custom"={"method"="GET", "path"="custom_collection_asc_foos", "order"={ "name"="ASC"}},
* }
* )
* @ODM\Document
Expand Down
5 changes: 5 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Foo.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
* "collection_query"={"pagination_enabled"=false},
* "create",
* "delete"
* },
* collectionOperations={
* "get",
* "get_desc_custom"={"method"="GET", "path"="custom_collection_desc_foos", "order"={"name"="DESC"}},
* "get_asc_custom"={"method"="GET", "path"="custom_collection_asc_foos", "order"={ "name"="ASC"}},
* }
* )
* @ORM\Entity
Expand Down