Skip to content

[Feature] When using java.net.URL as part of the Equal or HashCode properties, warn and/or use #1203

@borjab

Description

@borjab

Describe the feature

First, Kudos for this great project. I have discovered a weird and painful issue in the one of our equality class. When doing this in Kotlin ( would work the same on Java with auto-generated equals):

data class Image(val url: URL, val position: Int)

Everything seems good even with the tests:

funfulfills equal contract() { EqualsVerifier.forClass(Image::class.java).verify() }

Debugging an issue with Claude it discovered why other behaviors were flaky: it was using the DNS to check the IPs. This meant that if the network glitched equality could do weird things.

@Test
fun `images with different URL strings are not equal`() {
    // URL.equals() resolves hostnames via DNS before comparing.
    // "localhost" and "127.0.0.1" resolve to the same IP, so URL.equals()
    // treats them as equal even though their string representations differ.
    // This proves that Image equality is DNS-driven, not URL-string-driven.
    val imageWithHostname = Image(URI("http://localhost/image.jpg").toURL(), 1)
    val imageWithIp = Image(URI("http://127.0.0.1/image.jpg").toURL(), 1)

    assertThat(imageWithHostname.url.toString()).isNotEqualTo(imageWithIp.url.toString())
    assertThat(imageWithHostname).isNotEqualTo(imageWithIp) // fails: URL.equals() uses DNS
}

EqualsVerifier was working as expected in general. But the network call for to URL inside can fail and have random, non-deterministic behavior.

Describe the solution you'd like

When generating cases to validate equals, it could fail saying that it is not deterministic.

And, probably, this mean sallowing the removal of this with_

EqualsVerifier.forClass(Image.class)
    .suppress(Warning.URL_SHOULD_NOT_BE_USED_IN_EQUALS)
    .verify();

Describe alternatives

A WARN log could be enough in some cases

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions