question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stopping diff to go one level deep

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
bartoszwalacikcommented, 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

0reactions
bartoszwalacikcommented, May 9, 2018

fixed in release 3.9.5

Read more comments on GitHub >

github_iconTop Results From Across the Web

Clostridium Difficile (C. diff) Symptoms - WebMD
If you're taking antibiotics and develop a serious case of diarrhea, you could have a bacterial infection known as C. diff.
Read more >
C. difficile infection - Symptoms and causes - Mayo Clinic
Symptoms can range from diarrhea to life-threatening damage to the colon. The bacterium is often referred to as C. difficile or C. diff....
Read more >
Prevent the Spread of C. diff - CDC
If you have C. diff and don't wash your hands with soap and water, you can spread it. Take these steps to make...
Read more >
4 Steps to Stop a Cut From Bleeding - Cleveland Clinic
How to Stop Bleeding in Small and Deep Cuts · 1. Apply pressure. Place clean gauze or cloth on the wound and apply...
Read more >
gitignore Documentation - Git
The purpose of gitignore files is to ensure that certain files not tracked by Git remain untracked. To stop tracking a file that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found