Skip to content

Commit

Permalink
Forbid slashes in names of Groups and Datasets (#1219)
Browse files Browse the repository at this point in the history
* * add checks to ensure that name and default name args do not contain slashes.
* include tests

* ruff

* Update changelog

---------

Co-authored-by: Ryan Ly <[email protected]>
  • Loading branch information
bendichter and rly authored Jan 9, 2025
1 parent a3c1998 commit d63e588
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# HDMF Changelog

## HDMF 4.0.0 (Upcoming)

### Bug fixes
- Added checks to ensure that group and dataset spec names and default names do not contain slashes. @bendichter [#1219](https://github.com/hdmf-dev/hdmf/pull/1219)

## HDMF 3.14.6 (December 20, 2024)

### Enhancements
Expand Down
6 changes: 6 additions & 0 deletions src/hdmf/spec/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,18 @@ class BaseStorageSpec(Spec):
def __init__(self, **kwargs):
name, doc, quantity, attributes, linkable, data_type_def, data_type_inc = \
getargs('name', 'doc', 'quantity', 'attributes', 'linkable', 'data_type_def', 'data_type_inc', kwargs)
if name is not None and "/" in name:
raise ValueError(f"Name '{name}' is invalid. Names of Groups and Datasets cannot contain '/'")
if name is None and data_type_def is None and data_type_inc is None:
raise ValueError("Cannot create Group or Dataset spec with no name "
"without specifying '%s' and/or '%s'." % (self.def_key(), self.inc_key()))
super().__init__(doc, name=name)
default_name = getargs('default_name', kwargs)
if default_name:
if "/" in default_name:
raise ValueError(
f"Default name '{default_name}' is invalid. Names of Groups and Datasets cannot contain '/'"
)
if name is not None:
warn("found 'default_name' with 'name' - ignoring 'default_name'")
else:
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/spec_tests/test_dataset_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,17 @@ def test_build_warn_extra_args(self):
"'dtype': 'int', 'required': True}")
with self.assertWarnsWith(UserWarning, msg):
DatasetSpec.build_spec(spec_dict)

def test_constructor_validates_name(self):
with self.assertRaisesWith(
ValueError,
"Name 'one/two' is invalid. Names of Groups and Datasets cannot contain '/'",
):
DatasetSpec(doc='my first dataset', dtype='int', name='one/two')

def test_constructor_validates_default_name(self):
with self.assertRaisesWith(
ValueError,
"Default name 'one/two' is invalid. Names of Groups and Datasets cannot contain '/'",
):
DatasetSpec(doc='my first dataset', dtype='int', default_name='one/two', data_type_def='test')

0 comments on commit d63e588

Please sign in to comment.