Skip to content

Commit 3605e6d

Browse files
authored
[DEFAULT ORDER] #1246: Support default order for a specific custom operation (#3784)
* feat: update order extension * test: update order behat tests
1 parent 4eda8d5 commit 3605e6d

File tree

5 files changed

+132
-2
lines changed

5 files changed

+132
-2
lines changed

features/main/default_order.feature

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,115 @@ Feature: Default order
117117
}
118118
}
119119
"""
120+
121+
Scenario: Override custom order asc
122+
When I send a "GET" request to "/custom_collection_asc_foos?itemsPerPage=10"
123+
Then the response status code should be 200
124+
And the response should be in JSON
125+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
126+
And the JSON should be equal to:
127+
"""
128+
{
129+
"@context": "/contexts/Foo",
130+
"@id": "/foos",
131+
"@type": "hydra:Collection",
132+
"hydra:member": [
133+
{
134+
"@id": "/foos/5",
135+
"@type": "Foo",
136+
"id": 5,
137+
"name": "Balbo",
138+
"bar": "Amet"
139+
},
140+
{
141+
"@id": "/foos/3",
142+
"@type": "Foo",
143+
"id": 3,
144+
"name": "Ephesian",
145+
"bar": "Dolor"
146+
},
147+
{
148+
"@id": "/foos/1",
149+
"@type": "Foo",
150+
"id": 1,
151+
"name": "Hawsepipe",
152+
"bar": "Lorem"
153+
},
154+
{
155+
"@id": "/foos/4",
156+
"@type": "Foo",
157+
"id": 4,
158+
"name": "Separativeness",
159+
"bar": "Sit"
160+
},
161+
{
162+
"@id": "/foos/2",
163+
"@type": "Foo",
164+
"id": 2,
165+
"name": "Sthenelus",
166+
"bar": "Ipsum"
167+
}
168+
],
169+
"hydra:totalItems": 5,
170+
"hydra:view": {
171+
"@id": "/custom_collection_asc_foos?itemsPerPage=10",
172+
"@type": "hydra:PartialCollectionView"
173+
}
174+
}
175+
"""
176+
177+
Scenario: Override custom order desc
178+
When I send a "GET" request to "/custom_collection_desc_foos?itemsPerPage=10"
179+
Then the response status code should be 200
180+
And the response should be in JSON
181+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
182+
And the JSON should be equal to:
183+
"""
184+
{
185+
"@context": "/contexts/Foo",
186+
"@id": "/foos",
187+
"@type": "hydra:Collection",
188+
"hydra:member": [
189+
{
190+
"@id": "/foos/2",
191+
"@type": "Foo",
192+
"id": 2,
193+
"name": "Sthenelus",
194+
"bar": "Ipsum"
195+
},
196+
{
197+
"@id": "/foos/4",
198+
"@type": "Foo",
199+
"id": 4,
200+
"name": "Separativeness",
201+
"bar": "Sit"
202+
},
203+
{
204+
"@id": "/foos/1",
205+
"@type": "Foo",
206+
"id": 1,
207+
"name": "Hawsepipe",
208+
"bar": "Lorem"
209+
},
210+
{
211+
"@id": "/foos/3",
212+
"@type": "Foo",
213+
"id": 3,
214+
"name": "Ephesian",
215+
"bar": "Dolor"
216+
},
217+
{
218+
"@id": "/foos/5",
219+
"@type": "Foo",
220+
"id": 5,
221+
"name": "Balbo",
222+
"bar": "Amet"
223+
}
224+
],
225+
"hydra:totalItems": 5,
226+
"hydra:view": {
227+
"@id": "/custom_collection_desc_foos?itemsPerPage=10",
228+
"@type": "hydra:PartialCollectionView"
229+
}
230+
}
231+
"""

src/Bridge/Doctrine/MongoDbOdm/Extension/OrderExtension.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public function applyToCollection(Builder $aggregationBuilder, string $resourceC
5353
$classMetaData = $this->getClassMetadata($resourceClass);
5454
$identifiers = $classMetaData->getIdentifier();
5555
if (null !== $this->resourceMetadataFactory) {
56-
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order');
56+
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)
57+
->getCollectionOperationAttribute($operationName, 'order', [], true);
58+
if (empty($defaultOrder)) {
59+
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order');
60+
}
5761
if (\is_array($defaultOrder)) {
5862
foreach ($defaultOrder as $field => $order) {
5963
if (\is_int($field)) {

src/Bridge/Doctrine/Orm/Extension/OrderExtension.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
5151
$classMetaData = $queryBuilder->getEntityManager()->getClassMetadata($resourceClass);
5252
$identifiers = $classMetaData->getIdentifier();
5353
if (null !== $this->resourceMetadataFactory) {
54-
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order');
54+
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)
55+
->getCollectionOperationAttribute($operationName, 'order', [], true);
56+
if (empty($defaultOrder)) {
57+
$defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order');
58+
}
5559
if (\is_array($defaultOrder)) {
5660
foreach ($defaultOrder as $field => $order) {
5761
if (\is_int($field)) {

tests/Fixtures/TestBundle/Document/Foo.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
* "collection_query"={"pagination_enabled"=false},
3131
* "create",
3232
* "delete"
33+
* },
34+
* collectionOperations={
35+
* "get",
36+
* "get_desc_custom"={"method"="GET", "path"="custom_collection_desc_foos", "order"={"name"="DESC"}},
37+
* "get_asc_custom"={"method"="GET", "path"="custom_collection_asc_foos", "order"={ "name"="ASC"}},
3338
* }
3439
* )
3540
* @ODM\Document

tests/Fixtures/TestBundle/Entity/Foo.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
* "collection_query"={"pagination_enabled"=false},
3131
* "create",
3232
* "delete"
33+
* },
34+
* collectionOperations={
35+
* "get",
36+
* "get_desc_custom"={"method"="GET", "path"="custom_collection_desc_foos", "order"={"name"="DESC"}},
37+
* "get_asc_custom"={"method"="GET", "path"="custom_collection_asc_foos", "order"={ "name"="ASC"}},
3338
* }
3439
* )
3540
* @ORM\Entity

0 commit comments

Comments
 (0)