This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/main by this push:
new 825b4bb7e7c Fix JAXBContextCache exact lookup when there is no default
namespace (#3781)
825b4bb7e7c is described below
commit 825b4bb7e7c636d5f3a35e9f2ed7ac90109c16a4
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Thu Nov 26 07:01:00 2026 +0000
Fix JAXBContextCache exact lookup when there is no default namespace (#3781)
* Fix JAXBContextCache exact lookup when there is no default namespace
* Addressing review comments
---
.../apache/cxf/common/jaxb/JAXBContextCache.java | 14 ++++--
.../cxf/common/jaxb/JAXBContextCacheTest.java | 55 ++++++++++++++++++++++
2 files changed, 65 insertions(+), 4 deletions(-)
diff --git
a/core/src/main/java/org/apache/cxf/common/jaxb/JAXBContextCache.java
b/core/src/main/java/org/apache/cxf/common/jaxb/JAXBContextCache.java
index 79908f1316c..f5f72374792 100644
--- a/core/src/main/java/org/apache/cxf/common/jaxb/JAXBContextCache.java
+++ b/core/src/main/java/org/apache/cxf/common/jaxb/JAXBContextCache.java
@@ -199,14 +199,20 @@ public final class JAXBContextCache {
CachedContextAndSchemasInternal cachedContextAndSchemasInternal = null;
JAXBContext context = null;
Map<String, CachedContextAndSchemasInternal>
cachedContextAndSchemasInternalMap = null;
- if (typeRefs == null || typeRefs.isEmpty()) {
+ // Entries are keyed on the classes and default namespace only. Exact
lookups with a
+ // null default namespace never hit the cache before, so keep that
behaviour when JAXB
+ // properties are given rather than sharing a context built with
different properties.
+ final boolean cacheable = (typeRefs == null || typeRefs.isEmpty())
+ && (defaultNs != null || props == null || props.isEmpty());
+ if (cacheable) {
synchronized (JAXBCONTEXT_CACHE) {
if (exact) {
cachedContextAndSchemasInternalMap
= JAXBCONTEXT_CACHE.get(classes);
- if (cachedContextAndSchemasInternalMap != null &&
defaultNs != null) {
- cachedContextAndSchemasInternal =
cachedContextAndSchemasInternalMap.get(defaultNs);
+ if (cachedContextAndSchemasInternalMap != null) {
+ cachedContextAndSchemasInternal
+ = cachedContextAndSchemasInternalMap.get(defaultNs
!= null ? defaultNs : "");
}
} else {
for (Entry<Set<Class<?>>, Map<String,
CachedContextAndSchemasInternal>> k
@@ -252,7 +258,7 @@ public final class JAXBContextCache {
}
cachedContextAndSchemasInternal = new
CachedContextAndSchemasInternal(context, classes);
synchronized (JAXBCONTEXT_CACHE) {
- if (typeRefs == null || typeRefs.isEmpty()) {
+ if (cacheable) {
if (cachedContextAndSchemasInternalMap == null) {
cachedContextAndSchemasInternalMap
= new CacheMap<>();
diff --git
a/core/src/test/java/org/apache/cxf/common/jaxb/JAXBContextCacheTest.java
b/core/src/test/java/org/apache/cxf/common/jaxb/JAXBContextCacheTest.java
new file mode 100644
index 00000000000..73934340af1
--- /dev/null
+++ b/core/src/test/java/org/apache/cxf/common/jaxb/JAXBContextCacheTest.java
@@ -0,0 +1,55 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.common.jaxb;
+
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.cxf.common.jaxb.JAXBContextCache.CachedContextAndSchemas;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertSame;
+
+public class JAXBContextCacheTest {
+
+ @Test
+ public void testExactLookupWithoutDefaultNamespaceIsCached() throws
Exception {
+ String pkg =
org.apache.cxf.configuration.security.ObjectFactory.class.getPackage().getName();
+ ClassLoader loader = getClass().getClassLoader();
+ CachedContextAndSchemas first =
JAXBContextCache.getCachedContextAndSchemas(pkg, null, loader);
+ CachedContextAndSchemas second =
JAXBContextCache.getCachedContextAndSchemas(pkg, null, loader);
+ assertSame(first.getContext(), second.getContext());
+ }
+
+ @Test
+ public void testContextPropertiesAreNotIgnored() throws Exception {
+ String pkg =
org.apache.cxf.configuration.security.ObjectFactory.class.getPackage().getName();
+ ClassLoader loader = getClass().getClassLoader();
+ Map<String, Object> props =
+
Collections.singletonMap("org.glassfish.jaxb.defaultNamespaceRemap",
"urn:probe");
+ CachedContextAndSchemas plain =
JAXBContextCache.getCachedContextAndSchemas(pkg, null, loader);
+ CachedContextAndSchemas remapped =
JAXBContextCache.getCachedContextAndSchemas(pkg, props, loader);
+ assertNotSame(plain.getContext(), remapped.getContext());
+ // and a context built with properties must not be handed to a caller
without them
+ CachedContextAndSchemas plainAgain =
JAXBContextCache.getCachedContextAndSchemas(pkg, null, loader);
+ assertNotSame(remapped.getContext(), plainAgain.getContext());
+ }
+}