Dmytro Hrankin
Dmytro Hrankin

Reputation: 105

Javers returns incomplete diff when entity field type is changed (inheritance)

Javers returns incomplete diff when a field of an entity is declared using an abstract type.

I am using Javers 2.9.2, but also tried 5.4.0. My issue appeared in both versions.

I have a model like the following:

// An entity.
class Entity {

  AbstractType field;
}

abstract class AbstractType {}

// A value object.
class ConcreteA extends AbstractType {

  AnotherEntity entityA;
}

// A value object.
class ConcreteB extends AbstractType {

  AnotherEntity entityB;

  // Other fields are omitted for simplicity.
}

// The class registered as an entity.
class AnotherEntity {

  String uuid;
  String name;
}

I am registering the entities and value objects above.

Comparing the following objects:

AnotherEntity anotherEntity = new AnotherEntity("name");

Entity originalEntity = new Entity();
originalEntity.field = new ConcreteA(anotherEntity);

Entity updatedEntity = new Entity();
updatedEntity.field = new ConcreteB(anotherEntity);

javers.compare(originalEntity, updatedEntity);

I expect the diff saying that:

But instead, the diff says that only field entityA was removed (ReferenceChange). So, one field is missing in the diff.

How can I get the complete diff for my case?

Upvotes: 1

Views: 906

Answers (1)

Bartek Walacik
Bartek Walacik

Reputation: 3496

In 5.5.0 we have added better support for types refactoring. Added/removed properties are detected and Javers calculates correct diff for them. Each PropertyChange has the new enum — PropertyChangeType indicating if a property is added/removed:

/**
 * When two objects being compared have different classes,
 * they can have different sets of properties.
 * <br/>
 * When both objects have the same class, all changes have PROPERTY_VALUE_CHANGED type.
 *
 * @since 5.5.0
 */
public enum PropertyChangeType {

    /**
     * When a property of the right object is absent in the left object.
     */
    PROPERTY_ADDED,

    /**
     * When a property of the left object is absent in the right object.
     */
    PROPERTY_REMOVED,

    /**
     * Regular value change &mdash; when a property is present in both objects.
     */
    PROPERTY_VALUE_CHANGED
}

PropertyChangeType is also reflected in diff.prettyPrint():

Diff diff = javers.compare(originalEntity, updatedEntity)

println diff.prettyPrint()
Diff:
* changes on org.javers.core.cases.Entity/123 :
  - 'field.entityA' property with reference '...AnotherEntity/uuid' removed
  - 'field.entityB' property with reference '...AnotherEntity/uuid' added

Upvotes: 1

Related Questions