Make SanitizableData.key non-nullable

Closes gh-47220
This commit is contained in:
Moritz Halbritter 2025-09-16 08:39:08 +02:00
parent eeac38b70b
commit 641b72db65
3 changed files with 9 additions and 18 deletions

View File

@ -21,6 +21,7 @@ import java.util.Locale;
import org.jspecify.annotations.Nullable;
import org.springframework.core.env.PropertySource;
import org.springframework.util.Assert;
/**
* Value object that represents the data that can be used by a {@link SanitizingFunction}.
@ -38,7 +39,7 @@ public final class SanitizableData {
private final @Nullable PropertySource<?> propertySource;
private final @Nullable String key;
private final String key;
private @Nullable String lowerCaseKey;
@ -50,7 +51,8 @@ public final class SanitizableData {
* @param key the data key
* @param value the data value
*/
public SanitizableData(@Nullable PropertySource<?> propertySource, @Nullable String key, @Nullable Object value) {
public SanitizableData(@Nullable PropertySource<?> propertySource, String key, @Nullable Object value) {
Assert.notNull(key, "'key' must not be null");
this.propertySource = propertySource;
this.key = key;
this.value = value;
@ -69,7 +71,7 @@ public final class SanitizableData {
* Return the key of the data.
* @return the data key
*/
public @Nullable String getKey() {
public String getKey() {
return this.key;
}
@ -78,9 +80,9 @@ public final class SanitizableData {
* @return the key as a lowercase value
* @since 3.5.0
*/
public @Nullable String getLowerCaseKey() {
public String getLowerCaseKey() {
String result = this.lowerCaseKey;
if (result == null && this.key != null) {
if (result == null) {
result = this.key.toLowerCase(Locale.getDefault());
this.lowerCaseKey = result;
}

View File

@ -399,13 +399,12 @@ public interface SanitizingFunction {
Assert.notNull(predicate, "'predicate' must not be null");
Assert.notNull(value, "'value' must not be null");
String lowerCaseValue = value.toLowerCase(Locale.getDefault());
return (data) -> nullSafeTest(data.getLowerCaseKey(),
(lowerCaseKey) -> predicate.test(lowerCaseKey, lowerCaseValue));
return (data) -> predicate.test(data.getLowerCaseKey(), lowerCaseValue);
}
private Predicate<SanitizableData> onKey(Predicate<String> predicate) {
Assert.notNull(predicate, "'predicate' must not be null");
return (data) -> nullSafeTest(data.getKey(), predicate);
return (data) -> predicate.test(data.getKey());
}
private Predicate<SanitizableData> onValue(Predicate<Object> predicate) {

View File

@ -136,7 +136,6 @@ class SanitizingFunctionTests {
assertThatApplyingToKey(function, "boot").has(unsanitizedValue());
assertThatApplyingToKey(function, "xspring").has(unsanitizedValue());
assertThatApplyingToKey(function, "springx").has(unsanitizedValue());
assertThatApplyingToKey(function, null).has(unsanitizedValue());
}
@Test
@ -148,7 +147,6 @@ class SanitizingFunctionTests {
assertThatApplyingToKey(function, "boot").has(sanitizedValue());
assertThatApplyingToKey(function, "atest").has(sanitizedValue());
assertThatApplyingToKey(function, "bootx").has(unsanitizedValue());
assertThatApplyingToKey(function, null).has(unsanitizedValue());
}
@Test
@ -160,7 +158,6 @@ class SanitizingFunctionTests {
assertThatApplyingToKey(function, "boot").has(sanitizedValue());
assertThatApplyingToKey(function, "beet").has(sanitizedValue());
assertThatApplyingToKey(function, "spring").has(unsanitizedValue());
assertThatApplyingToKey(function, null).has(unsanitizedValue());
}
@Test
@ -171,7 +168,6 @@ class SanitizingFunctionTests {
assertThatApplyingToKey(function, "XtestX").has(sanitizedValue());
assertThatApplyingToKey(function, "YY").has(sanitizedValue());
assertThatApplyingToKey(function, "xy").has(unsanitizedValue());
assertThatApplyingToKey(function, null).has(unsanitizedValue());
}
@Test
@ -182,7 +178,6 @@ class SanitizingFunctionTests {
assertThatApplyingToKey(function, "SPRING").has(sanitizedValue());
assertThatApplyingToKey(function, "BOOT").has(sanitizedValue());
assertThatApplyingToKey(function, "xspring").has(unsanitizedValue());
assertThatApplyingToKey(function, null).has(unsanitizedValue());
}
@Test
@ -192,7 +187,6 @@ class SanitizingFunctionTests {
assertThatApplyingToKey(function, "spin").has(sanitizedValue());
assertThatApplyingToKey(function, "SPRING").has(unsanitizedValue());
assertThatApplyingToKey(function, "xspring").has(unsanitizedValue());
assertThatApplyingToKey(function, null).has(unsanitizedValue());
}
@Test
@ -204,7 +198,6 @@ class SanitizingFunctionTests {
assertThatApplyingToKey(function, "BO").has(sanitizedValue());
assertThatApplyingToKey(function, "SPRING").has(unsanitizedValue());
assertThatApplyingToKey(function, "boot").has(unsanitizedValue());
assertThatApplyingToKey(function, null).has(unsanitizedValue());
}
@Test
@ -213,7 +206,6 @@ class SanitizingFunctionTests {
assertThatApplyingToKey(function, "spring").has(sanitizedValue());
assertThatApplyingToKey(function, "spin").has(sanitizedValue());
assertThatApplyingToKey(function, "boot").has(unsanitizedValue());
assertThatApplyingToKey(function, null).has(unsanitizedValue());
}
@Test
@ -223,7 +215,6 @@ class SanitizingFunctionTests {
assertThatApplyingToValue(function, "SPRING").has(sanitizedValue());
assertThatApplyingToValue(function, "boot").has(sanitizedValue());
assertThatApplyingToValue(function, "other").has(unsanitizedValue());
assertThatApplyingToKey(function, null).has(unsanitizedValue());
}
@Test
@ -281,7 +272,6 @@ class SanitizingFunctionTests {
assertThatApplyingToValue(function, "spin").has(sanitizedValue());
assertThatApplyingToValue(function, "boot").has(unsanitizedValue());
assertThatApplyingToValue(function, 123).has(unsanitizedValue());
assertThatApplyingToKey(function, null).has(unsanitizedValue());
}
@Test