Author: justin
Date: Mon Jun 19 15:28:23 2017
New Revision: 1799221

URL: http://svn.apache.org/viewvc?rev=1799221&view=rev
Log:
SLING-6966 - don't use ResourceSerializer for Model annotated classes

Added:
    
sling/trunk/bundles/extensions/models/jackson-exporter/src/main/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializers.java
    sling/trunk/bundles/extensions/models/jackson-exporter/src/test/
    sling/trunk/bundles/extensions/models/jackson-exporter/src/test/java/
    sling/trunk/bundles/extensions/models/jackson-exporter/src/test/java/org/
    
sling/trunk/bundles/extensions/models/jackson-exporter/src/test/java/org/apache/
    
sling/trunk/bundles/extensions/models/jackson-exporter/src/test/java/org/apache/sling/
    
sling/trunk/bundles/extensions/models/jackson-exporter/src/test/java/org/apache/sling/models/
    
sling/trunk/bundles/extensions/models/jackson-exporter/src/test/java/org/apache/sling/models/jacksonexporter/
    
sling/trunk/bundles/extensions/models/jackson-exporter/src/test/java/org/apache/sling/models/jacksonexporter/impl/
    
sling/trunk/bundles/extensions/models/jackson-exporter/src/test/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializersTest.java
Modified:
    
sling/trunk/bundles/extensions/models/jackson-exporter/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceModuleProvider.java

Added: 
sling/trunk/bundles/extensions/models/jackson-exporter/src/main/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializers.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/jackson-exporter/src/main/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializers.java?rev=1799221&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/models/jackson-exporter/src/main/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializers.java
 (added)
+++ 
sling/trunk/bundles/extensions/models/jackson-exporter/src/main/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializers.java
 Mon Jun 19 15:28:23 2017
@@ -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);
+    }
+}

Modified: 
sling/trunk/bundles/extensions/models/jackson-exporter/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceModuleProvider.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/jackson-exporter/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceModuleProvider.java?rev=1799221&r1=1799220&r2=1799221&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/models/jackson-exporter/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceModuleProvider.java
 (original)
+++ 
sling/trunk/bundles/extensions/models/jackson-exporter/src/main/java/org/apache/sling/models/jacksonexporter/impl/ResourceModuleProvider.java
 Mon Jun 19 15:28:23 2017
@@ -49,7 +49,7 @@ public class ResourceModuleProvider impl
     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);
     }

Added: 
sling/trunk/bundles/extensions/models/jackson-exporter/src/test/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializersTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/jackson-exporter/src/test/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializersTest.java?rev=1799221&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/models/jackson-exporter/src/test/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializersTest.java
 (added)
+++ 
sling/trunk/bundles/extensions/models/jackson-exporter/src/test/java/org/apache/sling/models/jacksonexporter/impl/ModelSkippingSerializersTest.java
 Mon Jun 19 15:28:23 2017
@@ -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;
+        }
+    }
+}


Reply via email to