`OrderExtension` and `OrderFilter` pass string sort directions, deprecated since `doctrine/orm` 3.7
メンテナーはふだん 1 日以内に返信
評価
調査の方向性
src/Doctrine/Orm/Extension/OrderExtension.php の 72 行目と 85 行目、および src/Doctrine/Orm/Filter/OrderFilter.php の 281 行目と 284 行目から始める。これらでは 'ASC'/'DESC' リテラルが orderBy()/addOrderBy() に渡されている。doctrine/orm >= 3.7 の場合にのみ \SortDirection::Ascending/Descending へマップし(判定は Composer\InstalledVersions または QueryBuilder::addOrderBy() に対するリフレクションによる)、それ以外は文字列を返す小さなヘルパーを導入し、OrderFilter の nulls の扱いを含む 4 すべての呼び出し箇所に適用する。完了の条件は、doctrine/orm 3.7 でのコレクションリクエストで文字列ソートの deprecation が出ないこと、および ORM 2.17/^3.3 で壊れないこと — 既存の Doctrine ORM extension/filter のテストスイートで検証する。
索引モデルが issue の本文から書いたものです。
説明
API Platform affected: 4.3.21, and the current heads of 4.4, 5.0 and main
The doctrine/orm 3.7 deprecates string based sort directions in QueryBuilder::orderBy() and addOrderBy(). It wants the PHP 8.6 \SortDirection enum instead, which ORM itself requires through symfony/polyfill-php86. See "Deprecated using strings or null as sort directions" in https://github.com/doctrine/orm/blob/3.7.x/UPGRADE.md
OrderExtension and OrderFilter still pass 'ASC' and 'DESC' literals:
- https://github.com/api-platform/core/blob/main/src/Doctrine/Orm/Extension/OrderExtension.php#L72
- https://github.com/api-platform/core/blob/main/src/Doctrine/Orm/Extension/OrderExtension.php#L85
- https://github.com/api-platform/core/blob/main/src/Doctrine/Orm/Filter/OrderFilter.php#L281
- https://github.com/api-platform/core/blob/main/src/Doctrine/Orm/Filter/OrderFilter.php#L284
Line 85 matters most. collection.order defaults to ASC, so every Doctrine collection without its own order gets addOrderBy('o.id', 'ASC'). Any app on doctrine/orm 3.7 sees this on its first collection request:
Passing string as $order to Doctrine\ORM\QueryBuilder::addOrderBy() is deprecated, use an instance of SortDirection instead.
(QueryBuilder.php:1518 called by QueryBuilder.php:1254, https://github.com/doctrine/orm/issues/11313, package doctrine/orm)
The message names QueryBuilder.php as the caller, and doctrine/deprecations reports it only once per process, so it took me a while to trace it back to API Platform. We fixed all our own orderBy() calls and it kept coming.
How to reproduce
PHP 8.4, Symfony 8.1, doctrine/orm 3.7.3, deprecations visible (for example PHPUnit with failOnDeprecation). Request any Doctrine-backed GetCollection.
Possible solution
As the API Platform still allows doctrine/orm ^2.17 || ^3.3, passing the enum unconditionally would break older ORM versions. A small helper could map 'ASC'/'DESC' to \SortDirection::Ascending/Descending when the installed ORM is 3.7 or newer, and return the string otherwise. The version check could use i.e. Composer\InstalledVersions, or reflection on the second parameter of QueryBuilder::addOrderBy(). OrderFilter needs the same mapping, including the nulls handling on line 281.
I could prepare such PR if proposed solution fits. Which branch should it target then, 4.4?
- 主要言語
- PHP
- スター
- 2.6k
- フォーク
- 987
- 平均マージ
- 1日 8時間
- マージ済み PR(30日)
- 90
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
api-platform/core のほかの issue
-
DeserializeProvider calls PartialDenormalizationException::getErrors(), deprecated in Symfony 8.1オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
api-platform/core#8650 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
api-platform/core#8649 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
api-platform/core#8647 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
api-platform/core#8612 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 54/100
api-platform/core#8655 ·
メンテナーはふだん 1 日以内に返信
api-platform/core の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
hawk-digital-environments/HAWKI#443 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
crazy-goat/rabbit-stream#799 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
Code Quality
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
Automattic/safe-publish#708 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
WordPress/two-factor#1022 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信