Repository: asdf-format/asdf

Issue:
Tree containing `id` causes reference resolution failure.
# Description of the problem

The existence of a dictionary with a key `id` anywhere "higher" in the tree than a reference causes the reference to fail to resolve.

# Example of the problem

```python
import asdf


other_tree = {
    'thing': 42,
}

tree = {
    'myref': {
        '$ref': 'external.asdf#/thing'
    }
}

asdf.AsdfFile(other_tree).write_to('external.asdf')

uri=asdf.util.filepath_to_url(os.path.abspath('main.asdf'))
af = asdf.AsdfFile(tree, uri=uri)
af.resolve_references()
assert af['myref'] == 42

tree['id'] = 'bogus'
af = asdf.AsdfFile(tree, uri=uri)
af.resolve_references()  # raises ValueError
```

# System information

asdf version: main
python version: 3.10
operating system: mac osx


Discussion:

Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 556369e9..d02fdc48 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -13,6 +13,9 @@
   a file and ``AsdfPackageVersionWarning`` when installed extension/manifest
   package does not match that used to write the file [#1758]
 
+- Fix bug where a dictionary containing a key ``id`` caused
+  any contained references to fail to resolve [#1716]
+
 
 3.2.0 (2024-04-05)
 ------------------
diff --git a/asdf/reference.py b/asdf/reference.py
index 7aeba159..b2f5aad6 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)


Test patch:
diff --git a/asdf/_tests/_regtests/test_1715.py b/asdf/_tests/_regtests/test_1715.py
new file mode 100644
index 00000000..18c140ea
--- /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