Skip to content

Commit

Permalink
Fix comparison of object-nodes with custom-equals method
Browse files Browse the repository at this point in the history
fixes #335
  • Loading branch information
Captain-P-Goldfish committed Jan 26, 2024
1 parent 7df05d9 commit b93e87a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,58 @@ public static boolean isEmpty(JsonNode jsonNode)
return jsonNode.size() == 0;
}

/**
* jackson is currently not supporting comparison of int and long-nodes even if the nodes do have the same
* values. for this reason we are overriding the comparison here in case for number nodes
*
* @return true if the list contains a node that is equal to given complexNode, false else
*/
public static boolean hasEqualObject(List<ObjectNode> originalObjects, ObjectNode complexNode)
{
return originalObjects.parallelStream().anyMatch(originalNode -> isEqual(originalNode, complexNode));
}

/**
* jackson is currently not supporting comparison of int and long-nodes even if the nodes do have the same
* values. for this reason we are overriding the comparison here in case for number nodes
*
* @return true if the list contains a node that is equal to given complexNode, false else
*/
public static boolean isEqual(JsonNode node1, JsonNode node2)
{
if (node1 == null && node2 == null)
{
return true;
}
else if (node1 == null || node2 == null)
{
return false;
}
return node1.equals((o1, o2) -> {
boolean isO1Null = o1 == null || o1.isNull();
boolean isO2Null = o2 == null || o2.isNull();
if (isO1Null && isO2Null)
{
return 0;
}
else if (isO1Null || isO2Null)
{
return 1;
}

if (o1.isNumber() && o2.isNumber())
{
return o1.longValue() == o2.longValue() ? 0 : 1;
}
if (o1.isNumber() && o2.isNumber())
{
return o1.longValue() == o2.longValue() ? 0 : 1;
}
else
{
return o1.equals(o2) ? 0 : 1;
}
}, node2);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -860,34 +860,12 @@ private boolean handleDirectMultiValuedComplexPathReference(ArrayNode multiValue
else if (!path.isWithFilter() || matchingComplexNodes.isEmpty())
{
multiValued.add(complexNode);
isResourceChanged = isResourceChanged || !hasEqualObject(originalNodes, complexNode);
isResourceChanged = isResourceChanged || !JsonHelper.hasEqualObject(originalNodes, complexNode);
}
}
return isResourceChanged;
}

/**
* jackson is failing on int and long comparison even if the nodes do have the same values. for this reason we
* are overriding the comparison here in case for number nodes
*
* @return true if the list contains a node that is equal to given complexNode, false else
*/
private boolean hasEqualObject(List<ObjectNode> originalObjects, ObjectNode complexNode)
{
return originalObjects.parallelStream().anyMatch(originalNode -> {
return originalNode.equals((o1, o2) -> {
if (o1.isNumber() && o2.isNumber())
{
return o1.longValue() == o2.longValue() ? 0 : 1;
}
else
{
return o1.equals(o2) ? 0 : 1;
}
}, complexNode);
});
}

/**
* this method will check if the current operation adds a new primary value and will set the original primary
* to false if such a value exists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private void checkResourceSchema(ResourceType resourceTypeObject, JsonNode resou
else
{
if (registeredResourceSchema != null && resourceSchema != null
&& !registeredResourceSchema.equals(resourceSchema))
&& !JsonHelper.isEqual(registeredResourceSchema, resourceSchema))
{
log.debug("Resource schema with id '{}' is already registered. The new instance is not equal to"
+ " the old schema document. The old document is being replaced by the new one",
Expand Down

0 comments on commit b93e87a

Please sign in to comment.