Skip to content

Stopping diff to go one level deep #669

Description

@tarunlalwani

I have a normal program which does below

package com.javerstest;

import com.google.common.collect.ImmutableList;
import org.javers.core.Javers;
import org.javers.core.JaversBuilder;
import org.javers.core.diff.Diff;
import org.javers.core.diff.ListCompareAlgorithm;

import java.util.List;

public class App {


    public static void main(String[] args) {

        List<ListItem> list1 = ImmutableList.of(
                ListItem.builder()
                        .itemName("item1")
                        .itemValue("value")
                        .build(),
                ListItem.builder()
                        .itemName("item2")
                        .itemValue("value2")
                        .build()
        );

        List<ListItem> list2 = ImmutableList.of(
                ListItem.builder()
                        .itemName("item2")
                        .itemValue("value2change")
                        .build(),
                ListItem.builder()
                        .itemName("item1")
                        .itemValue("value")
                        .build(),
                ListItem.builder()
                        .itemName("item3")
                        .itemValue("value3")
                        .build()

        );

        TopLevelClass tlc1 = TopLevelClass.builder().items(list1).build();
        TopLevelClass tlc2 = TopLevelClass.builder().items(list2).build();

        Javers jvc = JaversBuilder.javers().withListCompareAlgorithm(ListCompareAlgorithm.AS_SET)
                .build();
        Diff diffTlc = jvc.compare(tlc1, tlc2);
        System.out.println(diffTlc);
    }
}
package com.javerstest;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

@Builder
@Getter
@Setter
@EqualsAndHashCode
public class ListItem {
    private String itemName;
    private String itemValue;
}
package com.javerstest;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.javers.core.metamodel.annotation.IgnoreDeclaredProperties;
import org.javers.core.metamodel.annotation.ShallowReference;

import java.util.List;
import java.util.Set;


@Builder
@Getter
@Setter
@EqualsAndHashCode
public class TopLevelClass {

    @ShallowReference
    List<ListItem> items;
}

Below is the repo if you need to have a look

https://github.com/tarunlalwani/javers-diff-issue

Now when you run the diff is like below

* new object: com.javerstest.TopLevelClass/#items/2
* changes on com.javerstest.TopLevelClass/ :
  - 'items' collection changes :
    . 'com.javerstest.ListItem@306a04bf' removed
    . 'com.javerstest.ListItem@306a04fb' added
    . 'com.javerstest.ListItem@29f62baf' added
  - 'items/0.itemName' changed from 'item1' to 'item2'
  - 'items/0.itemValue' changed from 'value' to 'value2change'
  - 'items/1.itemName' changed from 'item2' to 'item1'
  - 'items/1.itemValue' changed from 'value2' to 'value'

All the changes are actually covered by below

  - 'items' collection changes :
    . 'com.javerstest.ListItem@306a04bf' removed
    . 'com.javerstest.ListItem@306a04fb' added
    . 'com.javerstest.ListItem@29f62baf' added

But javers still goes one level deep again in the list and does another comparison and adds below

  - 'items/0.itemName' changed from 'item1' to 'item2'
  - 'items/0.itemValue' changed from 'value' to 'value2change'
  - 'items/1.itemName' changed from 'item2' to 'item1'
  - 'items/1.itemValue' changed from 'value2' to 'value'

Bug/feature/limitation? Need to understand. If the list has been already compared, I don't want it to go one level deep again and of course @ShallowReference doesn't help

Activity

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