Skip to content

[API] Enforce pk ordering for API endpoints#11446

Merged
SchrodingersGat merged 7 commits intoinventree:masterfrom
SchrodingersGat:api-ordering
Mar 2, 2026
Merged

[API] Enforce pk ordering for API endpoints#11446
SchrodingersGat merged 7 commits intoinventree:masterfrom
SchrodingersGat:api-ordering

Conversation

@SchrodingersGat
Copy link
Member

@SchrodingersGat SchrodingersGat commented Mar 2, 2026

References

@SchrodingersGat SchrodingersGat added this to the 1.3.0 milestone Mar 2, 2026
@SchrodingersGat SchrodingersGat added bug Identifies a bug which needs to be addressed api Relates to the API backport Apply this label to a PR to enable auto-backport action backport-to-1.2.x labels Mar 2, 2026
@netlify
Copy link

netlify bot commented Mar 2, 2026

Deploy Preview for inventree-web-pui-preview canceled.

Name Link
🔨 Latest commit bee91ac
🔍 Latest deploy log https://app.netlify.com/projects/inventree-web-pui-preview/deploys/69a56ed8dbda1f00080181ad

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses unstable ordering in paginated API list endpoints by ensuring a deterministic ordering that avoids duplicates / dropped items across pagination boundaries (ref #11442).

Changes:

  • Updates the custom DRF ordering backend to enforce a stable ordering by appending a tie-breaker field.
  • Replaces aliased ordering filter backend constants across multiple API modules to consistently use the custom ordering backend.
  • Adds a regression test covering pagination stability for the stock list endpoint.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/backend/InvenTree/stock/test_api.py Adds a pagination regression test for /api/stock/ to detect duplicate PKs across pages.
src/backend/InvenTree/stock/api.py Switches list endpoints to the updated ordering backend constant.
src/backend/InvenTree/part/api.py Switches list endpoints to the updated ordering backend constant.
src/backend/InvenTree/order/api.py Switches list endpoints to the updated ordering backend constant and simplifies imports.
src/backend/InvenTree/company/api.py Switches list endpoints to the updated ordering backend constant and removes alias import.
src/backend/InvenTree/common/api.py Switches list endpoints to the updated ordering backend constant and simplifies imports.
src/backend/InvenTree/build/api.py Switches list endpoints to the updated ordering backend constant.
src/backend/InvenTree/InvenTree/filters.py Implements stable ordering behavior in InvenTreeOrderingFilter and consolidates filter backend constants.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 2, 2026

Merging this PR will degrade performance by 14.5%

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

❌ 33 regressed benchmarks
✅ 11 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_api_list_performance[/api/part/] 74.2 ms 79.5 ms -6.6%
WallTime test_api_list_performance[/api/stock/] 160 ms 176.1 ms -9.15%
WallTime test_api_list_performance[/api/stock/location/] 20 ms 22.8 ms -12.31%
WallTime test_api_options_performance[/api/parameter/template/] 8.9 ms 9.6 ms -7.71%
WallTime test_api_options_performance[/api/parameter/] 12.7 ms 14.3 ms -11.35%
WallTime test_api_list_performance[/api/part/category/] 16.6 ms 19.3 ms -14.14%
WallTime test_api_options_performance[/api/order/po-line/] 160.4 ms 173.8 ms -7.72%
WallTime test_api_list_performance[/api/build/] 42 ms 47.8 ms -12.18%
WallTime test_api_options_performance[/api/user/roles/] 6.3 ms 6.8 ms -7.49%
WallTime test_search_performance[supplierpart] 39.2 ms 42.6 ms -8.12%
WallTime test_search_performance[all] 359 ms 392.9 ms -8.64%
WallTime test_api_list_performance[/api/build/item/] 332.3 ms 367.6 ms -9.61%
WallTime test_api_list_performance[/api/user/roles/] 8.8 ms 9.9 ms -10.88%
WallTime test_search_performance[stocklocation] 17.5 ms 18.7 ms -6.6%
WallTime test_search_performance[build] 34.5 ms 37.8 ms -8.73%
WallTime test_api_list_performance[/api/order/so/] 45.9 ms 50.8 ms -9.65%
WallTime test_api_options_performance[/api/part/category/] 14.4 ms 15.9 ms -9.81%
WallTime test_api_list_performance[/api/order/so/shipment/] 22.1 ms 24.5 ms -9.69%
WallTime test_api_list_performance[/api/parameter/] 11.5 ms 13 ms -11.23%
WallTime test_api_options_performance[/api/stock/location/] 24 ms 27.3 ms -12.13%
... ... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.


Comparing SchrodingersGat:api-ordering (bee91ac) with master (2c67454)

Open in CodSpeed

@codecov
Copy link

codecov bot commented Mar 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.03%. Comparing base (2c67454) to head (bee91ac).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #11446   +/-   ##
=======================================
  Coverage   88.02%   88.03%           
=======================================
  Files        1296     1296           
  Lines       59003    59038   +35     
  Branches     1938     1938           
=======================================
+ Hits        51940    51974   +34     
- Misses       6581     6582    +1     
  Partials      482      482           
Flag Coverage Δ
backend 89.23% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Backend Apps 91.72% <100.00%> (+<0.01%) ⬆️
Backend General 93.37% <100.00%> (ø)
Frontend 70.81% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@SchrodingersGat SchrodingersGat merged commit 3bbdddf into inventree:master Mar 2, 2026
38 checks passed
@SchrodingersGat SchrodingersGat deleted the api-ordering branch March 2, 2026 11:45
github-actions bot pushed a commit that referenced this pull request Mar 2, 2026
* Add unit test to detect unreliable pagination

* Enforce PK field ordering

- Append 'pk' ordering to InvenTreeOrderingFilter

* Use our ordering filter everywhere

* Simplify ordering options

* Enforce list

* Use last term for ordering checks

* Individual delete to fix mysql issue

(cherry picked from commit 3bbdddf)
@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2026

💚 All backports created successfully

Status Branch Result
1.2.x

Questions ?

Please refer to the Backport tool documentation and see the Github Action logs for details

SchrodingersGat added a commit that referenced this pull request Mar 2, 2026
* Add unit test to detect unreliable pagination

* Enforce PK field ordering

- Append 'pk' ordering to InvenTreeOrderingFilter

* Use our ordering filter everywhere

* Simplify ordering options

* Enforce list

* Use last term for ordering checks

* Individual delete to fix mysql issue

(cherry picked from commit 3bbdddf)

Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Relates to the API backport Apply this label to a PR to enable auto-backport action backport-to-1.2.x bug Identifies a bug which needs to be addressed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Paginated API returns duplicate items and silently drops others due to missing stable sort order

2 participants