Skip to content

chore: #2058 Override equals/hashCode/toString in ResponseResult - #2065

Merged
lbroudoux merged 1 commit into
microcks:masterfrom
Heyy-Himanshuu:chore/2058-response-result-equals-hashcode
May 4, 2026
Merged

lbroudoux merged 1 commit into
microcks:masterfrom
Heyy-Himanshuu:chore/2058-response-result-equals-hashcode

Conversation

@Heyy-Himanshuu

Copy link
Copy Markdown
Contributor

ResponseResult is a Java record with a byte[] content field. Java's auto-generated record accessors use reference semantics on arrays — two records with byte-for-byte identical content are not .equals() to each other, their hashCode() values diverge, and toString() prints content=[B@1a2b. Sonar flags this with rule java:S6218 ("override equals/hashCode/toString to consider array's content").

Description

  • equals: compare status and headers via Objects.equals and content via Arrays.equals, so two records with identical bytes compare equal.
  • hashCode: combine Objects.hash(status, headers) with Arrays.hashCode(content), so equal records share a hashCode.
  • toString: print the content as byte[<length>] rather than dumping the whole array (which may be megabytes for binary responses) or emitting the unhelpful default [B@hashref.
  • No behavioural change for callers that don't use equality, hashing, or toString on ResponseResult; strict improvement everywhere else.
  • mvn spotless:check -pl webapp passes locally.

Related issue(s)

See also #2058

ResponseResult is a Java record carrying a 'byte[] content' field.
Java's auto-generated record accessors use reference semantics on
arrays, which means two ResponseResult values with byte-for-byte
identical content are NOT .equals() to each other, their hashCode()
values diverge, and toString() prints something like 'content=[B@1a2b'.
Sonar flags this with the 'override equals/hashCode/toString to
consider array's content' rule.

- equals: compare status and headers via Objects.equals and content
  via Arrays.equals so two records with identical bytes compare equal.
- hashCode: combine Objects.hash(status, headers) with
  Arrays.hashCode(content) so equal records share a hashCode.
- toString: print the content as 'byte[<length>]' instead of dumping
  the whole array (which can be megabytes for binary responses) or
  emitting the unhelpful default '[B@hashref'.

No behavioural change for callers that don't use equality, hashing,
or toString on ResponseResult; strict improvement everywhere else.

Signed-off-by: Git-HimanshuRathi <[email protected]>
@lbroudoux

Copy link
Copy Markdown
Member

That's a really good catch! Thanks for spending time on this!

@lbroudoux lbroudoux left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lbroudoux lbroudoux added kind/task java Pull requests that update Java code labels May 4, 2026
@lbroudoux lbroudoux added this to the 1.15.0 milestone May 4, 2026
@lbroudoux
lbroudoux merged commit d62c029 into microcks:master May 4, 2026
14 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

java Pull requests that update Java code kind/task

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants