From 905f81b8fb8f1e9655f04b3926658f6e01332cc3 Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 21 Dec 2023 11:44:22 -0500 Subject: [PATCH] fix bug where a dict with an id causes contained refs to fail to resolve --- CHANGES.rst | 3 +++ asdf/_tests/_regtests/test_1715.py | 28 ++++++++++++++++++++++++++++ asdf/reference.py | 4 ++-- 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 asdf/_tests/_regtests/test_1715.py diff --git a/CHANGES.rst b/CHANGES.rst index 5f55a8926..73e6e7490 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -42,6 +42,9 @@ The ASDF Standard is at v1.6.0 from working. If you want these warnings to produce errors you can now add your own warning filter [#1757] +- Fix bug where a dictionary containing a key ``id`` caused + any contained references to fail to resolve [#1716] + 3.0.1 (2023-10-30) ------------------ diff --git a/asdf/_tests/_regtests/test_1715.py b/asdf/_tests/_regtests/test_1715.py new file mode 100644 index 000000000..18c140eaa --- /dev/null +++ b/asdf/_tests/_regtests/test_1715.py @@ -0,0 +1,28 @@ +import pytest + +import asdf + + +def test_id_in_tree_breaks_ref(tmp_path): + """ + a dict containing id will break contained References + + https://github.com/asdf-format/asdf/issues/1715 + """ + external_fn = tmp_path / "external.asdf" + + external_tree = {"thing": 42} + + asdf.AsdfFile(external_tree).write_to(external_fn) + + main_fn = tmp_path / "main.asdf" + + af = asdf.AsdfFile({}) + af["id"] = "bogus" + af["myref"] = {"$ref": "external.asdf#/thing"} + af.write_to(main_fn) + + with pytest.warns(asdf.exceptions.AsdfDeprecationWarning, match="find_references"): + with asdf.open(main_fn) as af: + af.resolve_references() + assert af["myref"] == 42 diff --git a/asdf/reference.py b/asdf/reference.py index 7aeba1599..b2f5aad60 100644 --- a/asdf/reference.py +++ b/asdf/reference.py @@ -111,11 +111,11 @@ def find_references(tree, ctx, _warning_msg=False): `Reference` objects. """ - def do_find(tree, json_id): + def do_find(tree): if isinstance(tree, dict) and "$ref" in tree: if _warning_msg: warnings.warn(_warning_msg, AsdfDeprecationWarning) - return Reference(tree["$ref"], json_id, asdffile=ctx) + return Reference(tree["$ref"], asdffile=ctx) return tree return treeutil.walk_and_modify(tree, do_find, ignore_implicit_conversion=ctx._ignore_implicit_conversion)