Skip to content

Commit 399e7eb

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

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -211,7 +211,13 @@ public void putAll(Map<? extends String, ? extends V> map) {
211211
public V putIfAbsent(String key, @Nullable V value) {
212212
String oldKey = this.caseInsensitiveKeys.putIfAbsent(convertKey(key), key);
213213
if (oldKey != null) {
214-
return this.targetMap.get(oldKey);
214+
V oldKeyValue = this.targetMap.get(oldKey);
215+
if (oldKeyValue != null) {
216+
return oldKeyValue;
217+
}
218+
else {
219+
key = oldKey;
220+
}
215221
}
216222
return this.targetMap.putIfAbsent(key, value);
217223
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -99,6 +99,10 @@ void computeIfAbsentWithExistingValue() {
9999
assertThat(map.computeIfAbsent("key", key2 -> "value1")).isEqualTo("value3");
100100
assertThat(map.computeIfAbsent("KEY", key1 -> "value2")).isEqualTo("value3");
101101
assertThat(map.computeIfAbsent("Key", key -> "value3")).isEqualTo("value3");
102+
103+
assertThat(map.put("null", null)).isNull();
104+
assertThat(map.putIfAbsent("NULL", "value")).isNull();
105+
assertThat(map.get("null")).isEqualTo("value");
102106
}
103107

104108
@Test

0 commit comments

Comments
 (0)