GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Recursive comparison uses `equals` on root object when `useOverriddenEquals` is enabled · Issue #3320 · assertj/assertj

Recursive comparison uses equals on root object when useOverriddenEquals is enabled #3320

New issue
Copy link
New issue
Copy link
Closed
🐛 Regression
Closed
Recursive comparison uses equals on root object when useOverriddenEquals is enabled#3320
🐛 Regression
Copy link
Assignees
joel-costigliola
Labels
theme: recursive comparisonAn issue related to the recursive comparison
Milestone
3.25.2

Description

@austinarbor-wk
austinarbor-wk
opened on Jan 2, 2024
Issue body actions

Describe the bug
It appears that 3.25.0 has changed behavior of recursive comparison

  • assertj core version: 3.25.0
  • java version: 17
  • test framework version: junit platform 5.10.1

The below test succeeds in 3.24.2 but fails in 3.25.0

  @Test
  void testRecursiveCompare() {
    var a1 = new A();
    a1.name = "test1";
    var b1 = new B();
    a1.b = b1;
    b1.name = "test2";

    var a2 = new A();
    a2.name = "test1";
    a2.b = b1;

    var list1 = List.of(a1);
    var list2 = List.of(a2);

    var cfg = RecursiveComparisonConfiguration.builder().withComparedFields("name", "b").build();
    cfg.useOverriddenEquals();

    assertThat(list1)
        .usingRecursiveFieldByFieldElementComparator(cfg)
        .containsExactlyElementsOf(list2);
  }

  private static class A {
    private String name;

    private String guid = UUID.randomUUID().toString();

    private B b;

    @Override
    public boolean equals(Object o) {
      if (this == o) {
        return true;
      }
      if (!(o instanceof A a)) {
        return false;
      }
      return Objects.equals(name, a.name) && Objects.equals(guid, a.guid) && Objects.equals(b, a.b);
    }

    @Override
    public int hashCode() {
      return Objects.hash(name, guid, b);
    }
  }

  private static class B {
    private String name;

    private String guid = UUID.randomUUID().toString();

    @Override
    public boolean equals(Object o) {
      if (this == o) {
        return true;
      }
      if (!(o instanceof B b)) {
        return false;
      }
      return Objects.equals(name, b.name) && Objects.equals(guid, b.guid);
    }
  }

Activity

  1. scordio
    added
    theme: recursive comparisonAn issue related to the recursive comparison
    type: regression
    on Jan 2, 2024
  2. scordio commented on Jan 2, 2024

    @scordio
    scordio
    on Jan 2, 2024
    Member
    More actions

    Thanks for reporting it, @austinarbor-wk!

    Reacted by austinarbor-wk
  3. scordio
    added this to the 3.25.2 milestone on Jan 2, 2024
  4. scordio commented on Jan 7, 2024

    @scordio
    scordio
    on Jan 7, 2024
    Member
    More actions

    Possibly related to 0eb44cc and still unsure if the behavior in version 3.24.2 was correct, though.

    What should be the expected behavior when withComparedFields("name", "b") and useOverriddenEquals() are used together, and the overridden equals compares also guid?

    I feel useOverriddenEquals() should win on top of any withComparedFields(...) config, like the behavior of 3.25. Maybe we should prevent such config combinations or fail fast with some IllegalStateException.

    Any thoughts @joel-costigliola?

  5. austinarbor-wk commented on Jan 8, 2024

    @austinarbor-wk
    austinarbor-wk
    on Jan 8, 2024
    Author
    More actions

    I think my expectation from the original code sample above based on the javadoc is that the comparison would be equivalent to the compound assertion of

    assertThat(a1.name).isEqualTo(a2.name)
    assertThat(a1.b).isEqualTo(a2.b)

    Is my understanding incorrect?

  6. scordio commented on Jan 8, 2024

    @scordio
    scordio
    on Jan 8, 2024
    Member
    More actions

    Do I understand correctly that you'd like to use the overridden equals only for B but not for A?

  7. joel-costigliola commented on Jan 8, 2024

    @joel-costigliola
    joel-costigliola
    on Jan 8, 2024
    Member
    More actions

    I would expect both compared fields to be compared with equals, compared fields limits the comparison to the specified fields and useOverriddenEquals() instructs the recursive comparison to compare them with their equals method.

    I'll have a look at the issue this week.

    Reacted by Stefano Cordio and Austin Arbor
  8. joel-costigliola
    self-assigned this
    on Jan 8, 2024
  9. austinarbor-wk commented on Jan 8, 2024

    @austinarbor-wk
    austinarbor-wk
    on Jan 8, 2024
    Author
    More actions

    My expectation is how @joel-costigliola describes

  10. joel-costigliola commented on Jan 9, 2024

    @joel-costigliola
    joel-costigliola
    on Jan 9, 2024
    Member
    More actions

    Alright, the issue is that the code that decides whether to use the overridden equals or not does not honor the compared fields, in the given example test it decides to compare A and B with equals instead of only comparing the name and b fields

  11. joel-costigliola
    added
    type: bug
    on Jan 9, 2024
  12. joel-costigliola
    closed this as completedin 7f786bdon Jan 17, 2024
  13. scordio
    changed the title [-]Regression in Recursive Comparison[/-] [+]Recursive comparison uses `equals` on root object when `useOverriddenEquals` is enabled[/+] on Jan 18, 2024
  14. scordio
    removed
    type: bug
    on Jan 18, 2024
  15. schlosna commented on Jan 19, 2024

    @schlosna
    schlosna
    on Jan 19, 2024
    Contributor
    More actions

    Thank you for the assertj project, it makes reading & writing tests enjoyable.

    I am curious if there is a timeline for 3.25.2 release with the fix for this issue. We have had to roll back a number of projects to 3.24.2 due to tests using recursive comparison timing out and failing.

    Thank you!

  16. scordio commented on Jan 19, 2024

    @scordio
    scordio
    on Jan 19, 2024
    Member
    More actions

    @schlosna yes, we plan to release 3.25.2 later this month, as soon as we finish looking into its remaining issues.

    Reacted by Joel Costigliola, David Schlosnagle and Andrew Ash
    Reacted by David Schlosnagle
  17. schlosna commented on Jan 24, 2024

    @schlosna
    schlosna
    on Jan 24, 2024
    Contributor
    More actions

    Thank you for the 3.25.2 release! I ran some quick tests using 3.25.2 and it has resolved the issues we were encountering with usingRecursiveComparison.

    Reacted by Stefano Cordio and Joel Costigliola
  18. austinarbor-wk commented on Jan 24, 2024

    @austinarbor-wk
    austinarbor-wk
    on Jan 24, 2024
    Author
    More actions

    Confirmed 3.25.2 resolves the issues we were experiencing as well, thanks!

    Reacted by Joel Costigliola
  19. joel-costigliola commented on Jan 24, 2024

    @joel-costigliola
    joel-costigliola
    on Jan 24, 2024
    Member
    More actions

    Thanks for the feedback, appreciated!

  20. ash211 commented on Jan 26, 2024

    @ash211
    ash211
    on Jan 26, 2024
    Contributor
    More actions

    @schlosna and I work together -- 3.25.2 solves most of the recursive comparison issues we had, but not all. I've identified a regression in comparing deeply-nested objects and opened a demonstrative test case at #3350.

    Thoughts on that PR would be much appreciated! Thanks again for assertj.

  21. schlosna
    added a commit that references this issue on Jan 26, 2024

    The recursive comparison should never use equals on root objects sinc…

    This issue will close once commit 7f786bd is merged into the 'main' branch.
    7f786bd
  22. scordio
    removed
    type: regression
    on Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @joel-costigliola
    joel-costigliola

Labels

theme: recursive comparisonAn issue related to the recursive comparison

Type

🐛 Regression

Projects

No projects

    Milestone

    • 3.25.2

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.