Skip to content

Commit 641b72d

Browse files
committed
Make SanitizableData.key non-nullable
Closes gh-47220
1 parent eeac38b commit 641b72d

File tree

3 files changed

+9
-18
lines changed

3 files changed

+9
-18
lines changed

module/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/SanitizableData.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.jspecify.annotations.Nullable;
2222

2323
import org.springframework.core.env.PropertySource;
24+
import org.springframework.util.Assert;
2425

2526
/**
2627
* Value object that represents the data that can be used by a {@link SanitizingFunction}.
@@ -38,7 +39,7 @@ public final class SanitizableData {
3839

3940
private final @Nullable PropertySource<?> propertySource;
4041

41-
private final @Nullable String key;
42+
private final String key;
4243

4344
private @Nullable String lowerCaseKey;
4445

@@ -50,7 +51,8 @@ public final class SanitizableData {
5051
* @param key the data key
5152
* @param value the data value
5253
*/
53-
public SanitizableData(@Nullable PropertySource<?> propertySource, @Nullable String key, @Nullable Object value) {
54+
public SanitizableData(@Nullable PropertySource<?> propertySource, String key, @Nullable Object value) {
55+
Assert.notNull(key, "'key' must not be null");
5456
this.propertySource = propertySource;
5557
this.key = key;
5658
this.value = value;
@@ -69,7 +71,7 @@ public SanitizableData(@Nullable PropertySource<?> propertySource, @Nullable Str
6971
* Return the key of the data.
7072
* @return the data key
7173
*/
72-
public @Nullable String getKey() {
74+
public String getKey() {
7375
return this.key;
7476
}
7577

@@ -78,9 +80,9 @@ public SanitizableData(@Nullable PropertySource<?> propertySource, @Nullable Str
7880
* @return the key as a lowercase value
7981
* @since 3.5.0
8082
*/
81-
public @Nullable String getLowerCaseKey() {
83+
public String getLowerCaseKey() {
8284
String result = this.lowerCaseKey;
83-
if (result == null && this.key != null) {
85+
if (result == null) {
8486
result = this.key.toLowerCase(Locale.getDefault());
8587
this.lowerCaseKey = result;
8688
}

module/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/SanitizingFunction.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,12 @@ private Predicate<SanitizableData> onKeyIgnoringCase(BiPredicate<String, String>
399399
Assert.notNull(predicate, "'predicate' must not be null");
400400
Assert.notNull(value, "'value' must not be null");
401401
String lowerCaseValue = value.toLowerCase(Locale.getDefault());
402-
return (data) -> nullSafeTest(data.getLowerCaseKey(),
403-
(lowerCaseKey) -> predicate.test(lowerCaseKey, lowerCaseValue));
402+
return (data) -> predicate.test(data.getLowerCaseKey(), lowerCaseValue);
404403
}
405404

406405
private Predicate<SanitizableData> onKey(Predicate<String> predicate) {
407406
Assert.notNull(predicate, "'predicate' must not be null");
408-
return (data) -> nullSafeTest(data.getKey(), predicate);
407+
return (data) -> predicate.test(data.getKey());
409408
}
410409

411410
private Predicate<SanitizableData> onValue(Predicate<Object> predicate) {

module/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/SanitizingFunctionTests.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ void ifKeyEqualsFiltersExpected() {
136136
assertThatApplyingToKey(function, "boot").has(unsanitizedValue());
137137
assertThatApplyingToKey(function, "xspring").has(unsanitizedValue());
138138
assertThatApplyingToKey(function, "springx").has(unsanitizedValue());
139-
assertThatApplyingToKey(function, null).has(unsanitizedValue());
140139
}
141140

142141
@Test
@@ -148,7 +147,6 @@ void ifKeyEndsWithFiltersExpected() {
148147
assertThatApplyingToKey(function, "boot").has(sanitizedValue());
149148
assertThatApplyingToKey(function, "atest").has(sanitizedValue());
150149
assertThatApplyingToKey(function, "bootx").has(unsanitizedValue());
151-
assertThatApplyingToKey(function, null).has(unsanitizedValue());
152150
}
153151

154152
@Test
@@ -160,7 +158,6 @@ void ifKeyContainsFiltersExpected() {
160158
assertThatApplyingToKey(function, "boot").has(sanitizedValue());
161159
assertThatApplyingToKey(function, "beet").has(sanitizedValue());
162160
assertThatApplyingToKey(function, "spring").has(unsanitizedValue());
163-
assertThatApplyingToKey(function, null).has(unsanitizedValue());
164161
}
165162

166163
@Test
@@ -171,7 +168,6 @@ void ifKeyMatchesIgnoringCaseFiltersExpected() {
171168
assertThatApplyingToKey(function, "XtestX").has(sanitizedValue());
172169
assertThatApplyingToKey(function, "YY").has(sanitizedValue());
173170
assertThatApplyingToKey(function, "xy").has(unsanitizedValue());
174-
assertThatApplyingToKey(function, null).has(unsanitizedValue());
175171
}
176172

177173
@Test
@@ -182,7 +178,6 @@ void ifKeyMatchesWithRegexFiltersExpected() {
182178
assertThatApplyingToKey(function, "SPRING").has(sanitizedValue());
183179
assertThatApplyingToKey(function, "BOOT").has(sanitizedValue());
184180
assertThatApplyingToKey(function, "xspring").has(unsanitizedValue());
185-
assertThatApplyingToKey(function, null).has(unsanitizedValue());
186181
}
187182

188183
@Test
@@ -192,7 +187,6 @@ void ifKeyMatchesWithPatternFiltersExpected() {
192187
assertThatApplyingToKey(function, "spin").has(sanitizedValue());
193188
assertThatApplyingToKey(function, "SPRING").has(unsanitizedValue());
194189
assertThatApplyingToKey(function, "xspring").has(unsanitizedValue());
195-
assertThatApplyingToKey(function, null).has(unsanitizedValue());
196190
}
197191

198192
@Test
@@ -204,7 +198,6 @@ void ifKeyMatchesWithPredicatesFiltersExpected() {
204198
assertThatApplyingToKey(function, "BO").has(sanitizedValue());
205199
assertThatApplyingToKey(function, "SPRING").has(unsanitizedValue());
206200
assertThatApplyingToKey(function, "boot").has(unsanitizedValue());
207-
assertThatApplyingToKey(function, null).has(unsanitizedValue());
208201
}
209202

210203
@Test
@@ -213,7 +206,6 @@ void ifKeyMatchesWithPredicateFiltersExpected() {
213206
assertThatApplyingToKey(function, "spring").has(sanitizedValue());
214207
assertThatApplyingToKey(function, "spin").has(sanitizedValue());
215208
assertThatApplyingToKey(function, "boot").has(unsanitizedValue());
216-
assertThatApplyingToKey(function, null).has(unsanitizedValue());
217209
}
218210

219211
@Test
@@ -223,7 +215,6 @@ void ifValueStringMatchesWithRegexesFiltersExpected() {
223215
assertThatApplyingToValue(function, "SPRING").has(sanitizedValue());
224216
assertThatApplyingToValue(function, "boot").has(sanitizedValue());
225217
assertThatApplyingToValue(function, "other").has(unsanitizedValue());
226-
assertThatApplyingToKey(function, null).has(unsanitizedValue());
227218
}
228219

229220
@Test
@@ -281,7 +272,6 @@ void ifValueMatchesWithPredicateFiltersExpected() {
281272
assertThatApplyingToValue(function, "spin").has(sanitizedValue());
282273
assertThatApplyingToValue(function, "boot").has(unsanitizedValue());
283274
assertThatApplyingToValue(function, 123).has(unsanitizedValue());
284-
assertThatApplyingToKey(function, null).has(unsanitizedValue());
285275
}
286276

287277
@Test

0 commit comments

Comments
 (0)