mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-26 17:09:05 +00:00
Fix LinkedCaseInsensitiveMap entrySet case-insensitivity
See gh-36056 Signed-off-by: Terry Tao <yueyang.tao@gmail.com>
This commit is contained in:
@@ -448,7 +448,18 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return this.delegate.contains(o);
|
||||
if (!(o instanceof Map.Entry<?, ?> entry)) {
|
||||
return false;
|
||||
}
|
||||
Object key = entry.getKey();
|
||||
if (!(key instanceof String stringKey)) {
|
||||
return false;
|
||||
}
|
||||
if (!LinkedCaseInsensitiveMap.this.containsKey(stringKey)) {
|
||||
return false;
|
||||
}
|
||||
V value = LinkedCaseInsensitiveMap.this.get(stringKey);
|
||||
return ObjectUtils.nullSafeEquals(value, entry.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -457,13 +468,23 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean remove(Object o) {
|
||||
if (this.delegate.remove(o)) {
|
||||
removeCaseInsensitiveKey(((Map.Entry<String, V>) o).getKey());
|
||||
return true;
|
||||
if (!(o instanceof Map.Entry<?, ?> entry)) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
Object key = entry.getKey();
|
||||
if (!(key instanceof String stringKey)) {
|
||||
return false;
|
||||
}
|
||||
if (!LinkedCaseInsensitiveMap.this.containsKey(stringKey)) {
|
||||
return false;
|
||||
}
|
||||
V value = LinkedCaseInsensitiveMap.this.get(stringKey);
|
||||
if (!ObjectUtils.nullSafeEquals(value, entry.getValue())) {
|
||||
return false;
|
||||
}
|
||||
LinkedCaseInsensitiveMap.this.remove(stringKey);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.util;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -218,6 +219,21 @@ class LinkedCaseInsensitiveMapTests {
|
||||
assertThat(map.get("key")).isEqualTo("newvalue");
|
||||
}
|
||||
|
||||
@Test
|
||||
void entrySetContainsIsCaseInsensitive() {
|
||||
map.put("Key", "value");
|
||||
assertThat(map.entrySet().contains(Map.entry("KEY", "value"))).isTrue();
|
||||
assertThat(map.entrySet().contains(Map.entry("key", "value"))).isTrue();
|
||||
assertThat(map.entrySet().contains(Map.entry("Key", "value"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void entrySetRemoveIsCaseInsensitive() {
|
||||
map.put("Key", "value");
|
||||
assertThat(map.entrySet().remove(Map.entry("KEY", "value"))).isTrue();
|
||||
assertThat(map).isEmpty();
|
||||
}
|
||||
|
||||
private void nextAndRemove(Iterator<?> iterator) {
|
||||
iterator.next();
|
||||
iterator.remove();
|
||||
|
||||
Reference in New Issue
Block a user