From a4c9ef926547c101bc6237febc2dc718020291c7 Mon Sep 17 00:00:00 2001 From: Nicolas Peltier Date: Fri, 25 Sep 2020 17:44:30 +0200 Subject: [PATCH] GH-156 fix NPE for corner case + docs --- ...dobe.dx.domtagging.internal.Spacing.config | 0 .../internal/AttributeServiceImpl.java | 16 ++++++++----- .../internal/AttributeServiceImplTest.java | 24 ++++++++++++++++++- 3 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 apps/docs/app/jcr_root/apps/dx-docs/configs/config/com.adobe.dx.domtagging.internal.Spacing.config diff --git a/apps/docs/app/jcr_root/apps/dx-docs/configs/config/com.adobe.dx.domtagging.internal.Spacing.config b/apps/docs/app/jcr_root/apps/dx-docs/configs/config/com.adobe.dx.domtagging.internal.Spacing.config new file mode 100644 index 00000000..e69de29b diff --git a/bundles/core/src/main/java/com/adobe/dx/domtagging/internal/AttributeServiceImpl.java b/bundles/core/src/main/java/com/adobe/dx/domtagging/internal/AttributeServiceImpl.java index e1d80b3b..921a33c9 100644 --- a/bundles/core/src/main/java/com/adobe/dx/domtagging/internal/AttributeServiceImpl.java +++ b/bundles/core/src/main/java/com/adobe/dx/domtagging/internal/AttributeServiceImpl.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -58,9 +59,10 @@ public class AttributeServiceImpl extends AbstractWorkerManager if (worker != null) { logger.debug("get attributes from worker {}", worker.getKey()); Map workerMap = worker.getAttributes(request); - if (attributes == null) { - attributes = workerMap; - } else { + if (workerMap != null){ + if (attributes == null) { + attributes = new HashMap<>(); + } attributes.putAll(workerMap); } } @@ -76,10 +78,12 @@ public class AttributeServiceImpl extends AbstractWorkerManager if (worker != null) { logger.debug("get classes from worker {}", worker.getKey()); Collection workerClasses = worker.getClasses(request); - if (classes == null) { - classes = new ArrayList<>(); + if (workerClasses != null) { + if (classes == null) { + classes = new ArrayList<>(); + } + classes.addAll(workerClasses); } - classes.addAll(workerClasses); } } return classes != null ? String.join(SPACE, classes) : null; diff --git a/bundles/core/src/test/java/com/adobe/dx/domtagging/internal/AttributeServiceImplTest.java b/bundles/core/src/test/java/com/adobe/dx/domtagging/internal/AttributeServiceImplTest.java index 2d32c910..5b7860ac 100644 --- a/bundles/core/src/test/java/com/adobe/dx/domtagging/internal/AttributeServiceImplTest.java +++ b/bundles/core/src/test/java/com/adobe/dx/domtagging/internal/AttributeServiceImplTest.java @@ -84,7 +84,7 @@ public String getKey() { void setup() { List breakpoints = ResponsivePropertiesImplTest.initResponsiveConfiguration(context); String someComp = CONTENT_ROOT + "/comp"; - final String[] array = new String[] {"worker1", "worker2"}; + final String[] array = new String[] {"worker1", "worker2", "workerNull"}; context.build().resource(CONFIG_ROOTS + "/apps/foo/bar", "attributeWorkers", array); context.build().resource(someComp, "sling:resourceType", "foo/bar"); context.currentResource(someComp); @@ -114,4 +114,26 @@ public void testTwoWorker() { assertTrue(CollectionUtils.isEqualCollection(Arrays.asList("11", "12", "21", "22"), service.getAttributes(context.request()).keySet())); assertEquals("c1 c2", service.getClassesString(context.request())); } + + @Test + public void testNullWorker() { + service.bindWorker(new AttributeWorker() { + @Override + public Map getAttributes(SlingHttpServletRequest request) { + return null; + } + + @Override + public Collection getClasses(SlingHttpServletRequest request) { + return null; + } + + @Override + public String getKey() { + return "workerNull"; + } + }); + assertNull(service.getClassesString(context.request())); + assertNull(service.getAttributes(context.request())); + } } \ No newline at end of file