From f8c375e550ac6851bec534475c781dee2e112aaa Mon Sep 17 00:00:00 2001 From: lma Date: Fri, 8 Mar 2019 11:12:04 -0800 Subject: [PATCH] ATLAS-2933:Empty array attributes are returned as null instead of an empty list. --- .../store/graph/v2/AtlasGraphUtilsV2.java | 13 +++--- .../store/graph/v2/EntityGraphRetriever.java | 6 ++- .../graph/v2/AtlasComplexAttributesTest.java | 42 ++++++++++++++++++- 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasGraphUtilsV2.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasGraphUtilsV2.java index 8f4baa1cc9c..b144bf9f372 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasGraphUtilsV2.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/AtlasGraphUtilsV2.java @@ -207,12 +207,15 @@ public static void setProperty(T element, String proper Object existingValue = element.getProperty(propertyName, Object.class); if (value == null || (value instanceof Collection && ((Collection)value).isEmpty())) { - if (existingValue != null) { - if (LOG.isDebugEnabled()) { - LOG.debug("Removing property {} from {}", propertyName, toString(element)); + if (value == null) { + if (existingValue != null) { + if (LOG.isDebugEnabled()) { + LOG.debug("Removing property {} from {}", propertyName, toString(element)); + } + element.removeProperty(propertyName); } - - element.removeProperty(propertyName); + } else { + element.setProperty(propertyName, value); } } else { if (!value.equals(existingValue)) { diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/EntityGraphRetriever.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/EntityGraphRetriever.java index c727a4fd937..fc69357c828 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/EntityGraphRetriever.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v2/EntityGraphRetriever.java @@ -725,6 +725,10 @@ private List mapVertexToArrayForSoftRef(AtlasVertex entityVertex, List ret = null; List softRefVal = entityVertex.getListProperty(attribute.getVertexPropertyName(), List.class); + if (CollectionUtils.isEmpty(softRefVal)) { + return softRefVal; + } + if (CollectionUtils.isNotEmpty(softRefVal)) { ret = new ArrayList<>(); @@ -822,7 +826,7 @@ private List mapVertexToArray(AtlasVertex entityVertex, AtlasEntityExtIn List arrayElements = getArrayElementsProperty(arrayElementType, entityVertex, attribute); if (CollectionUtils.isEmpty(arrayElements)) { - return null; + return arrayElements; } if (LOG.isDebugEnabled()) { diff --git a/repository/src/test/java/org/apache/atlas/repository/store/graph/v2/AtlasComplexAttributesTest.java b/repository/src/test/java/org/apache/atlas/repository/store/graph/v2/AtlasComplexAttributesTest.java index cf7fa080a6c..2a3624a13a2 100644 --- a/repository/src/test/java/org/apache/atlas/repository/store/graph/v2/AtlasComplexAttributesTest.java +++ b/repository/src/test/java/org/apache/atlas/repository/store/graph/v2/AtlasComplexAttributesTest.java @@ -38,6 +38,7 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.Iterator; @@ -48,10 +49,13 @@ import static org.apache.atlas.TestUtilsV2.ENTITY_TYPE_MAP; import static org.apache.atlas.TestUtilsV2.ENTITY_TYPE_WITH_COMPLEX_COLLECTION_ATTR; import static org.apache.atlas.TestUtilsV2.ENTITY_TYPE_WITH_COMPLEX_COLLECTION_ATTR_DELETE; +import static org.apache.atlas.TestUtilsV2.ENTITY_TYPE_WITH_NESTED_COLLECTION_ATTR; import static org.apache.atlas.TestUtilsV2.NAME; import static org.apache.atlas.repository.graph.GraphHelper.getStatus; import static org.apache.atlas.type.AtlasTypeUtil.getAtlasObjectId; import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; import static org.testng.AssertJUnit.assertEquals; @Guice(modules = TestModules.TestOnlyModule.class) @@ -59,6 +63,7 @@ public class AtlasComplexAttributesTest extends AtlasEntityTestBase { private AtlasEntityWithExtInfo complexCollectionAttrEntity; private AtlasEntityWithExtInfo complexCollectionAttrEntityForDelete; private AtlasEntityWithExtInfo mapAttributesEntity; + private AtlasEntityWithExtInfo nestedCollectionAttrEntity; @BeforeClass public void setUp() throws Exception { @@ -66,13 +71,15 @@ public void setUp() throws Exception { // create typeDefs AtlasTypesDef[] testTypesDefs = new AtlasTypesDef[] { TestUtilsV2.defineTypeWithComplexCollectionAttributes(), - TestUtilsV2.defineTypeWithMapAttributes() }; + TestUtilsV2.defineTypeWithMapAttributes(), + TestUtilsV2.defineTypeWithNestedCollectionAttributes()}; createTypesDef(testTypesDefs); // create entity complexCollectionAttrEntity = TestUtilsV2.createComplexCollectionAttrEntity(); complexCollectionAttrEntityForDelete = TestUtilsV2.createComplexCollectionAttrEntity(); mapAttributesEntity = TestUtilsV2.createMapAttrEntity(); + nestedCollectionAttrEntity = TestUtilsV2.createNestedCollectionAttrEntity(); } @Test @@ -195,6 +202,39 @@ private void updateEntityMapAttributes(AtlasEntity attrEntity, Map