This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch v3-1-test
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/v3-1-test by this push:
     new 5d03970285b [v3-1-test] Return dag tags in alphabetical order (#58883) 
(#58904)
5d03970285b is described below

commit 5d03970285b1f8759e1e65fad5805e5ccbbc1eda
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Tue Dec 2 00:50:50 2025 +0100

    [v3-1-test] Return dag tags in alphabetical order (#58883) (#58904)
    
    * Return dag tags in alphabetical order
    
    * Return dag tags in alphabetical order
    
    * duplicated test
    (cherry picked from commit 20c44354f21785334e0e43cb4010cdae78d90120)
    
    Co-authored-by: Stuart Buckingham <[email protected]>
---
 .../api_fastapi/core_api/datamodels/dags.py        |  6 +++++
 .../core_api/routes/public/test_dags.py            | 27 ++++++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/airflow-core/src/airflow/api_fastapi/core_api/datamodels/dags.py 
b/airflow-core/src/airflow/api_fastapi/core_api/datamodels/dags.py
index 2a805da7abd..4cd3ed93817 100644
--- a/airflow-core/src/airflow/api_fastapi/core_api/datamodels/dags.py
+++ b/airflow-core/src/airflow/api_fastapi/core_api/datamodels/dags.py
@@ -28,6 +28,7 @@ from pydantic import (
     AliasGenerator,
     ConfigDict,
     computed_field,
+    field_serializer,
     field_validator,
 )
 
@@ -81,6 +82,11 @@ class DAGResponse(BaseModel):
     next_dagrun_run_after: datetime | None
     owners: list[str]
 
+    @field_serializer("tags")
+    def serialize_tags(self, tags: list[DagTagResponse]) -> 
list[DagTagResponse]:
+        """Sort tags alphabetically by name."""
+        return sorted(tags, key=lambda tag: tag.name)
+
     @field_validator("owners", mode="before")
     @classmethod
     def get_owners(cls, v: Any) -> list[str] | None:
diff --git 
a/airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_dags.py 
b/airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_dags.py
index 80a645ba699..78293779a09 100644
--- a/airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_dags.py
+++ b/airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_dags.py
@@ -1139,6 +1139,33 @@ class TestGetDag(TestDagEndpoint):
         }
         assert res_json == expected
 
+    def test_get_dag_tags_sorted_alphabetically(self, session, test_client, 
dag_maker):
+        """Test that tags are returned in alphabetical order for a single 
DAG."""
+        dag_id = "test_dag_single_sorted_tags"
+
+        # Create a DAG using dag_maker
+        with dag_maker(dag_id=dag_id, schedule=None):
+            EmptyOperator(task_id="task1")
+
+        dag_maker.sync_dagbag_to_db()
+
+        # Add tags in non-alphabetical order
+        tag_names = ["zebra", "alpha", "mike", "bravo"]
+        for tag_name in tag_names:
+            tag = DagTag(name=tag_name, dag_id=dag_id)
+            session.add(tag)
+
+        session.commit()
+
+        response = test_client.get(f"/dags/{dag_id}")
+        assert response.status_code == 200
+        res_json = response.json()
+
+        # Verify tags are sorted alphabetically
+        tag_names_in_response = [tag["name"] for tag in res_json["tags"]]
+        expected_sorted_tags = sorted(tag_names)
+        assert tag_names_in_response == expected_sorted_tags
+
     def test_get_dag_should_response_401(self, unauthenticated_test_client):
         response = unauthenticated_test_client.get(f"/dags/{DAG1_ID}")
         assert response.status_code == 401

Reply via email to