Skip to content

Commit b18cf3c

Browse files
committed
Follow contract of computeIfAbsent LinkedCaseInsensitiveMap
This commit makes sure that LinkedCaseInsensitiveMap::computeIfAbsent honors the contract of the method, and also replaces the old entry if that mapped to null. Closes gh-26868
1 parent 399e7eb commit b18cf3c

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,13 @@ public V putIfAbsent(String key, @Nullable V value) {
227227
public V computeIfAbsent(String key, Function<? super String, ? extends V> mappingFunction) {
228228
String oldKey = this.caseInsensitiveKeys.putIfAbsent(convertKey(key), key);
229229
if (oldKey != null) {
230-
return this.targetMap.get(oldKey);
230+
V oldKeyValue = this.targetMap.get(oldKey);
231+
if (oldKeyValue != null) {
232+
return oldKeyValue;
233+
}
234+
else {
235+
key = oldKey;
236+
}
231237
}
232238
return this.targetMap.computeIfAbsent(key, mappingFunction);
233239
}

spring-core/src/test/java/org/springframework/util/LinkedCaseInsensitiveMapTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ void computeIfAbsentWithExistingValue() {
102102

103103
assertThat(map.put("null", null)).isNull();
104104
assertThat(map.putIfAbsent("NULL", "value")).isNull();
105+
assertThat(map.put("null", null)).isEqualTo("value");
106+
assertThat(map.computeIfAbsent("NULL", s -> "value")).isEqualTo("value");
105107
assertThat(map.get("null")).isEqualTo("value");
106108
}
107109

0 commit comments

Comments
 (0)