Skip to content

Javers finds differences in the object tree when the same instance of a nested object is shared between parent and child #1358

Closed

Description

@idegroot

Clear description of my expectations versus reality

I have 2 "complex" objects to compare to find if they changed. The object has a property and a list of items, an item can have the same property as the top-level object. In the old version the property VO's are all unique instances. In the new version I use the same instance of the property VO in both top-level object and list-item. 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 top-level object and list-item, not when 2 siblings share the same object. It also only happens when the list-item is indeed in a collection (set or list make no difference) not when it is a single property.

I would expect that compare would not find any differences as all properties of both objects are the same and even equals says they are the same.

Steps To Reproduce
I have a **runnable test case ** which isolates the bug and allows Javers Core Team to easily reproduce it. I have pushed this test case to my fork of this repository:

https://github.com/idegroot/javers/blob/master/javers-core/src/test/groovy/org/javers/core/cases/CaseSharedObjectUnsetWhenUsedInCollection.groovy

Javers' Version
7.3.7 with java 17.

Additional context
See also https://stackoverflow.com/questions/77935250

Activity

  1. bartoszwalacik commented on May 28, 2024

    @bartoszwalacik
    Member

    Hi, thanks for reporting, indeed, looks like a bug ...

  2. bartoszwalacik commented on Jun 4, 2024

    @bartoszwalacik
    Member

    Fixed in 7.4.4

  3. clarkhan commented on Jun 5, 2024

    @clarkhan
    Contributor

    After working with the latest code fixes, I've run into a similar issue where Javers considers properties unset even though their equals method returns true. This seems to occur when I work with properties that have deeper levels of nesting.

    To illustrate this problem more clearly, I have modified a test case that was initially provided by idegroot.
    Here is the link to the modified test case:
    https://github.com/clarkhan/javers/blob/test/shared_object_unset/javers-core/src/test/groovy/org/javers/core/cases/CaseSharedObjectUnsetWhenUsedInCollection.groovy

    Additionally, for ease of reference, I've included both the content of the test case and the test results below:

        @EqualsAndHashCode
        class DeepComplexProperty {
            String value
        }
    
        @EqualsAndHashCode
        class ComplexProperty {
            String value
            DeepComplexProperty deepProperty
        }
    
        @EqualsAndHashCode
        class ListItem {
            String name
            ComplexProperty complexProperty
        }
    
        @EqualsAndHashCode
        class TopLevelClass {
            ComplexProperty complexProperty
            List<ListItem> items
        }
    
        def "should compare shared nested property on fields"() {
            given:
            def javers = JaversBuilder.javers().build()
    
            def l1 = new TopLevelClass(
                    complexProperty: new ComplexProperty(value: "value1", deepProperty: new DeepComplexProperty(value: "value2")),
                    items: [
                            new ListItem(name: "name1", complexProperty: new ComplexProperty(value: "value1", deepProperty: new DeepComplexProperty(value: "value2")))
                    ])
    
            def complexProperty = new ComplexProperty(value: "value1", deepProperty: new DeepComplexProperty(value: "value2"))
            def l2 = new TopLevelClass(
                    complexProperty: complexProperty,
                    items: [
                            new ListItem(name: "name1", complexProperty: complexProperty)
                    ])
    
            when:
            def diff = javers.compare(l1, l2)
    
            then:
            l1.equals(l2)
            diff.changes.isEmpty()
        }

    Test Results:

    diff.changes.isEmpty()
    |    |       |
    |    |       false
    |    [TerminalValueChange{ property: 'value', left:'value2',  right:'' }]
    Diff:
    * changes on org.javers.core.cases.CaseSharedObjectUnsetWhenUsedInCollection$TopLevelClass/ :
      - 'items/0/complexProperty/deepProperty.value' value 'value2' unset
    
  4. clarkhan commented on Jun 5, 2024

    @clarkhan
    Contributor

    As an additional note, I wanted to mention that I temporarily bypassed the issue by commenting out a section of code in the org.javers.core.graph.EdgeBuilder#buildNodeStubOrReuse method. After making this change, the test case passed. Here's the code snippet I commented out for temporary verification purposes:

        private LiveNode buildNodeStubOrReuse(LiveCdo cdo){
            if (nodeReuser.isReusable(cdo)){
                return nodeReuser.getForReuse(cdo);
            }
            else {
    //            if (nodeReuser.isTraversed(cdo)) {
    //                return buildNodeDouble(nodeReuser.getForDouble(cdo), cdo);
    //            }
                return buildNodeStub(cdo);
            }
        }
  5. bartoszwalacik commented on Jun 5, 2024

    @bartoszwalacik
    Member

    thanks for reporting @clarkhan , I will take a look on it

  6. bartoszwalacik commented on Jun 5, 2024

    @bartoszwalacik
    Member

    okay, I need to drop this node double concept and implement some cycle detection algo

  7. added a commit that references this issue on Jun 6, 2024
  8. added a commit that references this issue on Jun 6, 2024
  9. bartoszwalacik commented on Jun 7, 2024

    @bartoszwalacik
    Member

    Fixed in 7.4.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions