Skip to content

Commit

Permalink
Merge pull request #1785 from braingram/manifest_extension_mismatch
Browse files Browse the repository at this point in the history
issue warning if manifest id does not match uri
  • Loading branch information
braingram committed May 10, 2024
2 parents ffee4b3 + 5717f20 commit 1722dcf
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
- Fix bug where a dictionary containing a key ``id`` caused
any contained references to fail to resolve [#1716]

- Issue a ``AsdfManifestURIMismatchWarning`` during write if a used
extension was created from a manifest registered with a uri that
does not match the id in the manifest [#1785]


3.2.0 (2024-04-05)
------------------
Expand Down
11 changes: 10 additions & 1 deletion asdf/_asdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .exceptions import (
AsdfConversionWarning,
AsdfDeprecationWarning,
AsdfManifestURIMismatchWarning,
AsdfPackageVersionWarning,
AsdfWarning,
DelimiterNotFoundError,
Expand Down Expand Up @@ -491,7 +492,15 @@ def _update_extension_history(self, tree, serialization_context):
if manifest is not None:
# check if this extension was built from a manifest is a different package
resource_mapping = get_config().resource_manager._mappings_by_uri.get(manifest["id"])
if resource_mapping.package_name != extension.package_name:
# if an extension was registered with a manifest uri that does not match the id the
# resource_mapping lookup will fail (resource_mapping will be None)
if resource_mapping is None:
warnings.warn(
f"Extension ({extension.extension_uri}) uses a manifest with a uri that "
"does not match it's id. Please open an issue with the extension maintainer",
AsdfManifestURIMismatchWarning,
)
elif resource_mapping.package_name != extension.package_name:
ext_meta["manifest_software"] = Software(
name=resource_mapping.package_name,
version=resource_mapping.package_version,
Expand Down
43 changes: 42 additions & 1 deletion asdf/_tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from yaml.representer import RepresenterError

from asdf import AsdfFile, config_context
from asdf.exceptions import AsdfWarning, ValidationError
from asdf.exceptions import AsdfManifestURIMismatchWarning, AsdfWarning, ValidationError
from asdf.extension import (
Compressor,
Converter,
Expand Down Expand Up @@ -941,3 +941,44 @@ class FractionWithInverseExtension:

read_f1 = roundtrip_object(f1)
assert read_f1.inverse.inverse is read_f1


def test_manifest_uri_id_mismatch_warning(tmp_path):
with config_context() as config:
# make an extension with a manifest (where id doesn't match the registered uri)
full_manifest = """%YAML 1.1
---
id: asdf://somewhere.org/manifests/foo
extension_uri: asdf://somewhere.org/extensions/foo
tags:
- asdf://somewhere.org/tags/bar
...
"""
config.add_resource_mapping({"asdf://somewhere.org/extensions/foo": full_manifest})

class Foo:
pass

class FooConverter:
tags = ["asdf://somewhere.org/tags/bar"]
types = [Foo]

def to_yaml_tree(self, *args):
return {}

def from_yaml_tree(self, *args):
return Foo()

extension = ManifestExtension.from_uri(
"asdf://somewhere.org/extensions/foo",
converters=[FooConverter()],
)

# use the extension to write out a file
config.add_extension(extension)

af = AsdfFile()
af["foo"] = Foo()
fn = tmp_path / "foo.asdf"
with pytest.warns(AsdfManifestURIMismatchWarning):
af.write_to(fn)
9 changes: 9 additions & 0 deletions asdf/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
__all__ = [
"AsdfConversionWarning",
"AsdfDeprecationWarning",
"AsdfManifestURIMismatchWarning",
"AsdfPackageVersionWarning",
"AsdfProvisionalAPIWarning",
"AsdfWarning",
"DelimiterNotFoundError",
Expand Down Expand Up @@ -54,3 +56,10 @@ class AsdfPackageVersionWarning(AsdfWarning):
"""
A warning indicating a package version mismatch
"""


class AsdfManifestURIMismatchWarning(AsdfWarning):
"""
A warning indicaing that an extension registered with a manifest
contains a id that does not match the uri of the manifest.
"""
8 changes: 5 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,7 @@ commands_pre =
pip install -r {env_tmp_dir}/requirements.txt
pip freeze
commands =
pytest roman_datamodels/tests \
-W "ignore:asdf.extensions plugin from package gwcs==0.18.3:asdf.exceptions.AsdfWarning"
pytest roman_datamodels/tests

[testenv:weldx]
change_dir = {env_tmp_dir}
Expand Down Expand Up @@ -320,5 +319,8 @@ commands_pre =
pip install -r {env_tmp_dir}/requirements.txt
pip freeze
commands =
# the AsdfManifestURIMismatchWarning filter can be removed when a new sunpy
# is released which contains the fixed manifests:
# https://github.com/sunpy/sunpy/pull/7432
pytest dkist \
-W "ignore:asdf.extensions plugin from package gwcs==0.18.3:asdf.exceptions.AsdfWarning"
-W "ignore::asdf.exceptions.AsdfManifestURIMismatchWarning"

0 comments on commit 1722dcf

Please sign in to comment.