This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/CXF-8142 in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 850ef84d2e68f8cf21f0f4960776a1ce2c9f4d61 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Mon Oct 19 09:48:10 2026 +0100 CXF-8142 - Fix WSDLManager.removeDefinition leaving stale entries --- .../java/org/apache/cxf/common/util/CacheMap.java | 11 ++++++++--- .../cxf/common/util/WeakIdentityHashMap.java | 4 +++- .../org/apache/cxf/common/util/CacheMapTest.java | 22 ++++++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/apache/cxf/common/util/CacheMap.java b/core/src/main/java/org/apache/cxf/common/util/CacheMap.java index 17aeb8004f0..08fe765980b 100644 --- a/core/src/main/java/org/apache/cxf/common/util/CacheMap.java +++ b/core/src/main/java/org/apache/cxf/common/util/CacheMap.java @@ -18,9 +18,11 @@ */ package org.apache.cxf.common.util; +import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; +import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.WeakHashMap; @@ -120,10 +122,13 @@ public class CacheMap<K, V> implements Map<K, V> { public V remove(Object key) { V v = mainDataMap.remove(key); - Set<K> keys = new HashSet<>(extraKeyMap.keySet()); + // Must not use a HashSet here: extraKeyMap may hold several distinct key + // instances that are equal to each other (e.g. different String instances + // of the same URL), and all of them need to be removed. + List<K> keys = new ArrayList<>(extraKeyMap.keySet()); V v2 = extraKeyMap.remove(key); for (K nk : keys) { - if ((key != null && key.equals(nk)) || (key == null && nk == null)) { + if (Objects.equals(key, nk)) { V v3 = extraKeyMap.remove(nk); if (v2 == null) { v2 = v3; diff --git a/core/src/main/java/org/apache/cxf/common/util/WeakIdentityHashMap.java b/core/src/main/java/org/apache/cxf/common/util/WeakIdentityHashMap.java index 53ce7f035de..43e4bb2e09c 100644 --- a/core/src/main/java/org/apache/cxf/common/util/WeakIdentityHashMap.java +++ b/core/src/main/java/org/apache/cxf/common/util/WeakIdentityHashMap.java @@ -25,6 +25,7 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.IdentityHashMap; import java.util.Map; import java.util.Set; @@ -95,7 +96,8 @@ public class WeakIdentityHashMap<K, V> implements Map<K, V> { } public Set<K> keySet() { reap(); - Set<K> ret = new HashSet<>(); + // keys are compared by identity, so equal but distinct keys must not be merged + Set<K> ret = Collections.newSetFromMap(new IdentityHashMap<>()); for (IdentityWeakReference ref : backingStore.keySet()) { K key = ref.get(); if (key != null) { diff --git a/core/src/test/java/org/apache/cxf/common/util/CacheMapTest.java b/core/src/test/java/org/apache/cxf/common/util/CacheMapTest.java index a28576cf918..6bf5aa83968 100644 --- a/core/src/test/java/org/apache/cxf/common/util/CacheMapTest.java +++ b/core/src/test/java/org/apache/cxf/common/util/CacheMapTest.java @@ -50,4 +50,26 @@ public class CacheMapTest { Assert.assertNull(definitions.remove(null)); } + @Test + public void testRemoveWithMultipleEqualKeys() { + Map<Object, Object> definitions = new CacheMap<>(); + Object value = new Object(); + + String putKey = new String("http://localhost/service?wsdl"); + definitions.put(putKey, value); + // lookups with distinct but equal key instances are recorded in the extra key map + String lookupKey1 = new String("http://localhost/service?wsdl"); + String lookupKey2 = new String("http://localhost/service?wsdl"); + Assert.assertSame(value, definitions.get(lookupKey1)); + Assert.assertSame(value, definitions.get(lookupKey2)); + + Assert.assertSame(value, definitions.remove(new String("http://localhost/service?wsdl"))); + + Assert.assertTrue(definitions.isEmpty()); + Assert.assertFalse(definitions.containsKey(putKey)); + Assert.assertNull(definitions.get(lookupKey1)); + Assert.assertNull(definitions.get(lookupKey2)); + Assert.assertEquals(0, definitions.size()); + } + }
