From a3c1998c56bdcb876124900c16d288bef422c4c0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 6 Jan 2025 20:26:19 -0800 Subject: [PATCH 1/2] [pre-commit.ci] pre-commit autoupdate (#1228) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 340c50361..05fbd3e04 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,7 +18,7 @@ repos: # hooks: # - id: black - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.8.4 + rev: v0.8.6 hooks: - id: ruff # - repo: https://github.com/econchick/interrogate From d63e5880265490dacd4362705fecd9ecbb2377ab Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Wed, 8 Jan 2025 22:22:27 -0500 Subject: [PATCH 2/2] Forbid slashes in names of Groups and Datasets (#1219) * * add checks to ensure that name and default name args do not contain slashes. * include tests * ruff * Update changelog --------- Co-authored-by: Ryan Ly --- CHANGELOG.md | 5 +++++ src/hdmf/spec/spec.py | 6 ++++++ tests/unit/spec_tests/test_dataset_spec.py | 14 ++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a1dd6701..4a38f0279 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/hdmf/spec/spec.py b/src/hdmf/spec/spec.py index e10d5e43e..9f02b262f 100644 --- a/src/hdmf/spec/spec.py +++ b/src/hdmf/spec/spec.py @@ -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: diff --git a/tests/unit/spec_tests/test_dataset_spec.py b/tests/unit/spec_tests/test_dataset_spec.py index c9db14635..60025fd7e 100644 --- a/tests/unit/spec_tests/test_dataset_spec.py +++ b/tests/unit/spec_tests/test_dataset_spec.py @@ -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')