You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/10/18 23:20:53 UTC

[sling-org-apache-sling-models-jacksonexporter] 20/26: SLING-6966 - don't use ResourceSerializer for Model annotated classes

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-jacksonexporter.git

commit d3352df0e6cb1fd346675694ad2edb46d7a361ce
Author: Justin Edelson <ju...@apache.org>
AuthorDate: Mon Jun 19 15:28:23 2017 +0000

    SLING-6966 - don't use ResourceSerializer for Model annotated classes
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1799221 13f79535-47bb-0310-9956-ffa450edef68
---
 .../impl/ModelSkippingSerializers.java             |  37 +++++
 .../impl/ResourceModuleProvider.java               |   2 +-
 .../impl/ModelSkippingSerializersTest.java         | 182 +++++++++++++++++++++
 3 files changed, 220 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializers.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializers.java
new file mode 100644
index 0000000..8fe6bc9
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializers.java
@@ -0,0 +1,37 @@
+/*
+ * 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.jacksonexporter.impl;
+
+import com.fasterxml.jackson.databind.BeanDescription;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.SerializationConfig;
+import com.fasterxml.jackson.databind.module.SimpleSerializers;
+import org.apache.sling.models.annotations.Model;
+
+public class ModelSkippingSerializers extends SimpleSerializers {
+
+    @Override
+    public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) {
+        Class<?> clazz = type.getRawClass();
+        if (clazz.getAnnotation(Model.class) != null) {
+            return null;
+        }
+
+        return super.findSerializer(config, type, beanDesc);
+    }
+}
diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceModuleProvider.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceModuleProvider.java
index 2c125cd..343fd37 100644
--- a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceModuleProvider.java
+++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceModuleProvider.java
@@ -49,7 +49,7 @@ public class ResourceModuleProvider implements ModuleProvider {
     private void activate(Map<String, Object> props) {
         final int maxRecursionLevels = PropertiesUtil.toInteger(props.get(PROP_MAX_RECURSION_LEVELS), DEFAULT_MAX_RECURSION_LEVELS);
         this.moduleInstance = new SimpleModule();
-        SimpleSerializers serializers = new SimpleSerializers();
+        ModelSkippingSerializers serializers = new ModelSkippingSerializers();
         serializers.addSerializer(Resource.class, new ResourceSerializer(maxRecursionLevels));
         moduleInstance.setSerializers(serializers);
     }
diff --git a/src/test/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializersTest.java b/src/test/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializersTest.java
new file mode 100644
index 0000000..52fda00
--- /dev/null
+++ b/src/test/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializersTest.java
@@ -0,0 +1,182 @@
+/*
+ * 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.jacksonexporter.impl;
+
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.SerializationConfig;
+import com.fasterxml.jackson.databind.type.TypeFactory;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.AbstractResource;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.models.annotations.Model;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Iterator;
+
+import static org.junit.Assert.*;
+
+public class ModelSkippingSerializersTest {
+
+    private ModelSkippingSerializers serializers = new ModelSkippingSerializers();
+    private JavaType nonAnnotatedType = TypeFactory.defaultInstance().constructType(NonAnnotated.class);
+    private JavaType annotatedType = TypeFactory.defaultInstance().constructType(Annotated.class);
+
+    @Before
+    public void setup() {
+        serializers.addSerializer(Resource.class, new ResourceSerializer(-1));
+    }
+
+    @Test
+    public void testDefaultSerializerAccess() {
+        assertTrue(serializers.findSerializer(null, nonAnnotatedType, null) instanceof ResourceSerializer);
+    }
+
+    @Test
+    public void testAnnotatedNullLookup() {
+        assertNull(serializers.findSerializer(null, annotatedType, null));
+    }
+
+    @Model(adaptables = SlingHttpServletRequest.class)
+    private class Annotated extends AbstractResource {
+
+        @Override
+        public String getName() {
+            return null;
+        }
+
+        @Override
+        public String getPath() {
+            return null;
+        }
+
+        @Override
+        public Resource getParent() {
+            return null;
+        }
+
+        @Override
+        public Resource getChild(String relPath) {
+            return null;
+        }
+
+        @Override
+        public Iterator<Resource> listChildren() {
+            return null;
+        }
+
+        @Override
+        public Iterable<Resource> getChildren() {
+            return null;
+        }
+
+        @Override
+        public boolean isResourceType(String resourceType) {
+            return false;
+        }
+
+        @Override
+        public String getResourceType() {
+            return null;
+        }
+
+        @Override
+        public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
+            return null;
+        }
+
+        @Override
+        public ResourceMetadata getResourceMetadata() {
+            return null;
+        }
+
+        @Override
+        public ResourceResolver getResourceResolver() {
+            return null;
+        }
+
+        @Override
+        public String getResourceSuperType() {
+            return null;
+        }
+    }
+
+    private class NonAnnotated extends AbstractResource {
+
+        @Override
+        public String getName() {
+            return null;
+        }
+
+        @Override
+        public String getPath() {
+            return null;
+        }
+
+        @Override
+        public Resource getParent() {
+            return null;
+        }
+
+        @Override
+        public Resource getChild(String relPath) {
+            return null;
+        }
+
+        @Override
+        public Iterator<Resource> listChildren() {
+            return null;
+        }
+
+        @Override
+        public Iterable<Resource> getChildren() {
+            return null;
+        }
+
+        @Override
+        public boolean isResourceType(String resourceType) {
+            return false;
+        }
+
+        @Override
+        public String getResourceType() {
+            return null;
+        }
+
+        @Override
+        public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
+            return null;
+        }
+
+        @Override
+        public ResourceMetadata getResourceMetadata() {
+            return null;
+        }
+
+        @Override
+        public ResourceResolver getResourceResolver() {
+            return null;
+        }
+
+        @Override
+        public String getResourceSuperType() {
+            return null;
+        }
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.