Skip to content

Commit a729bf8

Browse files
jason810496claude
andcommitted
Report a schema error's path the same way under every jsonschema version
jsonschema 4.26 writes $.dag.tasks[2]['__type'] where 4.23 writes $.dag.tasks[2].__type, so the import error and three tests changed with the installed version. Build the path from the error's absolute path. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 962acf2 commit a729bf8

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

‎airflow-core/src/airflow/serialization/serialized_objects.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,16 @@ def _decode_start_trigger_args(var: dict[str, Any]) -> StartTriggerArgs:
300300
)
301301

302302

303+
def _build_json_path(error: ValidationError) -> str:
304+
"""
305+
Return where *error* is in the document, such as ``$.dag.tasks[2].__type``.
306+
307+
Unlike ``ValidationError.json_path``, whose quoting of a key such as ``__type`` depends on the
308+
jsonschema version, this is the same under every version.
309+
"""
310+
return "$" + "".join(f"[{part}]" if isinstance(part, int) else f".{part}" for part in error.absolute_path)
311+
312+
303313
class _XComRef(NamedTuple):
304314
"""
305315
Store info needed to create XComArg.
@@ -2182,7 +2192,7 @@ def validate_serialized_dag(cls, serialized_obj: dict[str, Any]) -> SerializedDA
21822192
cls.validate_schema(serialized_obj)
21832193
except ValidationError as e:
21842194
raise DeserializationError(
2185-
dag_id, f"Dag {dag_id!r} does not match the schema at {e.json_path}: {e.message}"
2195+
dag_id, f"Dag {dag_id!r} does not match the schema at {_build_json_path(e)}: {e.message}"
21862196
) from e
21872197
task_ids = collections.Counter(
21882198
task[Encoding.VAR]["task_id"] for task in serialized_obj["dag"]["tasks"]

‎airflow-core/tests/unit/serialization/test_dag_serialization.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5209,12 +5209,12 @@ def test_returns_the_deserialized_dag(self):
52095209
pytest.param({}, "$.dag.tasks[2]", id="empty"),
52105210
pytest.param("x", "$.dag.tasks[2]", id="not-an-object"),
52115211
pytest.param(
5212-
{"__type": "dag", "__var": {"task_id": "t"}}, "$.dag.tasks[2]['__type']", id="wrong-type"
5212+
{"__type": "dag", "__var": {"task_id": "t"}}, "$.dag.tasks[2].__type", id="wrong-type"
52135213
),
5214-
pytest.param({"__type": "operator", "__var": {}}, "$.dag.tasks[2]['__var']", id="no-task-id"),
5214+
pytest.param({"__type": "operator", "__var": {}}, "$.dag.tasks[2].__var", id="no-task-id"),
52155215
pytest.param(
52165216
{"__type": "operator", "__var": {"task_id": 5}},
5217-
"$.dag.tasks[2]['__var'].task_id",
5217+
"$.dag.tasks[2].__var.task_id",
52185218
id="task-id-not-a-string",
52195219
),
52205220
],

0 commit comments

Comments
 (0)