From a985bd7e876510e6215814ea26268c9e2bd89352 Mon Sep 17 00:00:00 2001
From: fspreck <f.spreckelsen@indiscale.com>
Date: Mon, 2 May 2022 08:46:08 +0200
Subject: [PATCH] ENH: Add special treatment for name property

---
 src/caosadvancedtools/models/parser.py     | 19 +++++++++++++++++--
 unittests/test_json_schema_model_parser.py |  4 ++--
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/caosadvancedtools/models/parser.py b/src/caosadvancedtools/models/parser.py
index 48fc1e72..4d5730e5 100644
--- a/src/caosadvancedtools/models/parser.py
+++ b/src/caosadvancedtools/models/parser.py
@@ -566,9 +566,11 @@ class Parser(object):
                              db.BOOLEAN]:
 
                     if is_list:
-                        value.datatype = db.LIST(db.__getattribute__(dtype))  # pylint: disable=no-member
+                        value.datatype = db.LIST(db.__getattribute__(
+                            dtype))  # pylint: disable=no-member
                     else:
-                        value.datatype = db.__getattribute__(dtype)  # pylint: disable=no-member
+                        value.datatype = db.__getattribute__(
+                            dtype)  # pylint: disable=no-member
 
                     continue
 
@@ -692,6 +694,15 @@ class JsonSchemaParser(Parser):
             # Each element must have a specific type
             raise JsonSchemaDefinitionError(
                 f"`type` is missing in element {name}.")
+        if name == "name":
+            # This is identified with the CaosDB name property as long as the
+            # type is correct.
+            if not elt["type"] == "string":
+                raise JsonSchemaDefinitionError(
+                    "The 'name' property must be string-typed, otherwise it cannot "
+                    "be identified with CaosDB's name property."
+                )
+            return None, force_list
         if "enum" in elt:
             ent = self._treat_enum(elt, name)
         elif elt["type"] in JSON_SCHEMA_ATOMIC_TYPES:
@@ -726,6 +737,10 @@ class JsonSchemaParser(Parser):
                 else:
                     name = self._stringify(key)
                 prop_ent, force_list = self._treat_element(prop, name)
+                if prop_ent is None:
+                    # Nothing to be appended since the property has to be
+                    # treated specially.
+                    continue
                 importance = db.OBLIGATORY if key in required else db.RECOMMENDED
                 if not force_list:
                     rt.add_property(prop_ent, importance=importance)
diff --git a/unittests/test_json_schema_model_parser.py b/unittests/test_json_schema_model_parser.py
index 061d9299..c5dbcf53 100644
--- a/unittests/test_json_schema_model_parser.py
+++ b/unittests/test_json_schema_model_parser.py
@@ -350,8 +350,8 @@ def test_name_property():
     assert dataset_rt.get_property("name") is None
     assert "name" not in model
 
-    with pytest.raises(ValueError) as err:
+    with pytest.raises(JsonSchemaDefinitionError) as err:
         broken = parse_model_from_json_schema(os.path.join(
             FILEPATH, "datamodel_name_wrong_type.schema.json"))
     assert str(err.value).startswith(
-        "The 'name' property of Dataset must be string-typed, otherwise it cannot be identified with CaosDB's name property.")
+        "The 'name' property must be string-typed, otherwise it cannot be identified with CaosDB's name property.")
-- 
GitLab