jsinovassin commented on code in PR #408:
URL: https://github.com/apache/unomi/pull/408#discussion_r861595407


##########
services/src/main/java/org/apache/unomi/services/impl/schemas/SchemaServiceImpl.java:
##########
@@ -266,6 +272,69 @@ public JsonSchema getJsonSchema(String schemaId) {
         return jsonSchemaFactory.getSchema(schemaAsString);
     }
 
+    private void findExtensionAndUpdateSchema(String schemaId) {
+        try {
+            String schemaAsString = 
objectMapper.writeValueAsString(schemasById.get(schemaId).getSchemaTree());
+            if (Objects.nonNull(schemaAsString)) {
+                extensionById.entrySet().stream().filter(entry -> 
entry.getValue().getSchemaId().equals(schemaId))
+                        .forEach(entry -> 
findAndUpdateSchemaWithExtension(schemaId, entry.getValue().getId()));
+            }
+        } catch (JsonProcessingException e) {
+            logger.error("Error when merging extensions into schema {}", 
schemaId, e);
+        }
+    }
+
+    private void findAndUpdateSchemaWithExtension(String schemaId, String 
extensionId) {
+        try {
+            JSONSchema schema = schemasById.get(schemaId);
+            if (Objects.nonNull(schema)) {
+                String schemaAsString = 
objectMapper.writeValueAsString(schemasById.get(schemaId).getSchemaTree());
+                JsonNode mergedSchema = 
mergeIntoSchema(objectMapper.readTree(schemaAsString),
+                        
objectMapper.readTree(extensionById.get(extensionId).getExtension()));
+                schemasById.put(mergedSchema.get("$id").asText(), 
buildJSONSchema(jsonSchemaFactory.getSchema(mergedSchema)));
+            }
+        } catch (JsonProcessingException e) {
+            logger.error("Error when merging extension {} into schema {}", 
extensionId, schemaId, e);
+        }
+    }
+
+    private JsonNode mergeIntoSchema(JsonNode schemaNode, JsonNode extension) {
+        extension.fields().forEachRemaining((entry) -> {
+            String path = JsonPointer.SEPARATOR + entry.getKey();
+            JsonNode targetNode = schemaNode.at(path);
+            JsonNode nodeToAdd = entry.getValue();
+            if (targetNode.isArray()) {
+                handleArray((ArrayNode) targetNode, nodeToAdd);
+            } else if (targetNode.isObject() && nodeToAdd.isObject()) {
+                mergeIntoSchema(targetNode, nodeToAdd);
+            } else if (targetNode instanceof MissingNode) {
+                ((ObjectNode) schemaNode).set(path.replace("/", ""), 
nodeToAdd);
+            }
+        });

Review Comment:
   If it's another type, for example text, number, boolean, we don't change the 
value of the property in the schema. We simply skip, so the user can not 
override simple property.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to