-
-
Notifications
You must be signed in to change notification settings - Fork 920
Improve the ExistsFilter behavior (to cohabit with the SearchFilter) #2243
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
Improve the ExistsFilter behavior (to cohabit with the SearchFilter) #2243
Conversation
ExistsFilter
behavior (to cohabit with the SearchFilter
)
ExistsFilter
behavior (to cohabit with the SearchFilter
)acc3675
to
2c206b8
Compare
Why |
You need to make sure the previous way of using the exists filter is still working, but deprecate it. |
@@ -30,11 +34,24 @@ | |||
* Request: GET /products?brand[exists] | |||
* Interpretation: filter products which have a brand | |||
* | |||
* @author Teoh Han Hui <teohhanhui@gmail.com> | |||
* @author Julien Verger <julien.verger@gmail.com> |
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.
Please add you after the previous author.
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.
done.
/** | ||
* @param RequestStack|null $requestStack No prefix to prevent autowiring of this deprecated property | ||
*/ | ||
public function __construct(ManagerRegistry $managerRegistry, $requestStack = null, string $existsParameterName = 'exists', LoggerInterface $logger = null, array $properties = null) |
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.
Add $existsParameterName
after $logger
to prevent BC.
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.
And you can reuse the existing constant: $existsParameterName = self::QUERY_PARAMETER_KEY
*/ | ||
class ExistsFilter extends AbstractContextAwareFilter | ||
{ | ||
const QUERY_PARAMETER_KEY = 'exists'; |
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.
Should be kept (or it's a BC break)
/** | ||
* @param RequestStack|null $requestStack No prefix to prevent autowiring of this deprecated property | ||
*/ | ||
public function __construct(ManagerRegistry $managerRegistry, $requestStack = null, string $existsParameterName = 'exists', LoggerInterface $logger = null, array $properties = null) |
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.
And you can reuse the existing constant: $existsParameterName = self::QUERY_PARAMETER_KEY
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null) | ||
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = []) | ||
{ | ||
if (isset($context['filters']) && !isset($context['filters'][$this->existsParameterName])) { |
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.
the first isset
is useless and can be removed.
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.
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.
got it
return; | ||
} | ||
|
||
if (!isset($context['filters'][$this->existsParameterName]) || !\is_array($context['filters'][$this->existsParameterName])) { |
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.
the first isset can be removed (you already tested the existence of the key previously), and you can then merge both if
:
if (!\is_array($context['filters'][$this->existsParameterName] ?? null)) {
|
||
$this->assertEquals([ | ||
'description[exists]' => [ |
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.
We need to keep this test to be sure that the old behavior still work. You should use a PHPUnit's data provider to test both behaviors instead.
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 kept the test and create a new one.
It will be a nice improvement, thanks for working on this! |
@alan Yes I keep the same logic (behavior and naming) as the order filter. |
*/ | ||
class ExistsFilter extends AbstractContextAwareFilter | ||
final class ExistsFilter extends AbstractContextAwareFilter |
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.
you can only add a @final
annotation, this is a BC break even if it was forgotten in the first place, people might have extended this class
https://symfony.com/doc/current/contributing/code/bc.html#changing-classes
5b13d41
to
09c0715
Compare
190a979
to
3ee15ff
Compare
3ee15ff
to
671e4d6
Compare
671e4d6
to
8239fe3
Compare
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.
@alanpoulain @dunglas Concerning this PR, I set in standby the development due to lack of time and unable to fix the behat tests.
Would you take it from here (to follow and close) ?
|
||
$this->assertEquals([ | ||
'description[exists]' => [ |
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 kept the test and create a new one.
@epourail I am planning to finish your PR. I am working on a PR and it takes a lot of time. That's why I can't right now. If anyone is willing to take it before me go ahead. |
092fb40
to
fd548d6
Compare
This PR has been rebased, tests are fixed. |
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.
Looks really good, nice that this is still supporting the old syntax!
fd548d6
to
3110c9b
Compare
Thank you @epourail! Nice one. |
We've come full circle then! When I did the And then this happened: #906 (comment) 🙈 |
Remove a BC break introduced by #2243
Improve the behavior of the
ExistsFilter
filter to be declared according to the following syntax:/dummies?exists[<property>]=true/false
dummies(exists: {<property>: "true/false"}) { ... }
The name of the query parameter is controlled via the bundle configuration:
It allows to use an
ExistsFilter
and aSearchFilter
on the same property via the GraphQL endpoint.