Repository navigation
chore: #2058 Override hashCode in StringToStringsMap to mirror equals - #2068
Heyy-Himanshuu wants to merge 2 commits into
Conversation
…r equals StringToStringsMap extends HashMap and overrides equals(Object) with a mode switch driven by the equalsOnThis flag - reference equality when true, otherwise the inherited HashMap content equality. hashCode() was never overridden though, so it always returned HashMap's content-based hash. Sonar flags this with rule java:S1206, and beyond the rule the behaviour is genuinely fragile: when equalsOnThis is true, two distinct instances with identical content satisfy hashCode() == hashCode() but equals() returns false, which produces surprising results inside hash-based collections. Add a hashCode() override that mirrors the equals mode: when equalsOnThis is true, return System.identityHashCode(this) so the hash is consistent with reference equality. When equalsOnThis is false, delegate to super.hashCode() to stay consistent with the inherited content equality. Also add the missing @OverRide annotation on the existing equals method so the pair is explicit. Signed-off-by: Git-HimanshuRathi <[email protected]>
|
The |
|
makes sense, the override alone is just Sonar-silencing without a regression test to lock the contract in. let me add test for it. |
…ontract Lock in the equals/hashCode mode switch with three JUnit cases so the override cannot be silently re-broken later: - testEqualsAndHashCodeAreContentBasedByDefault: with the default flag, two maps with identical content are equal, share a hashCode, and collapse into a single HashSet entry, matching the inherited HashMap behaviour. - testEqualsOnThisTrueUsesIdentitySemantics: with equalsOnThis true, two distinct instances with identical content are not equal, hashCode returns System.identityHashCode of the instance (asserted directly to avoid depending on identity-hash collision rates), and HashSet stores them as two separate entries instead of collapsing them under a content hash. - testEqualsOnThisCanBeToggledBackToContentMode: toggling the flag off restores the inherited content-based equals and hashCode, so the previous toggle does not leak state. Running these three cases against the pre-fix code (no hashCode override) would fail testEqualsOnThisTrueUsesIdentitySemantics on the identityHashCode assertion and on the HashSet-size check, since the inherited content hash would collide the two instances. Signed-off-by: Git-HimanshuRathi <[email protected]>
|
@Vaishnav88sk added. Have a look and let me know if any changes are required. |
|
We have to be careful here as the |
|
@lbroudoux good call! checked the linked SoapUI source and confirmed: SoapUI's upstream Closing this PR. Will follow up with a small PR that suppresses the Sonar finding at the source (e.g. |
…2083) StringToStringsMap is a manual port of com.eviware.soapui.support.types.StringToStringsMap, kept in the tree so SoapUI scripts hosted by Microcks behave the same way as in real SoapUI without us having to depend on the outdated SoapUI library. The upstream class (e.g. https://github.com/oysteing/soapui/blob/ master/src/java/com/eviware/soapui/support/types/StringToStringsMap.java) overrides equals(Object) with a mode switch driven by an equalsOnThis flag and does not override hashCode, so the equals/hashCode contract is technically violated when equalsOnThis is true. That violation is exactly what Sonar's java:S1206 flags here. Replacing #2068, which tried to fix the contract by overriding hashCode. As @lbroudoux pointed out on that PR, fixing it would make Microcks-hosted SoapUI scripts diverge from real SoapUI - the opposite of this file's stated purpose. The right move is to suppress the Sonar finding with explanation, not to fix the code. Concretely: - Add @SuppressWarnings("java:S1206") to the existing equals override along with an inline comment that points future readers to the SoapUI port rationale. - Strengthen the class Javadoc with an explicit warning: any apparent bug or fragility here should first be checked against the SoapUI source, and if the upstream behaves the same way the finding should be suppressed rather than fixed. No behavioural change. Signed-off-by: Git-HimanshuRathi <[email protected]>
StringToStringsMapextendsHashMapand overridesequals(Object)with a mode switch driven by theequalsOnThisflag reference equality whentrue, otherwise the inheritedHashMapcontent equality.hashCode()was never overridden though, so it always returned HashMap's content-based hash.Sonar flags this with rule
java:S1206, and beyond the rule the behaviour is genuinely fragile: whenequalsOnThisistrue, two distinct instances with identical content satisfyhashCode() == hashCode()butequals()returnsfalse, which produces surprising results inside hash-based collections.Description
hashCode()override that mirrors the equals mode:equalsOnThis = true: returnSystem.identityHashCode(this)so the hash is consistent with reference equality.equalsOnThis = false: delegate tosuper.hashCode()to stay consistent with the inherited content equality.@Overrideannotation on the existingequalsmethod so the pair is explicit.mvn spotless:check -pl webapppasses locally.Related issue(s)
See also #2058