From ef8c59988e38bf69bffa9c4bdd99110d1ace80e6 Mon Sep 17 00:00:00 2001
From: Daniel <d.hornung@indiscale.com>
Date: Fri, 8 Dec 2023 16:48:43 +0100
Subject: [PATCH] FIX: jsex: handle reference properties better

---
 CHANGELOG.md                                  |  2 +
 integrationtests/test_json_schema_exporter.py | 37 +++++++++++++++++++
 src/caosadvancedtools/json_schema_exporter.py |  6 +++
 unittests/test_json_schema_exporter.py        | 32 ++++++++++++++++
 4 files changed, 77 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 460d6756..ea3eff76 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 69edcf42..44b42826 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 910238c9..7f210f38 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 f0503385..4e3f4e18 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():
 
-- 
GitLab