diff --git a/src/caosadvancedtools/json_schema_exporter.py b/src/caosadvancedtools/json_schema_exporter.py index 7bccae9d5ea7c908bf079eded8b7402bd1e6a2e2..d34bed9c0189789eb384c303f8311abb38be7324 100644 --- a/src/caosadvancedtools/json_schema_exporter.py +++ b/src/caosadvancedtools/json_schema_exporter.py @@ -33,7 +33,8 @@ def _make_required_list(rt: db.RecordType): if rt.get_importance(prop.name) == db.OBLIGATORY] -def _make_prop_from_prop(prop: db.Property, additional_options_for_text_props: Optional[dict], units_in_description: bool): +def _make_prop_from_prop(prop: db.Property, additional_options_for_text_props: Optional[dict], + units_in_description: bool): """Return the JSON Schema segment for the given property Parameters @@ -54,6 +55,8 @@ def _make_prop_from_prop(prop: db.Property, additional_options_for_text_props: O "Reference properties are not supported in this version of the json schema exporter." ) + if not additional_options_for_text_props: + additional_options_for_text_props = {} if prop.datatype == db.TEXT or prop.datatype == db.DATETIME: text_format = None text_pattern = None @@ -111,7 +114,19 @@ def _make_text_property(description="", text_format=None, text_pattern=None): prop["description"] = description if text_format is not None: if isinstance(text_format, list): - prop["anyOf"] = [{"format": tf} for tf in text_format] + # We want the type inside the options, not in the head: + # "datetime property": { + # "anyOf": [ + # { + # "type": "string", + # "format": "date" + # }, + # { + # "type": "string", + # "format": "date-time" + # }]} + prop.pop("type") + prop["anyOf"] = [{"type": "string", "format": tf} for tf in text_format] else: prop["format"] = text_format if text_pattern is not None: diff --git a/unittests/test_json_schema_exporter.py b/unittests/test_json_schema_exporter.py index 6b43b382ab460d37af855f8359d5a6005312e84f..b1a51f1aee87d500dab6536b0df08f1535226dbf 100644 --- a/unittests/test_json_schema_exporter.py +++ b/unittests/test_json_schema_exporter.py @@ -85,12 +85,20 @@ def test_rt_with_scalar_props(): assert props["SimpleText"]["description"] == "This is a simple text" assert "ObligatoryDatetime" in props - assert props["ObligatoryDatetime"]["type"] == "string" + assert "type" not in props["ObligatoryDatetime"] assert "anyOf" in props["ObligatoryDatetime"] assert len(props["ObligatoryDatetime"]["anyOf"]) == 2 - fmts = [fmt["format"] for fmt in props["ObligatoryDatetime"]["anyOf"]] - assert "date" in fmts - assert "date-time" in fmts + date_found = 0 + datetime_found = 0 + for option in props["ObligatoryDatetime"]["anyOf"]: + assert option["type"] == "string" + fmt = option["format"] + if fmt == "date": + date_found += 1 + if fmt == "date-time": + datetime_found += 1 + assert date_found == 1 + assert datetime_found == 1 assert "JustDateNoTime" in props assert props["JustDateNoTime"]["type"] == "string"