diff --git a/CHANGELOG.md b/CHANGELOG.md index 04b5981bb221e749619aa11b018d495e395eff37..58534a476d65488f59b8a188f90542eb4c8774bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed ### +- `JsonSchemaParser` now identifies `name` properties in the schema with the + CaosDB name property. + ### Deprecated ### ### Removed ### diff --git a/src/caosadvancedtools/models/parser.py b/src/caosadvancedtools/models/parser.py index 48fc1e722c4ce9e888b8b80dbb5f29595c2f6b26..ad149222b5b90671a50943dc00bc9de8074a42f1 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__( # pylint: disable=no-member + dtype)) else: - value.datatype = db.__getattribute__(dtype) # pylint: disable=no-member + value.datatype = db.__getattribute__( # pylint: disable=no-member + dtype) continue @@ -632,8 +634,9 @@ class JsonSchemaParser(Parser): return self._create_model_from_dict(model_dict) def _create_model_from_dict(self, model_dict: [dict, List[dict]]): - """Parse a dictionary read in from the model definition in a json schema and - return the Datamodel created from it. + """Parse a dictionary and return the Datamodel created from it. + + The dictionary was typically created from the model definition in a json schema file. Parameters ---------- @@ -692,6 +695,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 +738,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/json-schema-models/datamodel_name.schema.json b/unittests/json-schema-models/datamodel_name.schema.json new file mode 100644 index 0000000000000000000000000000000000000000..c0e86028c36172d27a4523f2c08db1b413b5c19f --- /dev/null +++ b/unittests/json-schema-models/datamodel_name.schema.json @@ -0,0 +1,12 @@ +{ + "title": "Dataset", + "type": "object", + "properties": { + "name": { "type": "string", "description": "Name of this dataset" }, + "date_time": { "type": "string", "format": "date-time" }, + "date": { "type": "string", "format": "date" }, + "integer": { "type": "integer", "description": "Some integer property" }, + "boolean": { "type": "boolean" }, + "number_prop": { "type": "number", "description": "Some float property" } + } +} diff --git a/unittests/json-schema-models/datamodel_name_wrong_type.schema.json b/unittests/json-schema-models/datamodel_name_wrong_type.schema.json new file mode 100644 index 0000000000000000000000000000000000000000..1988ad3d8cd613def36df69f5ad30fedd0a26e48 --- /dev/null +++ b/unittests/json-schema-models/datamodel_name_wrong_type.schema.json @@ -0,0 +1,12 @@ +{ + "title": "Dataset", + "type": "object", + "properties": { + "name": { "type": "boolean", "description": "Name of this dataset" }, + "date_time": { "type": "string", "format": "date-time" }, + "date": { "type": "string", "format": "date" }, + "integer": { "type": "integer", "description": "Some integer property" }, + "boolean": { "type": "boolean" }, + "number_prop": { "type": "number", "description": "Some float property" } + } +} diff --git a/unittests/test_json_schema_model_parser.py b/unittests/test_json_schema_model_parser.py index 4b44f6efa1cda19c04ee13a6a50b04cefbff9177..7f47890f413dce5511cd498fe802e03a1af3be70 100644 --- a/unittests/test_json_schema_model_parser.py +++ b/unittests/test_json_schema_model_parser.py @@ -340,3 +340,19 @@ def test_list(): assert model[name].name == name assert len(model[name].parents) == 1 assert model[name].has_parent(model["license"]) + + +def test_name_property(): + model = parse_model_from_json_schema(os.path.join( + FILEPATH, "datamodel_name.schema.json")) + + dataset_rt = model["Dataset"] + assert dataset_rt.get_property("name") is None + assert "name" not in model + + 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 must be string-typed, otherwise it cannot be identified with CaosDB's " + "name property.")