SLING-8438 - Sling Models BeanPropertyViaProvider should support nesting
diff --git a/src/main/java/org/apache/sling/models/impl/via/BeanPropertyViaProvider.java b/src/main/java/org/apache/sling/models/impl/via/BeanPropertyViaProvider.java
index 337ee12..e05b04b 100644
--- a/src/main/java/org/apache/sling/models/impl/via/BeanPropertyViaProvider.java
+++ b/src/main/java/org/apache/sling/models/impl/via/BeanPropertyViaProvider.java
@@ -45,6 +45,14 @@
if (StringUtils.isBlank(value)) {
return ORIGINAL;
}
+
+ // support nested values, e.g. requestPathInfo.suffixResource.path
+ if (StringUtils.contains(value, '.')) {
+ String[] parts = StringUtils.split(value, ".", 2);
+ Object adaptable = getAdaptable(original, parts[0]);
+ return getAdaptable(adaptable, parts[1]);
+ }
+
try {
BeanInfo beanInfo = Introspector.getBeanInfo(original.getClass());
for (PropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {
diff --git a/src/test/java/org/apache/sling/models/impl/ConstructorTest.java b/src/test/java/org/apache/sling/models/impl/ConstructorTest.java
index 3393f01..1d28125 100644
--- a/src/test/java/org/apache/sling/models/impl/ConstructorTest.java
+++ b/src/test/java/org/apache/sling/models/impl/ConstructorTest.java
@@ -29,16 +29,21 @@
import java.util.concurrent.Future;
import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.request.RequestPathInfo;
+import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.factory.ModelClassException;
import org.apache.sling.models.impl.injectors.RequestAttributeInjector;
import org.apache.sling.models.impl.injectors.SelfInjector;
+import org.apache.sling.models.impl.via.BeanPropertyViaProvider;
import org.apache.sling.models.testmodels.classes.InvalidConstructorModel;
import org.apache.sling.models.testmodels.classes.SuperclassConstructorModel;
import org.apache.sling.models.testmodels.classes.WithOneConstructorModel;
import org.apache.sling.models.testmodels.classes.WithThreeConstructorsModel;
import org.apache.sling.models.testmodels.classes.WithTwoConstructorsModel;
import org.apache.sling.models.testmodels.classes.constructorinjection.NoNameModel;
+import org.apache.sling.models.testmodels.classes.constructorinjection.ViaRequestSuffixModel;
import org.apache.sling.models.testmodels.classes.constructorinjection.WithThreeConstructorsOneInjectModel;
+import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -77,7 +82,17 @@
factory.activate(componentCtx);
factory.bindInjector(new RequestAttributeInjector(), new ServicePropertiesMap(1, 1));
factory.bindInjector(new SelfInjector(), new ServicePropertiesMap(2, 2));
- factory.adapterImplementations.addClassesAsAdapterAndImplementation(WithOneConstructorModel.class, WithThreeConstructorsModel.class, WithTwoConstructorsModel.class, SuperclassConstructorModel.class, InvalidConstructorModel.class, WithThreeConstructorsOneInjectModel.class, NoNameModel.class);
+ factory.bindViaProvider(new BeanPropertyViaProvider(), null);
+ factory.adapterImplementations.addClassesAsAdapterAndImplementation(
+ WithOneConstructorModel.class,
+ WithThreeConstructorsModel.class,
+ WithTwoConstructorsModel.class,
+ SuperclassConstructorModel.class,
+ InvalidConstructorModel.class,
+ WithThreeConstructorsOneInjectModel.class,
+ NoNameModel.class,
+ ViaRequestSuffixModel.class
+ );
}
@Test
@@ -185,4 +200,19 @@
NoNameModel model = factory.getAdapter(request, NoNameModel.class);
assertNull(model);
}
+
+ @Test
+ public void testViaInjectionModel() throws Exception {
+ Resource suffixResource = mock(Resource.class);
+ when(suffixResource.getPath()).thenReturn("/the/suffix");
+
+ RequestPathInfo requestPathInfo = mock(RequestPathInfo.class);
+ when(requestPathInfo.getSuffixResource()).thenReturn(suffixResource);
+
+ when(request.getRequestPathInfo()).thenReturn(requestPathInfo);
+
+ ViaRequestSuffixModel model = factory.getAdapter(request, ViaRequestSuffixModel.class);
+ assertThat(model, Matchers.notNullValue());
+ assertThat(model.getSuffix(), Matchers.is("/the/suffix"));
+ }
}
diff --git a/src/test/java/org/apache/sling/models/testmodels/classes/constructorinjection/ViaRequestSuffixModel.java b/src/test/java/org/apache/sling/models/testmodels/classes/constructorinjection/ViaRequestSuffixModel.java
new file mode 100644
index 0000000..affd62f
--- /dev/null
+++ b/src/test/java/org/apache/sling/models/testmodels/classes/constructorinjection/ViaRequestSuffixModel.java
@@ -0,0 +1,38 @@
+/*
+ * 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.testmodels.classes.constructorinjection;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.Via;
+
+import javax.inject.Inject;
+
+@Model(adaptables = SlingHttpServletRequest.class)
+public class ViaRequestSuffixModel {
+
+ private final String suffix;
+
+ @Inject
+ public ViaRequestSuffixModel(@Via("requestPathInfo.suffixResource.path") String suffixResourcePath) {
+ this.suffix = suffixResourcePath;
+ }
+
+ public String getSuffix() {
+ return suffix;
+ }
+}
\ No newline at end of file