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 05:30:39 UTC

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

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


##########
api/src/main/java/org/apache/unomi/api/schema/JsonSchemaEntity.java:
##########
@@ -22,14 +22,14 @@
 /**
  * Object which represents a JSON schema stored in the persistence service
  */
-public class UnomiJSONSchema extends MetadataItem {
+public class JsonSchemaEntity extends MetadataItem {

Review Comment:
   please rename to `JSONSchemaEntity` to match naming format



##########
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:
   What happen if the json node is neither an Object nor an Array? 



-- 
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