From a3f5c9708761b96c1520e554bef5dbd69ab130bf Mon Sep 17 00:00:00 2001 From: fspreck <f.spreckelsen@indiscale.com> Date: Fri, 13 Oct 2023 18:05:09 +0200 Subject: [PATCH] ENH: Raise error in case of multiprops --- src/caosadvancedtools/json_schema_exporter.py | 8 ++++++- unittests/test_json_schema_exporter.py | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/caosadvancedtools/json_schema_exporter.py b/src/caosadvancedtools/json_schema_exporter.py index e7f54151..f18a6116 100644 --- a/src/caosadvancedtools/json_schema_exporter.py +++ b/src/caosadvancedtools/json_schema_exporter.py @@ -94,7 +94,7 @@ def _make_prop_from_prop(prop, additional_options_for_text_props): json_prop["type"] = "integer" elif prop.datatype == db.DOUBLE: json_prop["type"] = "number" - elif prop.datatype.startswith("LIST"): + elif isinstance(prop.datatype, str) and prop.datatype.startswith("LIST"): json_prop["type"] = "array" list_element_prop = db.Property( name=prop.name, datatype=_extract_elemet_dtyp_from_list_dtype(prop.datatype)) @@ -170,6 +170,12 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T props["description"] = _make_text_property("The description of the Record to be created") for prop in rt.properties: + if prop.name in props: + # Multi property + raise NotImplementedError( + "Creating a schema for multi-properties is not specified. " + f"Property {prop.name} occurs more than once." + ) props[prop.name] = _make_prop_from_prop(prop, additional_options_for_text_props) schema["properties"] = props diff --git a/unittests/test_json_schema_exporter.py b/unittests/test_json_schema_exporter.py index a17717b2..b62349d3 100644 --- a/unittests/test_json_schema_exporter.py +++ b/unittests/test_json_schema_exporter.py @@ -232,3 +232,24 @@ def test_rt_with_references(): with raises(NotImplementedError): rtjs(rt) + + +def test_broken(): + + rt = db.RecordType() + rt.add_property(name="something", datatype=None) + + with raises(ValueError) as ve: + + rtjs(rt) + assert str(ve).startswith("Unknown or no property datatype.") + + rt = db.RecordType() + rt.add_property(name="MultiProp", datatype=db.INTEGER) + rt.add_property(name="MultiProp", datatype=db.INTEGER) + + with raises(NotImplementedError) as nie: + + rtjs(rt) + assert "MultiProp" in str(nie) + assert str(nie).startswith("Creating a schema for multi-properties is not specified.") -- GitLab