New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stopping diff to go one level deep #669

Closed
tarunlalwani opened this Issue May 2, 2018 · 6 comments

Comments

Projects
None yet
2 participants
@tarunlalwani

tarunlalwani commented May 2, 2018

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

@bartoszwalacik

This comment has been minimized.

Member

bartoszwalacik commented May 2, 2018

That's how Javers works. You can use @ShallowReference to limit the diff scope or other annotations described here
https://javers.org/documentation/domain-configuration/#ignoring-things

@tarunlalwani

This comment has been minimized.

tarunlalwani commented May 2, 2018

@bartoszwalacik , thanks! Really appreciate the response

@bartoszwalacik bartoszwalacik reopened this May 2, 2018

@bartoszwalacik

This comment has been minimized.

Member

bartoszwalacik commented May 2, 2018

Okay, looks like that you can't compare Value Objects in List wit AS_SET algorithm.

Normally, when a Value Object is in Set, its Id is generated using ObjectHasher, for example:

org.javers.core.cases.CaseListOfValueObjects$TopLevelClass/#items/4515e04193acc0f8bff590e9c0fd5837

If Value Object is in List, its Id is based on its index in this List, for example:

org.javers.core.cases.CaseListOfValueObjects$TopLevelClass/#items/0

When you AS_SET is chosen, Id should be generated using ObjectHasher for both Set and List containers

@tarunlalwani

This comment has been minimized.

tarunlalwani commented May 2, 2018

So pottenitaly a bug that you would fix?

@bartoszwalacik

This comment has been minimized.

Member

bartoszwalacik commented May 2, 2018

yes

bartoszwalacik added a commit that referenced this issue May 9, 2018

#669
fix for comparing VO in List with AS_SET

bartoszwalacik added a commit that referenced this issue May 9, 2018

@bartoszwalacik bartoszwalacik added the fixed label May 9, 2018

@bartoszwalacik

This comment has been minimized.

Member

bartoszwalacik commented May 9, 2018

fixed in release 3.9.5

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