diff --git a/src/main/java/ch/dlmw/swisssignchallenge/controllers/SigningRequestDocumentController.java b/src/main/java/ch/dlmw/swisssignchallenge/controllers/SigningRequestDocumentController.java index ee26dc4..d09c4d7 100644 --- a/src/main/java/ch/dlmw/swisssignchallenge/controllers/SigningRequestDocumentController.java +++ b/src/main/java/ch/dlmw/swisssignchallenge/controllers/SigningRequestDocumentController.java @@ -26,13 +26,7 @@ public class SigningRequestDocumentController implements SigningRequestDocumentA var document = signingRequestDocumentService.getSigningRequestDocument(id); try { - JsonNode patchNode = objectMapper.valueToTree(patchOperations); - - JsonPatch jsonPatch = JsonPatch.fromJson(patchNode); - - JsonNode patchedNode = jsonPatch.apply(objectMapper.convertValue(document, JsonNode.class)); - - SigningRequestDocument patchedDocument = objectMapper.treeToValue(patchedNode, SigningRequestDocument.class); + var patchedDocument = applyPatch(patchOperations, document, SigningRequestDocument.class); signingRequestDocumentService.updateSigningRequestDocument(patchedDocument); @@ -44,4 +38,14 @@ public class SigningRequestDocumentController implements SigningRequestDocumentA } } + private T applyPatch(List patchOperations, T object, Class entityType) throws JsonPatchException, IOException { + JsonNode patchNode = objectMapper.valueToTree(patchOperations); + JsonPatch jsonPatch = JsonPatch.fromJson(patchNode); + + // Convert the entity to JsonNode and apply the patch + JsonNode entityNode = objectMapper.convertValue(object, JsonNode.class); + JsonNode patchedNode = jsonPatch.apply(entityNode); + + return objectMapper.treeToValue(patchedNode, entityType); + } }