You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by as...@apache.org on 2021/03/19 15:06:26 UTC

[airflow] 24/42: Bugfix: Fix wrong output of tags and owners in dag detail API endpoint (#14490)

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

ash pushed a commit to branch v2-0-test
in repository https://gitbox.apache.org/repos/asf/airflow.git

commit 5dd51dc903933457e3f2978c22d8e0f98eb24ff1
Author: Ephraim Anierobi <sp...@gmail.com>
AuthorDate: Wed Mar 3 15:39:02 2021 +0100

    Bugfix: Fix wrong output of tags and owners in dag detail API endpoint (#14490)
    
    * fix wrong output of tags and owners in dag detail endpoint
    
    * fixup! fix wrong output of tags and owners in dag detail endpoint
    
    * fixup! fixup! fix wrong output of tags and owners in dag detail endpoint
    
    (cherry picked from commit 4424d10f05fa268b54c81ef8b96a0745643690b6)
---
 airflow/api_connexion/schemas/dag_schema.py        | 36 ++++++++++++++++------
 tests/api_connexion/endpoints/test_dag_endpoint.py | 28 ++++++++++-------
 tests/api_connexion/schemas/test_dag_schema.py     |  3 +-
 3 files changed, 46 insertions(+), 21 deletions(-)

diff --git a/airflow/api_connexion/schemas/dag_schema.py b/airflow/api_connexion/schemas/dag_schema.py
index b15fbd6..aabd215 100644
--- a/airflow/api_connexion/schemas/dag_schema.py
+++ b/airflow/api_connexion/schemas/dag_schema.py
@@ -21,6 +21,7 @@ from itsdangerous import URLSafeSerializer
 from marshmallow import Schema, fields
 from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field
 
+from airflow import DAG
 from airflow.api_connexion.schemas.common_schema import ScheduleIntervalSchema, TimeDeltaSchema, TimezoneField
 from airflow.configuration import conf
 from airflow.models.dag import DagModel, DagTag
@@ -73,15 +74,32 @@ class DAGSchema(SQLAlchemySchema):
 class DAGDetailSchema(DAGSchema):
     """DAG details"""
 
-    timezone = TimezoneField(dump_only=True)
-    catchup = fields.Boolean(dump_only=True)
-    orientation = fields.String(dump_only=True)
-    concurrency = fields.Integer(dump_only=True)
-    start_date = fields.DateTime(dump_only=True)
-    dag_run_timeout = fields.Nested(TimeDeltaSchema, dump_only=True, attribute="dagrun_timeout")
-    doc_md = fields.String(dump_only=True)
-    default_view = fields.String(dump_only=True)
-    params = fields.Dict(dump_only=True)
+    owners = fields.Method("get_owners", dump_only=True)
+    timezone = TimezoneField()
+    catchup = fields.Boolean()
+    orientation = fields.String()
+    concurrency = fields.Integer()
+    start_date = fields.DateTime()
+    dag_run_timeout = fields.Nested(TimeDeltaSchema, attribute="dagrun_timeout")
+    doc_md = fields.String()
+    default_view = fields.String()
+    params = fields.Dict()
+    tags = fields.Method("get_tags", dump_only=True)
+
+    @staticmethod
+    def get_tags(obj: DAG):
+        """Dumps tags as objects"""
+        tags = obj.tags
+        if tags:
+            return [DagTagSchema().dump(dict(name=tag)) for tag in tags]
+        return []
+
+    @staticmethod
+    def get_owners(obj: DAG):
+        """Convert owners attribute to DAG representation"""
+        if not getattr(obj, 'owner', None):
+            return []
+        return obj.owner.split(",")
 
 
 class DAGCollection(NamedTuple):
diff --git a/tests/api_connexion/endpoints/test_dag_endpoint.py b/tests/api_connexion/endpoints/test_dag_endpoint.py
index 146720c..6041b5f 100644
--- a/tests/api_connexion/endpoints/test_dag_endpoint.py
+++ b/tests/api_connexion/endpoints/test_dag_endpoint.py
@@ -75,7 +75,13 @@ class TestDagEndpoint(unittest.TestCase):
             access_control={'TestGranularDag': [permissions.ACTION_CAN_EDIT, permissions.ACTION_CAN_READ]},
         )
 
-        with DAG(cls.dag_id, start_date=datetime(2020, 6, 15), doc_md="details", params={"foo": 1}) as dag:
+        with DAG(
+            cls.dag_id,
+            start_date=datetime(2020, 6, 15),
+            doc_md="details",
+            params={"foo": 1},
+            tags=['example'],
+        ) as dag:
             DummyOperator(task_id=cls.task_id)
 
         with DAG(cls.dag2_id, start_date=datetime(2020, 6, 15)) as dag2:  # no doc_md
@@ -212,7 +218,7 @@ class TestGetDagDetails(TestDagEndpoint):
             "is_paused": None,
             "is_subdag": False,
             "orientation": "LR",
-            "owners": [],
+            "owners": ['airflow'],
             "params": {"foo": 1},
             "schedule_interval": {
                 "__type": "TimeDelta",
@@ -221,7 +227,7 @@ class TestGetDagDetails(TestDagEndpoint):
                 "seconds": 0,
             },
             "start_date": "2020-06-15T00:00:00+00:00",
-            "tags": None,
+            "tags": [{'name': 'example'}],
             "timezone": "Timezone('UTC')",
         }
         assert response.json == expected
@@ -244,7 +250,7 @@ class TestGetDagDetails(TestDagEndpoint):
             "is_paused": None,
             "is_subdag": False,
             "orientation": "LR",
-            "owners": [],
+            "owners": ['airflow'],
             "params": {},
             "schedule_interval": {
                 "__type": "TimeDelta",
@@ -253,7 +259,7 @@ class TestGetDagDetails(TestDagEndpoint):
                 "seconds": 0,
             },
             "start_date": "2020-06-15T00:00:00+00:00",
-            "tags": None,
+            "tags": [],
             "timezone": "Timezone('UTC')",
         }
         assert response.json == expected
@@ -276,7 +282,7 @@ class TestGetDagDetails(TestDagEndpoint):
             "is_paused": None,
             "is_subdag": False,
             "orientation": "LR",
-            "owners": [],
+            "owners": ['airflow'],
             "params": {},
             "schedule_interval": {
                 "__type": "TimeDelta",
@@ -285,7 +291,7 @@ class TestGetDagDetails(TestDagEndpoint):
                 "seconds": 0,
             },
             "start_date": None,
-            "tags": None,
+            "tags": [],
             "timezone": "Timezone('UTC')",
         }
         assert response.json == expected
@@ -313,7 +319,7 @@ class TestGetDagDetails(TestDagEndpoint):
             "is_paused": None,
             "is_subdag": False,
             "orientation": "LR",
-            "owners": [],
+            "owners": ['airflow'],
             "params": {"foo": 1},
             "schedule_interval": {
                 "__type": "TimeDelta",
@@ -322,7 +328,7 @@ class TestGetDagDetails(TestDagEndpoint):
                 "seconds": 0,
             },
             "start_date": "2020-06-15T00:00:00+00:00",
-            "tags": None,
+            "tags": [{'name': 'example'}],
             "timezone": "Timezone('UTC')",
         }
         response = client.get(
@@ -349,11 +355,11 @@ class TestGetDagDetails(TestDagEndpoint):
             'is_paused': None,
             'is_subdag': False,
             'orientation': 'LR',
-            'owners': [],
+            'owners': ['airflow'],
             "params": {"foo": 1},
             'schedule_interval': {'__type': 'TimeDelta', 'days': 1, 'microseconds': 0, 'seconds': 0},
             'start_date': '2020-06-15T00:00:00+00:00',
-            'tags': None,
+            'tags': [{'name': 'example'}],
             'timezone': "Timezone('UTC')",
         }
         assert response.json == expected
diff --git a/tests/api_connexion/schemas/test_dag_schema.py b/tests/api_connexion/schemas/test_dag_schema.py
index 2811bf3..96aba1f 100644
--- a/tests/api_connexion/schemas/test_dag_schema.py
+++ b/tests/api_connexion/schemas/test_dag_schema.py
@@ -107,6 +107,7 @@ class TestDAGDetailSchema:
             orientation="LR",
             default_view="duration",
             params={"foo": 1},
+            tags=['example1', 'example2'],
         )
         schema = DAGDetailSchema()
         expected = {
@@ -126,7 +127,7 @@ class TestDAGDetailSchema:
             'params': {'foo': 1},
             'schedule_interval': {'__type': 'TimeDelta', 'days': 1, 'seconds': 0, 'microseconds': 0},
             'start_date': '2020-06-19T00:00:00+00:00',
-            'tags': None,
+            'tags': [{'name': "example1"}, {'name': "example2"}],
             'timezone': "Timezone('UTC')",
         }
         assert schema.dump(dag) == expected