DSN Object Equals Method Broken
See original GitHub issueThe DSN Object’s equals method currently does not work as expected. Because the secret key is deprecated from the DSN, it is no longer required for a valid DSN object, however if the DSN is null, two equal DSN objects will throw a null pointer exception on the string comparison of these keys because null does not have an equals method. This can be resolved by using the Java standard library Objects.equals
method as shown below.
Broken Code Dsn.equals line 293
if (!secretKey.equals(dsn.secretKey)) {
return false;
}
Example
@Test(expected = NullPointerException::class)
fun selfContainedCompleteExample() {
val dsn1 = Dsn("http://publickey@host.com/0")
val dsn2 = Dsn("http://publickey@host.com/0")
assertThat(dsn1, equalTo(dsn2))
}
Solution
if (!Objects.equals(secretKey, dsn.secretKey)) {
return false;
}
Solution Proof
@Test
fun solutionProof() {
assertThat(true, equalTo(Objects.equals("test", "test")))
assertThat(true, equalTo(Objects.equals(null, null)))
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:6
Top Results From Across the Web
Overriding equals method doesn't work - Stack Overflow
I'm trying to use this class in an ArrayList<Event> events but I can't find a way to get events.contains("item") to work. I have...
Read more >Adhering to EQ_DOESNT_OVERRIDE_EQUALS ... - GitHub
The bug EQ_DOESNT_OVERRIDE_EQUALS is defined as: This class extends a class that defines an equals method and adds fields, but doesn't define an ......
Read more >apex - Why doesn't .equals() behave like a virtual method?
It's implemented by the ANY object internally, and this code is frustratingly broken. For example, String.toString() works fine most of the time ...
Read more >Writing an equals method - how hard can it be?
The reason why this test fails is that the equals method uses getClass() to verify if both objects belong to the same class...
Read more >EqualsIncompatibleType - Error Prone
This check detects circumstances where the equals method is called when the two objects in question can never be equal to each other....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@mayt017 I think either way would do ok (but you should probably wait for project owners to give advice). There is a
Util
class which is used to, among other things, check if a string isnull
or empty (https://github.com/getsentry/sentry-java/blob/master/sentry/src/main/java/io/sentry/util/Util.java#L26). Maybe something likeUtil.equals(Object a, Object b)
?Closing as the PR is merged.