Skip to content
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
Closed

Stopping diff to go one level deep #669

tarunlalwani opened this issue May 2, 2018 · 6 comments
Labels

Comments

@tarunlalwani
Copy link

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
Copy link
Member

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
Copy link
Author

@bartoszwalacik , thanks! Really appreciate the response

@bartoszwalacik bartoszwalacik reopened this May 2, 2018
@bartoszwalacik
Copy link
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
Copy link
Author

So pottenitaly a bug that you would fix?

@bartoszwalacik
Copy link
Member

yes

bartoszwalacik added a commit that referenced this issue May 9, 2018
fix for comparing VO in List with AS_SET
@bartoszwalacik
Copy link
Member

fixed in release 3.9.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants