Skip to content

Commit

Permalink
fix: retain null field in sync node merger
Browse files Browse the repository at this point in the history
  • Loading branch information
sameerzuberi committed Jan 30, 2024
1 parent e379fa0 commit c7c8290
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ && isDocVersionSame(localShadowDocument.get(), syncInformation, DataOwner.LOCAL)
long cloudDocumentVersion = cloudShadowDocument.get().getVersion();

// If the cloud document version is different from the last sync, that means the local document needed
// some updates. So we go ahead an update the local shadow document.
// some updates. So we go ahead and update the local shadow document.
if (!isDocVersionSame(cloudShadowDocument.get(), syncInformation, DataOwner.CLOUD)) {
localDocumentVersion = updateLocalDocumentAndGetUpdatedVersion(updateDocument,
Optional.of(localDocumentVersion));
}
// If the local document version is different from the last sync, that means the cloud document needed
// some updates. So we go ahead an update the cloud shadow document.
// some updates. So we go ahead and update the cloud shadow document.
if (!isDocVersionSame(localShadowDocument.get(), syncInformation, DataOwner.LOCAL)) {
cloudDocumentVersion = updateCloudDocumentAndGetUpdatedVersion(updateDocument,
Optional.of(cloudDocumentVersion));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private JsonMerger() {

/**
* Merges the patch JSON node to the existing source JSON node. If the node already exists in the source, then
* it replaces it. If the node is an array, then the the source array's contents are overwritten with the contents
* it replaces it. If the node is an array, then the source array's contents are overwritten with the contents
* from the patch.
*
* @param source The source JSON to merge.
Expand All @@ -44,9 +44,6 @@ public static void merge(JsonNode source, final JsonNode patch) throws InvalidRe
return;
}

// If the source node contains keys not present in the patch node, those keys have been deleted
removeDeletedFields((ObjectNode) source, (ObjectNode) patch);

// If both nodes are objects then do a recursive patch
if (source.isObject() && patch.isObject()) {
merge((ObjectNode) source, (ObjectNode) patch);
Expand Down Expand Up @@ -107,19 +104,6 @@ private static void merge(final ObjectNode source, final ObjectNode patch) {
}
}

private static void removeDeletedFields(final ObjectNode source, final ObjectNode patch) {
final Iterator<String> fieldNames = source.fieldNames();
while (fieldNames.hasNext()) {
final String field = fieldNames.next();
final JsonNode patchValue = patch.get(field);

// If the patch value is null then the field has been deleted, remove from source
if (isNullOrMissing(patchValue)) {
source.remove(field);
}
}
}

/**
* Creates a tree that can be merged into a JsonNode. Does the following:
* 1. removes nulls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,11 @@ private static void iterateOverAllUnvisitedFields(JsonNode local, JsonNode cloud
final JsonNode cloudValue = cloud.get(field);
final JsonNode baseValue = base.get(field);
JsonNode mergedResult = getMergedNode(localValue, cloudValue, baseValue, owner);
if (mergedResult != null) {
((ObjectNode) result).set(field, mergedResult);
((ObjectNode) result).set(field, mergedResult);
if (mergedResult == null) {
visited.add(field);
}
visited.add(field);
//visited.add(field);
}
}

Expand Down

0 comments on commit c7c8290

Please sign in to comment.