You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by GitBox <gi...@apache.org> on 2022/04/29 08:49:02 UTC

[GitHub] [unomi] jsinovassin commented on a diff in pull request #408: UNOMI-561 : Add merge extension logic

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: dev-unsubscribe@unomi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org