Skip to content

Add option to ignore compared fields existence check#3836

Closed
kssumin wants to merge 1 commit intoassertj:mainfrom
kssumin:fix/Add-ignoreNonExistentFields-option
Closed

Add option to ignore compared fields existence check#3836
kssumin wants to merge 1 commit intoassertj:mainfrom
kssumin:fix/Add-ignoreNonExistentFields-option

Conversation

@kssumin
Copy link
Contributor

@kssumin kssumin commented May 17, 2025

This adds a new option (ignoringNonExistentFields) to recursive comparison that allows polymorphic objects with different field structures to be compared effectively. When enabled, the comparison will skip field existence validation, which is useful when comparing objects of different subtypes where some fields might only exist in certain implementations.

Overview

This PR adds a new feature to the recursive comparison functionality that allows ignoring non-existent field checks. This is particularly useful when comparing polymorphic objects where some fields might not exist in all subtypes.

Implementation Details

  • Added ignoreNonExistentFields boolean flag to RecursiveComparisonConfiguration
  • Added ignoringNonExistentFields() method to RecursiveComparisonAssert
  • Added appropriate builder methods and setters
  • Added early return in checkComparedFieldsExist when this option is enabled
  • Added corresponding toString/description methods to show when this option is active
  • Added unit tests to verify the behavior

Example Use Case

When comparing objects like:

class BaseClass {
    String commonField = "common";
}

class SubClass1 extends BaseClass {
    String subField1 = "sub1";
}

class SubClass2 extends BaseClass {
    // 'subField1' doesn't exist here
    String subField2 = "sub2";
}

Regular recursive comparison would fail due to field existence checks. With this new option:

SubClass1 actual = new SubClass1();
SubClass2 expected = new SubClass2();

// This will now pass
assertThat(actual)
    .usingRecursiveComparison()
    .ignoringNonExistentFields()
    .isEqualTo(expected);

Check List:

@joel-costigliola joel-costigliola added this to the 4.0.0-M2 milestone May 17, 2025
@joel-costigliola joel-costigliola self-requested a review May 17, 2025 21:31
@joel-costigliola
Copy link
Member

Thanks @kssumin, I will review your PR shortly !

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not completely sure what we are testing here, we check that the config has the expected ignoreNonExistentFields value but I don't get the point of checking int compare = configurableRecursiveFieldByFieldComparator.compare(actual, other);.

Instead of these two tests, could we add a test in RecursiveComparisonAssert_isEqualTo_comparingOnlyFields_Test and reproduce the test example in the initial issue #3806 (comment) ? if the implementation is correct, the test should pass

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could in addition also add a test with the code example from the javadoc (with BaseClass, Subclass1 and 2)

Thanks !

Copy link
Member

@joel-costigliola joel-costigliola May 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename to ignoringNonExistentComparedFields which is more accurate since it only impacts the compared fields declared by the user.

@joel-costigliola
Copy link
Member

Thanks @kssumin, first round of code review done.

@kssumin kssumin force-pushed the fix/Add-ignoreNonExistentFields-option branch from 28a29e6 to 204e149 Compare May 18, 2025 07:42
@kssumin
Copy link
Contributor Author

kssumin commented May 18, 2025

@joel-costigliola Thank you for your review and suggestions! I've made the following changes based on your feedback:

  1. Added test cases in RecursiveComparisonAssert_isEqualTo_comparingOnlyFields_Test that reproduce the example from issue Allow to ignore check on compared fields in the recursive comparison (initial issue description was: Problems on method 'usingRecursiveFieldByFieldElementComparatorOnFields') #3806
  2. Added another test with the BaseClass/SubClass example from the JavaDoc to verify the functionality
  3. Updated the method name as suggested to be more descriptive (ignoringNonExistentComparedFields)

I've maintained the test structure with GIVEN/WHEN/THEN pattern and made sure all tests verify both success and failure cases. Let me know if you'd like me to make any additional changes!

This adds a new feature to recursive comparison that allows ignoring
fields that don't exist in the actual object. This is particularly
useful when comparing polymorphic objects where certain fields might
only exist in some subtypes.

Fixes assertj#3806

Signed-off-by: Kim Sumin <ksoomin25@gmail.com>
@kssumin kssumin force-pushed the fix/Add-ignoreNonExistentFields-option branch from 204e149 to 0e7a6d5 Compare May 18, 2025 12:50
@joel-costigliola
Copy link
Member

Integrated thanks @kssumin !

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

Labels

None yet

Projects

None yet

2 participants

Comments