Skip to content

chore: #2058 Override hashCode in StringToStringsMap to mirror equals - #2068

Closed
Heyy-Himanshuu wants to merge 2 commits into
microcks:masterfrom
Heyy-Himanshuu:chore/2058-stringtostringsmap-hashcode
Closed

Heyy-Himanshuu wants to merge 2 commits into
microcks:masterfrom
Heyy-Himanshuu:chore/2058-stringtostringsmap-hashcode

Conversation

@Heyy-Himanshuu

Copy link
Copy Markdown
Contributor

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.

Description

  • Added hashCode() override that mirrors the equals mode:
    • When equalsOnThis = true: return System.identityHashCode(this) so the hash is consistent with reference equality.
    • When equalsOnThis = false: delegate to super.hashCode() to stay consistent with the inherited content equality.
  • Added the missing @Override annotation on the existing equals method so the pair is explicit.
  • mvn spotless:check -pl webapp passes locally.

Related issue(s)

See also #2058

…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]>
@Vaishnav88sk

Copy link
Copy Markdown
Contributor

The identityHashCode approach is correct for the equalsOnThis = true mode, but is there actually a test that puts a StringToStringsMap with equalsOnThis = true into a HashMap or HashSet and verifies the contract holds? Without that, this is just Sonar-silencing, the broken behavior that originally existed would still be easy to reintroduce. What are your thoughts?

@Heyy-Himanshuu

Copy link
Copy Markdown
Contributor Author

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]>
@Heyy-Himanshuu

Copy link
Copy Markdown
Contributor Author

@Vaishnav88sk added. Have a look and let me know if any changes are required.

@lbroudoux

Copy link
Copy Markdown
Member

We have to be careful here as the StringToStringsMap is actually a port from the original SoapUI library to avoid us the pain of pulling this outdated lib. As a consequence, even if incorrect or fragile, we should keep it "as is" to avoid differences from the behaviour people can see in SoapUI. We may check the latest version of this class (don't know where is the original but there are some copies around there like in https://github.com/oysteing/soapui/blob/master/src/java/com/eviware/soapui/support/types/StringToStringsMap.java) to see how it evolves and then it may be better to ignore this issue at the Sonar level... Let's check.

@Heyy-Himanshuu

Copy link
Copy Markdown
Contributor Author

@lbroudoux good call! checked the linked SoapUI source and confirmed: SoapUI's upstream StringToStringsMap has the same equalsOnThis flag and equals override, and does not override hashCode either. So Microcks's current behavior already matches SoapUI exactly. Merging this PR would introduce a behavioral divergence between Microcks-hosted SoapUI scripts and real SoapUI, which defeats the purpose of the port.

Closing this PR. Will follow up with a small PR that suppresses the Sonar finding at the source (e.g. @SuppressWarnings("java:S1206") with a comment pointing to the SoapUI port rationale) so future contributors don't keep tripping over it.

@Heyy-Himanshuu
Heyy-Himanshuu deleted the chore/2058-stringtostringsmap-hashcode branch May 4, 2026 15:01
lbroudoux pushed a commit that referenced this pull request May 6, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants