Enforce non-null key in ValidatedKeyValue constructor#7578
Open
zbnerd wants to merge 1 commit into
Open
Conversation
The validator overload of KeyValue.of(...) accepted a null key and silently produced a half-broken object: getKey() returned null while equals/hashCode/toString still worked, and the failure only surfaced later in compareTo (via getKey().compareTo(...)). The package is @NullMarked, so key is non-null by contract. ImmutableKeyValue already enforces this with requireNonNull; ValidatedKeyValue did not. Add Objects.requireNonNull(key, ...) to the constructor to fail fast at construction time and align with the @NullMarked contract and the sibling ImmutableKeyValue behavior. The validator's role (deciding value nullability) is unchanged. Closes micrometer-metricsgh-7577 Signed-off-by: zbnerd <zbnerd@users.noreply.github.com>
edd4fd4 to
1b6b85c
Compare
Author
|
One thing that makes this look unintentional is the consistency of the other overloads. If the validator overload were intended to allow null keys whenever the predicate accepts the value, I would expect all key-accepting overloads to behave similarly. Instead:
So regardless of whether one views the predicate overload as a special case for value validation, the current behavior leaves a single entry point that handles null keys differently from every other overload. That asymmetry is what made me think this is more likely a missing null check than an intentional API distinction. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue #7577.
Summary
ValidatedKeyValuedid not enforce the package-level@NullMarkedcontract on itskeyparameter.As a result,
KeyValue.of(String, T, Predicate<? super T>)could create aKeyValueinstance withgetKey() == null. The object remained usable for most operations (equals,hashCode,toString, etc.) but later failed withNullPointerExceptionincompareTo.This change adds the missing null check and aligns
ValidatedKeyValuewithImmutableKeyValue, which already enforces non-null constructor arguments.Change
Tests
Added regression tests covering:
ValidatedKeyValueKeyValue.of(String, T, Predicate)KeyNamehandling through the corresponding overload