We have 2 "complex" objects to compare to find if they changed. In the old version the nested properties are all unique instances. In the new version we use the same instance in both parent and child. When comparing the objects with old.equals(new) they are considered equal. When comparing with javers.compare(old, new) differences are found. This only happens when the shared property is shared between parent and child, not when 2 siblings share the same object. It also only happens when child is in a collection (set or list make no difference) not when it is a single property.
We are using javers version 7.3.7 with java 17.
We expected that compare would not find any differences as all properties of both objects are the same and even equals says they are the same. I created a minimal unit-test to show the problem.
package exampletest.javers;
import org.javers.core.JaversBuilder;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
class JaversComparatorTest {
@Test
void objectsShouldBeEqual() {
var javers = JaversBuilder.javers().build();
var original = new TopLevel(List.of(new Child(new ComplexProperty("complexPropertyValue"))), new ComplexProperty("complexPropertyValue"));
var sharedComplexProperty = new ComplexProperty("complexPropertyValue");
var updated = new TopLevel(List.of(new Child(sharedComplexProperty)), sharedComplexProperty);
assertThat(original).isEqualTo(updated);
assertThat(javers.compare(original, updated).getChanges()).isEmpty();
}
record TopLevel(List<Child> children, ComplexProperty complexProperty) {
}
record Child(ComplexProperty complexProperty) {
}
record ComplexProperty(String complexProperty) {
}
}
The result of the test:
java.lang.AssertionError:
Expecting empty but was: [TerminalValueChange{ property: 'complexProperty', left:'complexPropertyValue', right:'' }]