Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion airflow/utils/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ def _default(obj):
return float(obj)
elif k8s is not None and isinstance(obj, k8s.V1Pod):
from airflow.kubernetes.pod_generator import PodGenerator
PodGenerator.serialize_pod(obj)
return PodGenerator.serialize_pod(obj)

raise TypeError(f"Object of type '{obj.__class__.__name__}' is not JSON serializable")
3 changes: 2 additions & 1 deletion airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from sqlalchemy import and_, desc, func, or_, union_all
from sqlalchemy.orm import joinedload
from wtforms import SelectField, validators
from airflow.utils import json as utils_json

import airflow
from airflow import models, plugins_manager, settings
Expand Down Expand Up @@ -2444,7 +2445,7 @@ def task_instances(self):
ti.task_id: alchemy_to_dict(ti)
for ti in dag.get_task_instances(dttm, dttm)}

return json.dumps(task_instances)
return json.dumps(task_instances, cls=utils_json.AirflowJsonEncoder)


class ConfigurationView(AirflowBaseView):
Expand Down
23 changes: 23 additions & 0 deletions tests/utils/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ def test_encode_numpy_float(self):
'3.76953125'
)

def test_encode_k8s_v1pod(self):
from kubernetes.client import models as k8s

pod = k8s.V1Pod(
metadata=k8s.V1ObjectMeta(
name="foo",
namespace="bar",
),
spec=k8s.V1PodSpec(
containers=[
k8s.V1Container(
name="foo",
image="bar",
)
]
)
)
self.assertEqual(
json.loads(json.dumps(pod, cls=utils_json.AirflowJsonEncoder)),
{"metadata": {"name": "foo", "namespace": "bar"},
"spec": {"containers": [{"image": "bar", "name": "foo"}]}}
)

def test_encode_raises(self):
self.assertRaisesRegex(TypeError,
"^.*is not JSON serializable$",
Expand Down