Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions asdf/_asdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,31 +1095,25 @@ def add_history_entry(self, description, software=None):
if software is not None:
entry["software"] = software

# validate the history entry
schema.validate(
yamlutil.custom_tree_to_tagged_tree({"entry": entry}, self),
ctx=self,
)

if self.version >= versioning.NEW_HISTORY_FORMAT_MIN_VERSION:
if "history" not in self.tree:
self.tree["history"] = {"entries": []}
elif "entries" not in self.tree["history"]:
self.tree["history"]["entries"] = []

self.tree["history"]["entries"].append(entry)

try:
self.validate()
except Exception:
self.tree["history"]["entries"].pop()
raise
else:
if "history" not in self.tree:
self.tree["history"] = []

self.tree["history"].append(entry)

try:
self.validate()
except Exception:
self.tree["history"].pop()
raise

def get_history_entries(self):
"""
Get a list of history entries from the file object.
Expand Down
20 changes: 20 additions & 0 deletions asdf/_tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,23 @@ class FractionExtension(Extension):

with asdf.open(file_path_3) as af:
assert len(af["history"]["extensions"]) == 2


def test_history_validate():
"""
Test that add_history_entry validates the generated entry and doesn't
add the entry to the history list if invalid.
"""
af = asdf.AsdfFile(version="1.6.0")
# add an invalid item to the tree to check if adding a history entry validates
# the entire tree
af["invalid"] = asdf.tagged.TaggedDict({}, tag="tag:stsci.edu:asdf/core/ndarray-1.1.0")
with pytest.raises(ValidationError):
af.validate()

af.add_history_entry("test")
assert af.get_history_entries()[0]["description"] == "test"

with pytest.raises(ValidationError):
af.add_history_entry(1)
assert len(af.get_history_entries()) == 1
1 change: 1 addition & 0 deletions changes/2029.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove whole-tree validation in ``AsdfFile.add_history_entry`` and replace it with validation of the newly created entry.
Loading