diff --git a/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java b/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java index a3db322b6f..a916f1d856 100644 --- a/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java +++ b/spring-core/src/main/java/org/springframework/util/LinkedCaseInsensitiveMap.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -211,7 +211,13 @@ public class LinkedCaseInsensitiveMap implements Map, Serializable public V putIfAbsent(String key, @Nullable V value) { String oldKey = this.caseInsensitiveKeys.putIfAbsent(convertKey(key), key); if (oldKey != null) { - return this.targetMap.get(oldKey); + V oldKeyValue = this.targetMap.get(oldKey); + if (oldKeyValue != null) { + return oldKeyValue; + } + else { + key = oldKey; + } } return this.targetMap.putIfAbsent(key, value); } diff --git a/spring-core/src/test/java/org/springframework/util/LinkedCaseInsensitiveMapTests.java b/spring-core/src/test/java/org/springframework/util/LinkedCaseInsensitiveMapTests.java index 0a2f6df061..f006b0baae 100644 --- a/spring-core/src/test/java/org/springframework/util/LinkedCaseInsensitiveMapTests.java +++ b/spring-core/src/test/java/org/springframework/util/LinkedCaseInsensitiveMapTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -99,6 +99,10 @@ class LinkedCaseInsensitiveMapTests { assertThat(map.computeIfAbsent("key", key2 -> "value1")).isEqualTo("value3"); assertThat(map.computeIfAbsent("KEY", key1 -> "value2")).isEqualTo("value3"); assertThat(map.computeIfAbsent("Key", key -> "value3")).isEqualTo("value3"); + + assertThat(map.put("null", null)).isNull(); + assertThat(map.putIfAbsent("NULL", "value")).isNull(); + assertThat(map.get("null")).isEqualTo("value"); } @Test