Skip to content

Add nulls_always_first and nulls_always_last to nulls_comparison #4103

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 5 commits into from
Mar 2, 2021
Merged

Add nulls_always_first and nulls_always_last to nulls_comparison #4103

merged 5 commits into from
Mar 2, 2021

Conversation

famoser
Copy link
Contributor

@famoser famoser commented Feb 28, 2021

Q A
Branch? main
Bug fix? no
New feature? yes
Deprecations? no
Tickets (no issue)
License MIT
Doc PR api-platform/docs#1297

Add nulls_always_first and nulls_always_last to nulls_comparison of the OrderFilter. This allows to always sort in null values first or last, independent of the current order.

This is useful for UIs with optional fields; displaying empty fields might not be useful to the user. Sorting these always last, independent of the chosen ordering, is the expected behavior.

Given something like

/**
 * @ApiResource(order={"number": "ASC"})
 * @ApiFilter(
 *     OrderFilter::class,
 *     properties={
 *         "number": {
 *             "nulls_comparison": OrderFilter::NULLS_ALWAYS_LAST
 *         }
 *     }
 * )
 */

will result in an ordering like

1
4
NULL
NULL

@famoser
Copy link
Contributor Author

famoser commented Feb 28, 2021

Pinging the maintainers as described in CONTRIBUTING.md (only those listed as one of the authors of the modified files):
@dunglas
@theofidry

Notes:

  • The failing unit test seems to be unrelated to this patch
  • There might be a unit tests missing for mongoDB: ApiPlatform\Core\Tests\Bridge\Doctrine\MongoDbOdm\Filter\OrderFilterTest; although no unit test seems to fail because of it.
  • Do I need to / am allowed to add myself as one of the authors of the modified files?

I am actually using this functionality in a "real-world" project (https://github.com/mangelio/web/), so in my opinion, there exists an actual use case. Thanks for considering to integrate it into the core.

Comment on lines 106 to 110
if (\in_array($nullsComparison, [self::NULLS_ALWAYS_FIRST, self::NULLS_ALWAYS_LAST], true)) {
$nullsDirection = self::NULLS_ALWAYS_FIRST === $nullsComparison ? 'ASC' : 'DESC';
} else {
$nullsDirection = self::NULLS_DIRECTION_MAP[$nullsComparison][$direction];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be simpler if you reused the direction map instead (clearer and no need to add specific code).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍🏻

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to try to fix the MongoDB tests too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I seem to have succeeded. See comment below.

@alanpoulain
Copy link
Member

Hello, thank you for this PR!
The use case seems legit to me too.
I don't know why but the MongoDB unit tests are not executed anymore, but I think they are failing. I will fix it.
I don't think you need to add yourself as the author. Generally, it's done when the file is created for the first time, or when a lot of code is changed.

@alanpoulain
Copy link
Member

Rebased to show the MongoDB issue.

@famoser
Copy link
Contributor Author

famoser commented Mar 1, 2021

The MongoDB tests are also passing now.

I have to be honest: I am not sure I understood the MongoDB tests specification 100%. Could you please quickly glance over it for a plausibility check?

@alanpoulain alanpoulain merged commit a3f3d7c into api-platform:main Mar 2, 2021
@alanpoulain
Copy link
Member

Nice work! Thank you @famoser.

@famoser famoser deleted the add-always-first-last-order branch March 2, 2021 09:43
@famoser famoser restored the add-always-first-last-order branch March 2, 2021 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants