-
-
Notifications
You must be signed in to change notification settings - Fork 918
Fix 1246: configure default order on ApiResource annotation #1321
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
Feature: Default order | ||
In order to get a list in a specific order, | ||
As a client software developer, | ||
I need to be able to specify default order. | ||
|
||
@createSchema @dropSchema | ||
Scenario: Override custom order | ||
Given there are 5 foo objects with fake names | ||
When I send a "GET" request to "/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" | ||
}, | ||
{ | ||
"@id": "/foos/4", | ||
"@type": "Foo", | ||
"id": 4, | ||
"name": "Separativeness" | ||
}, | ||
{ | ||
"@id": "/foos/1", | ||
"@type": "Foo", | ||
"id": 1, | ||
"name": "Hawsepipe" | ||
}, | ||
{ | ||
"@id": "/foos/3", | ||
"@type": "Foo", | ||
"id": 3, | ||
"name": "Ephesian" | ||
}, | ||
{ | ||
"@id": "/foos/5", | ||
"@type": "Foo", | ||
"id": 5, | ||
"name": "Balbo" | ||
} | ||
], | ||
"hydra:totalItems": 5, | ||
"hydra:view": { | ||
"@id": "/foos?itemsPerPage=10", | ||
"@type": "hydra:PartialCollectionView" | ||
} | ||
} | ||
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,20 +14,24 @@ | |
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Extension; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
/** | ||
* Applies selected ordering while querying resource collection. | ||
* | ||
* @author Kévin Dunglas <[email protected]> | ||
* @author Samuel ROZE <[email protected]> | ||
* @author Vincent Chalamon <[email protected]> | ||
*/ | ||
final class OrderExtension implements QueryCollectionExtensionInterface | ||
{ | ||
private $order; | ||
private $resourceMetadataFactory; | ||
|
||
public function __construct(string $order = null) | ||
public function __construct(string $order = null, ResourceMetadataFactoryInterface $resourceMetadataFactory = null) | ||
{ | ||
$this->resourceMetadataFactory = $resourceMetadataFactory; | ||
$this->order = $order; | ||
} | ||
|
||
|
@@ -38,6 +42,14 @@ 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'); | ||
if (null !== $defaultOrder) { | ||
$queryBuilder->addOrderBy('o.'.$defaultOrder[0], $defaultOrder[1] ?? 'ASC'); | ||
|
||
return; | ||
} | ||
} | ||
|
||
if (null !== $this->order) { | ||
foreach ($identifiers as $identifier) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
* "my_dummy.order", | ||
* "my_dummy.range", | ||
* "my_dummy.search", | ||
* }, | ||
* } | ||
* }) | ||
* @ORM\Entity | ||
*/ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity; | ||
|
||
use ApiPlatform\Core\Annotation\ApiProperty; | ||
use ApiPlatform\Core\Annotation\ApiResource; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
/** | ||
* Foo. | ||
* | ||
* @author Vincent Chalamon <[email protected]> | ||
* | ||
* @ApiResource(attributes={ | ||
* "order"={"name", "DESC"} | ||
* }) | ||
* @ORM\Entity | ||
*/ | ||
class Foo | ||
{ | ||
/** | ||
* @var int The id | ||
* | ||
* @ORM\Column(type="integer") | ||
* @ORM\Id | ||
* @ORM\GeneratedValue(strategy="AUTO") | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @var string The dummy name | ||
* | ||
* @ORM\Column | ||
* @Assert\NotBlank | ||
* @ApiProperty(iri="http://schema.org/name") | ||
*/ | ||
private $name; | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setName($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I probably have been too fast to merge. Wouldn't be better to allow complex ordering? I suggest the following:
(Order by name then by firstname). WDYT @vincentchalamon? Can you do a follow up PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I though about adding the multiple default ordering, but I wasn't sure it was very useful.
I would prefer a syntax like that:
WDYT @dunglas? If so, I could open another PR to fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO it's useful (ordering by name then firstname is very common for instance). You're right for the syntax, my example is faulty (missing
order
key). A PR would be great!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#1323