1111from typing import TYPE_CHECKING
1212
1313import asdf
14- import asdf .schema
14+ import asdf .treeutil
1515from semantic_version import Version
1616
1717from ._registry import NODE_CLASSES_BY_TAG , SCHEMA_URIS_BY_TAG
2727NOBOOL = False
2828
2929
30+ @functools .cache
31+ def _load_schema (schema_uri : str ) -> dict [str , Any ]:
32+ """
33+ Load a schema by URI.
34+
35+ Parameters
36+ ----------
37+ uri : str
38+ The URI of the schema to load.
39+ """
40+ # See Issue https://github.com/asdf-format/asdf/issues/1977
41+ if Version (asdf .__version__ ) < Version ("5.1.0" ):
42+ schema = asdf .schema .load_schema (schema_uri , resolve_references = False )
43+
44+ def resolve_refs (node , json_id ):
45+ if json_id is None :
46+ json_id = schema_uri
47+
48+ if isinstance (node , dict ) and "$ref" in node :
49+ suburl_base , suburl_fragment = asdf .schema ._safe_resolve (json_id , node ["$ref" ])
50+
51+ if suburl_base == schema_uri or suburl_base == schema .get ("id" ):
52+ # This is a local ref, which we'll resolve in both cases.
53+ subschema = schema
54+ else :
55+ subschema = asdf .schema .load_schema (suburl_base , True )
56+
57+ return asdf .treeutil .walk_and_modify (asdf .reference .resolve_fragment (subschema , suburl_fragment ), resolve_refs )
58+
59+ return node
60+
61+ return asdf .treeutil .walk_and_modify (schema , resolve_refs ) # type: ignore[no-any-return]
62+
63+ return asdf .schema .load_schema (schema_uri , resolve_references = True ) # type: ignore[no-any-return]
64+
65+
3066@functools .cache
3167def get_latest_schema (uri : str ) -> tuple [str , dict [str , Any ]]:
3268 """
@@ -51,7 +87,7 @@ def get_latest_schema(uri: str) -> tuple[str, dict[str, Any]]:
5187 if latest_uri is None :
5288 raise ValueError (f"No schema found for { uri } " )
5389
54- return latest_uri , asdf . schema . load_schema (latest_uri , resolve_references = True )
90+ return latest_uri , _load_schema (latest_uri )
5591
5692
5793@functools .cache
@@ -66,7 +102,7 @@ def _get_schema_from_tag(tag):
66102 """
67103 schema_uri = SCHEMA_URIS_BY_TAG [tag ]
68104
69- return asdf . schema . load_schema (schema_uri , resolve_references = True )
105+ return _load_schema (schema_uri )
70106
71107
72108class _MissingKeywordType :
0 commit comments