Skip to content

Commit 1874f88

Browse files
authored
Merge pull request #2029 from braingram/validate_history
only validate the new entry in add_history_entry
2 parents 416ac12 + dc17bd1 commit 1874f88

3 files changed

Lines changed: 27 additions & 12 deletions

File tree

asdf/_asdf.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,31 +1095,25 @@ def add_history_entry(self, description, software=None):
10951095
if software is not None:
10961096
entry["software"] = software
10971097

1098+
# validate the history entry
1099+
schema.validate(
1100+
yamlutil.custom_tree_to_tagged_tree({"entry": entry}, self),
1101+
ctx=self,
1102+
)
1103+
10981104
if self.version >= versioning.NEW_HISTORY_FORMAT_MIN_VERSION:
10991105
if "history" not in self.tree:
11001106
self.tree["history"] = {"entries": []}
11011107
elif "entries" not in self.tree["history"]:
11021108
self.tree["history"]["entries"] = []
11031109

11041110
self.tree["history"]["entries"].append(entry)
1105-
1106-
try:
1107-
self.validate()
1108-
except Exception:
1109-
self.tree["history"]["entries"].pop()
1110-
raise
11111111
else:
11121112
if "history" not in self.tree:
11131113
self.tree["history"] = []
11141114

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

1117-
try:
1118-
self.validate()
1119-
except Exception:
1120-
self.tree["history"].pop()
1121-
raise
1122-
11231117
def get_history_entries(self):
11241118
"""
11251119
Get a list of history entries from the file object.

asdf/_tests/test_history.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,23 @@ class FractionExtension(Extension):
273273

274274
with asdf.open(file_path_3) as af:
275275
assert len(af["history"]["extensions"]) == 2
276+
277+
278+
def test_history_validate():
279+
"""
280+
Test that add_history_entry validates the generated entry and doesn't
281+
add the entry to the history list if invalid.
282+
"""
283+
af = asdf.AsdfFile(version="1.6.0")
284+
# add an invalid item to the tree to check if adding a history entry validates
285+
# the entire tree
286+
af["invalid"] = asdf.tagged.TaggedDict({}, tag="tag:stsci.edu:asdf/core/ndarray-1.1.0")
287+
with pytest.raises(ValidationError):
288+
af.validate()
289+
290+
af.add_history_entry("test")
291+
assert af.get_history_entries()[0]["description"] == "test"
292+
293+
with pytest.raises(ValidationError):
294+
af.add_history_entry(1)
295+
assert len(af.get_history_entries()) == 1

changes/2029.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove whole-tree validation in ``AsdfFile.add_history_entry`` and replace it with validation of the newly created entry.

0 commit comments

Comments
 (0)