SLING-6341 - fix array support for resource path injector with patch from Santiago...
authorJustin Edelson <justin@apache.org>
Wed, 30 Nov 2016 17:47:18 +0000 (17:47 +0000)
committerJustin Edelson <justin@apache.org>
Wed, 30 Nov 2016 17:47:18 +0000 (17:47 +0000)
This closes #186

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1772094 13f79535-47bb-0310-9956-ffa450edef68

src/main/java/org/apache/sling/models/impl/injectors/ResourcePathInjector.java
src/test/java/org/apache/sling/models/impl/ResourcePathInjectionTest.java
src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathModelWrapping.java [new file with mode: 0644]

index 330ba5b..2473387 100644 (file)
@@ -21,6 +21,7 @@ package org.apache.sling.models.impl.injectors;
 import java.lang.reflect.AnnotatedElement;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import javax.annotation.Nonnull;
@@ -91,10 +92,13 @@ public class ResourcePathInjector extends AbstractInjector implements Injector,
         if (resources == null || resources.isEmpty()) {
             return null;
         }
-        // unwrap if necessary
+        // unwrap/wrap if necessary
         if (isDeclaredTypeCollection(declaredType)) {
             return resources;
-        } else if (resources.size() == 1) {
+        } if (declaredType instanceof Class<?> && ((Class<?>)declaredType).isArray()){
+            return resources.toArray(new Resource[0]);
+        }
+         if (resources.size() == 1) {
             return resources.get(0);
         } else {
             // multiple resources to inject, but field is not a list
index 6a3e6ab..787bef9 100644 (file)
@@ -36,6 +36,7 @@ import org.apache.sling.models.impl.injectors.SelfInjector;
 import org.apache.sling.models.impl.injectors.ValueMapInjector;
 import org.apache.sling.models.testmodels.classes.ResourcePathAllOptionalModel;
 import org.apache.sling.models.testmodels.classes.ResourcePathModel;
+import org.apache.sling.models.testmodels.classes.ResourcePathModelWrapping;
 import org.apache.sling.models.testmodels.classes.ResourcePathPartialModel;
 import org.junit.Before;
 import org.junit.Test;
@@ -112,7 +113,8 @@ public class ResourcePathInjectionTest {
         factory.bindInjector(new ValueMapInjector(), new ServicePropertiesMap(2, 2000));
         factory.bindInjector(new ResourcePathInjector(), new ServicePropertiesMap(3, 2500));
         factory.bindStaticInjectAnnotationProcessorFactory(new ResourcePathInjector(), new ServicePropertiesMap(3, 2500));
-        factory.adapterImplementations.addClassesAsAdapterAndImplementation(ResourcePathModel.class, ResourcePathPartialModel.class, ResourcePathAllOptionalModel.class);
+        factory.adapterImplementations.addClassesAsAdapterAndImplementation(ResourcePathModel.class, ResourcePathPartialModel.class,
+                ResourcePathAllOptionalModel.class, ResourcePathModelWrapping.class);
     }
 
     @Test
@@ -185,4 +187,12 @@ public class ResourcePathInjectionTest {
         assertNull(model);
     }
 
+    @Test
+    public void TestWithArrayWrapping() {
+        ResourcePathModelWrapping model = factory.getAdapter(adaptable, ResourcePathModelWrapping.class);
+        assertNotNull(model);
+        assertTrue(model.getFromPath().length > 0);
+        assertTrue(model.getMultipleResources().length > 0);
+    }
+
 }
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathModelWrapping.java b/src/test/java/org/apache/sling/models/testmodels/classes/ResourcePathModelWrapping.java
new file mode 100644 (file)
index 0000000..15f225b
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * 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;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.injectorspecific.ResourcePath;
+
+@Model(adaptables = { Resource.class, SlingHttpServletRequest.class })
+public class ResourcePathModelWrapping {
+
+    @ResourcePath(path = "/some/path")
+    private Resource[] fromPath;
+
+    @ResourcePath(name = "propertyWithSeveralPaths")
+    private Resource[] multipleResources;
+
+    public Resource[] getFromPath() {
+        return fromPath;
+    }
+
+    public Resource[] getMultipleResources() {
+        return this.multipleResources;
+    }
+
+}