question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DSN Object Equals Method Broken

See original GitHub issue

The 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:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
ninniuzcommented, Apr 3, 2019

@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 is null or empty (https://github.com/getsentry/sentry-java/blob/master/sentry/src/main/java/io/sentry/util/Util.java#L26). Maybe something like Util.equals(Object a, Object b)?

0reactions
justacodefancommented, May 24, 2019

Closing as the PR is merged.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found