Skip to content

Commit

Permalink
Fix .equals() logic in Profile class
Browse files Browse the repository at this point in the history
Summary: The existing `.equals()` method was throwing null pointer exceptions if certain fields of the `Profile` were null (which they are allowed to be, since we have explicitly specified that they are `nullable`). Fix this logic and add a sanity check to confirm that `.equals()` is indeed commutative.

Reviewed By: YOUDAN

Differential Revision: D24284819

fbshipit-source-id: 4efc3d3ac3228f3251a2e3d2c8e44a7f4fb93260
  • Loading branch information
jvlyn authored and facebook-github-bot committed Oct 14, 2020
1 parent ec5d83d commit a279b56
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
17 changes: 6 additions & 11 deletions facebook-core/src/main/java/com/facebook/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,12 @@ public boolean equals(Object other) {

Profile o = (Profile) other;

return id.equals(o.id) && firstName == null
? o.firstName == null
: firstName.equals(o.firstName) && middleName == null
? o.middleName == null
: middleName.equals(o.middleName) && lastName == null
? o.lastName == null
: lastName.equals(o.lastName) && name == null
? o.name == null
: name.equals(o.name) && linkUri == null
? o.linkUri == null
: linkUri.equals(o.linkUri);
return (id == null ? o.id == null : id.equals(o.id))
&& (firstName == null ? o.firstName == null : firstName.equals(o.firstName))
&& (middleName == null ? o.middleName == null : middleName.equals(o.middleName))
&& (lastName == null ? o.lastName == null : lastName.equals(o.lastName))
&& (name == null ? o.name == null : name.equals(o.name))
&& (linkUri == null ? o.linkUri == null : linkUri.equals(o.linkUri));
}

@Override
Expand Down
1 change: 1 addition & 0 deletions facebook/src/test/java/com/facebook/ProfileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public void testEquals() {

Profile profile3 = createMostlyNullsProfile();
assertNotEquals(profile1, profile3);
assertNotEquals(profile3, profile1);
}

@Test
Expand Down

0 comments on commit a279b56

Please sign in to comment.