This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch issue/SLING-13077 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git
commit 18e56fb2f188578616ec9ee9842c48160ac19543 Author: Robert Munteanu <[email protected]> AuthorDate: Tue Jan 27 16:45:58 2026 +0100 SLING-13077 - ExportServlet cannot adapt model to javax SlingHttpServletRequest when running in a Jakarta Servlet environment Ensure that when accessing adaptables and we find a SlingHttpServletRequest.class there is also SlingJakartaHttpServletRequest.class present. --- .../models/impl/LegacyAdaptablesExtender.java | 64 ++++++++++++++++++++++ .../sling/models/impl/ModelAdapterFactory.java | 2 +- .../models/impl/ModelPackageBundleListener.java | 5 +- 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/sling/models/impl/LegacyAdaptablesExtender.java b/src/main/java/org/apache/sling/models/impl/LegacyAdaptablesExtender.java new file mode 100644 index 0000000..df02faa --- /dev/null +++ b/src/main/java/org/apache/sling/models/impl/LegacyAdaptablesExtender.java @@ -0,0 +1,64 @@ +/* + * 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.sling.models.impl; + +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.SlingJakartaHttpServletRequest; +import org.apache.sling.models.annotations.Model; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LegacyAdaptablesExtender { + + private static final Logger LOGGER = LoggerFactory.getLogger(LegacyAdaptablesExtender.class); + + @SuppressWarnings("deprecation") + public static Class<?>[] getAdaptables(Model modelAnnotation) { + boolean hasJakartaServletRequest = false; + boolean hasJavaxServletRequest = false; + Class<?>[] adaptables = modelAnnotation.adaptables(); + for (Class<?> adaptable : adaptables) { + if (adaptable == SlingJakartaHttpServletRequest.class) { + hasJakartaServletRequest = true; + } else if (adaptable == SlingHttpServletRequest.class) { + hasJavaxServletRequest = true; + } + } + + if (hasJavaxServletRequest && !hasJakartaServletRequest) { + + if (LOGGER.isDebugEnabled()) { + LOGGER.debug( + "Model {} adapts from {} but not from {}. Adjusting list to compensage.", + modelAnnotation, + SlingJakartaHttpServletRequest.class.getName(), + SlingHttpServletRequest.class.getName()); + } + + Class<?>[] newAdaptables = new Class<?>[adaptables.length + 1]; + System.arraycopy(adaptables, 0, newAdaptables, 0, adaptables.length); + newAdaptables[adaptables.length] = SlingJakartaHttpServletRequest.class; + return newAdaptables; + } + + return adaptables; + } + + private LegacyAdaptablesExtender() {} +} diff --git a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java index 1158ea2..b0624c0 100644 --- a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java +++ b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java @@ -398,7 +398,7 @@ public class ModelAdapterFactory implements AdapterFactory, Runnable, ModelFacto } } - Class<?>[] declaredAdaptable = modelAnnotation.adaptables(); + Class<?>[] declaredAdaptable = LegacyAdaptablesExtender.getAdaptables(modelAnnotation); for (Class<?> clazz : declaredAdaptable) { if (clazz.isInstance(adaptable)) { isAdaptable = true; diff --git a/src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java b/src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java index cf733cb..b08b7e3 100644 --- a/src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java +++ b/src/main/java/org/apache/sling/models/impl/ModelPackageBundleListener.java @@ -157,7 +157,10 @@ public class ModelPackageBundleListener implements BundleTrackerCustomizer<Servi if (validateAdapterClasses(implType, adapterTypes)) { if (adapterImplementations.addAll(implType, adapterTypes)) { ServiceRegistration reg = registerAdapterFactory( - adapterTypes, annotation.adaptables(), implType, annotation.condition()); + adapterTypes, + LegacyAdaptablesExtender.getAdaptables(annotation), + implType, + annotation.condition()); regs.add(reg); String[] resourceTypes = annotation.resourceType();
