Skip to content

[kubernetes] Fix Quantity.hashCode to honor equals/hashCode contract - #4953

Draft
yue9944882 wants to merge 1 commit into
kubernetes-client:masterfrom
yue9944882:fix/core-npe
Draft

yue9944882 wants to merge 1 commit into
kubernetes-client:masterfrom
yue9944882:fix/core-npe

Conversation

@yue9944882

Copy link
Copy Markdown
Member

Bug

io.kubernetes.client.custom.Quantity violates the Object.equals/hashCode contract.

  • equals() compares the underlying BigDecimal by value (ObjectUtils.compare → BigDecimal.compareTo, which is scale-insensitive).
  • hashCode() used Objects.hash(number), i.e. BigDecimal.hashCode(), which is scale-sensitive.

So value-equal quantities with different scales are equals() but have different hash codes. Example via the public API:

Quantity a = Quantity.fromString("1");      // scale 0
Quantity b = Quantity.fromString("1000m");  // 1000 * 10^-3 = 1.000, scale 3
a.equals(b);          // true
a.hashCode() == b.hashCode(); // false  <-- contract violation

This breaks HashMap/HashSet membership for Quantity keys (a value inserted as 1000m is not found when looked up as 1).

Fix

One line: normalize the scale with BigDecimal.stripTrailingZeros() before hashing, making the hash consistent with the value-based equals(). Null-guarded. Behavior-preserving otherwise.

Verification

Added a focused unit test QuantityTest.equalQuantitiesWithDifferentScaleHaveEqualHashCode.

  • Before fix: FAILS — expected: <62> but was: <31034>.
  • After fix: PASSES.
  • No regressions: QuantityTest (2), QuantityFormatterTest (20), SuffixFormatterTest (16) all green.
./mvnw -pl kubernetes -am test -Dtest=QuantityTest

@kubernetes-prow kubernetes-prow Bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Oct 22, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: yue9944882

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow kubernetes-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 22, 2026
Quantity.equals compares the underlying BigDecimal by value
(ObjectUtils.compare -> BigDecimal.compareTo, scale-insensitive), but
hashCode used Objects.hash(number) which is scale-sensitive. Value-equal
quantities such as "1" and "1000m" (1.000) are therefore equal yet
produce different hash codes, breaking HashMap/HashSet lookups.

Normalize the scale with BigDecimal.stripTrailingZeros() before hashing
so the hash is consistent with the value-based equals.

Adds a focused unit test (fails before, passes after).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant