fix(api): handle None values in /dags/{dag_id}/tasks order_by parameter#63991
Open
tysoncung wants to merge 1 commit intoapache:mainfrom
Open
fix(api): handle None values in /dags/{dag_id}/tasks order_by parameter#63991tysoncung wants to merge 1 commit intoapache:mainfrom
tysoncung wants to merge 1 commit intoapache:mainfrom
Conversation
…er (apache#63927) When sorting tasks by an attribute that may contain None values (e.g., start_date), Python raises TypeError because None cannot be compared with '<'. This fix uses a sort key that places None values at the end of the sorted result, regardless of sort direction. Closes apache#63927
Contributor
|
Thanks for your PR! Can you add the corresponding tests? |
Contributor
SameerMesiah97
left a comment
There was a problem hiding this comment.
Logic is fine but I'd reassess whether the fallback is needed. Please add a test for this as well.
There are CI failures but they look unrelated.
| def _sort_key(task): | ||
| val = attrgetter(attr_name)(task) | ||
| # Place None values at the end regardless of sort direction | ||
| return (val is None, val if val is not None else "") |
Contributor
There was a problem hiding this comment.
Is the fallback to empty string necessary? It is unnecessary and introduces a type assumption. The tuple ordering already guarantees safe handling of None, so we can avoid this entirely.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Calling
/api/v2/dags/{dag_id}/tasks?order_by=start_datecauses a 500 Internal Server Error:This happens because task attributes like
start_datecan beNone, and Python'ssorted()cannot compareNonevalues using<.Solution
Introduced a sort key function that:
(val is None, val)to ensureNonevalues sort to the endNoneto avoid comparison errorsFiles Changed
airflow-core/src/airflow/api_fastapi/core_api/routes/public/tasks.pyTesting
GET /api/v2/dags/{dag_id}/tasks?order_by=start_date→ returns tasks sorted with None values at the end (previously 500)GET /api/v2/dags/{dag_id}/tasks?order_by=-start_date→ returns tasks reverse-sorted with None values at the endGET /api/v2/dags/{dag_id}/tasks?order_by=task_id→ unchanged behavior (no None values)GET /api/v2/dags/{dag_id}/tasks?order_by=invalid_field→ still returns 400 Bad RequestCloses #63927