diff --git a/CHANGELOG.md b/CHANGELOG.md index 460d675602600bfee2e7ce357d870d2a801cfba2..ea3eff76a5fe0aa9a5bb912ef0002f82f2508248 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed ### +- Json schema exporter handles reference properties better. + ### Security ### ### Documentation ### diff --git a/integrationtests/test_json_schema_exporter.py b/integrationtests/test_json_schema_exporter.py index 69edcf42d1fd285c030ad6d6ccb7f73f2d1b5536..44b428263ebbd9696fc2a171ea356d764482d5e3 100644 --- a/integrationtests/test_json_schema_exporter.py +++ b/integrationtests/test_json_schema_exporter.py @@ -20,9 +20,12 @@ # with this program. If not, see <https://www.gnu.org/licenses/>. # +import json + import linkahead as db from caosadvancedtools.json_schema_exporter import recordtype_to_json_schema as rtjs +from caosadvancedtools.models.parser import parse_model_from_string def _delete_everything(): @@ -75,3 +78,37 @@ def test_uniqueness_of_reference_types(): assert one_of[1 - enum_index]["type"] == "object" # No properties in parent_type assert len(one_of[1 - enum_index]["properties"]) == 0 + + +def test_reference_property(): + model_string = """ +RT1: + description: Some recordtype +RT2: + obligatory_properties: + prop1: + description: Some reference property + datatype: RT1 + """ + model = parse_model_from_string(model_string) + model.sync_data_model(noquestion=True) + schema = rtjs(db.RecordType(name="RT2").retrieve()) + assert json.dumps(schema, indent=2) == """{ + "type": "object", + "required": [ + "prop1" + ], + "additionalProperties": true, + "title": "RT2", + "properties": { + "prop1": { + "type": "object", + "required": [], + "additionalProperties": true, + "description": "Some reference property", + "title": "prop1", + "properties": {} + } + }, + "$schema": "https://json-schema.org/draft/2020-12/schema" +}""" diff --git a/src/caosadvancedtools/json_schema_exporter.py b/src/caosadvancedtools/json_schema_exporter.py index 910238c9045269d2e5eca4134f4addbcbb672bb4..7f210f385fd6560ecb45942af0267f73c00e078e 100644 --- a/src/caosadvancedtools/json_schema_exporter.py +++ b/src/caosadvancedtools/json_schema_exporter.py @@ -272,6 +272,12 @@ class JsonSchemaExporter: rt = db.execute_query(f"FIND RECORDTYPE WITH name='{prop_name}'", unique=True) subschema, ui_schema = self._make_segment_from_recordtype(rt) + if prop.is_reference(): + if prop.name: + subschema["title"] = prop.name + if prop.description: + subschema["description"] = prop.description + # if inner_ui_schema: # ui_schema = inner_ui_schema if values: diff --git a/unittests/test_json_schema_exporter.py b/unittests/test_json_schema_exporter.py index f0503385a25eb89e66dd3518d71a32b91d07bf88..4e3f4e18141b87a99b50b9bd4be6635ed8ed8472 100644 --- a/unittests/test_json_schema_exporter.py +++ b/unittests/test_json_schema_exporter.py @@ -598,6 +598,38 @@ def test_rt_with_references(): assert items["properties"]["file"]["type"] == "string" assert items["properties"]["file"]["format"] == "data-url" + # Test reference property + model_string = """ +RT1: + description: Some recordtype +RT2: + obligatory_properties: + prop1: + description: Some reference property + datatype: RT1 + """ + model = parse_model_from_string(model_string) + schema = rtjs(model.get_deep("RT2"), no_remote=True) + assert json.dumps(schema, indent=2) == """{ + "type": "object", + "required": [ + "RT1" + ], + "additionalProperties": true, + "title": "RT2", + "properties": { + "prop1": { + "type": "object", + "required": [], + "additionalProperties": true, + "description": "Some reference property", + "title": "prop1", + "properties": {} + } + }, + "$schema": "https://json-schema.org/draft/2020-12/schema" +}""" + def test_broken():