Make applyPatch a generic method

This commit is contained in:
2025-01-31 11:23:57 +01:00
parent 50ca04147a
commit d0d6f110c5

View File

@ -26,13 +26,7 @@ public class SigningRequestDocumentController implements SigningRequestDocumentA
var document = signingRequestDocumentService.getSigningRequestDocument(id); var document = signingRequestDocumentService.getSigningRequestDocument(id);
try { try {
JsonNode patchNode = objectMapper.valueToTree(patchOperations); var patchedDocument = applyPatch(patchOperations, document, SigningRequestDocument.class);
JsonPatch jsonPatch = JsonPatch.fromJson(patchNode);
JsonNode patchedNode = jsonPatch.apply(objectMapper.convertValue(document, JsonNode.class));
SigningRequestDocument patchedDocument = objectMapper.treeToValue(patchedNode, SigningRequestDocument.class);
signingRequestDocumentService.updateSigningRequestDocument(patchedDocument); signingRequestDocumentService.updateSigningRequestDocument(patchedDocument);
@ -44,4 +38,14 @@ public class SigningRequestDocumentController implements SigningRequestDocumentA
} }
} }
private <T> T applyPatch(List<PatchOperation> patchOperations, T object, Class<T> 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);
}
} }