Skip to content

Commit 7502442

Browse files
#199 DefaultArangoConverter - handle nulls when writing Maps, Arrays and Collections (#200)
1 parent ca1e32a commit 7502442

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

src/main/java/com/arangodb/springframework/core/convert/DefaultArangoConverter.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,10 @@ private void writeMap(
691691
final Object key = entry.getKey();
692692
final Object value = entry.getValue();
693693

694-
writeInternal(convertId(key), value, sink, getNonNullMapValueType(definedType));
694+
String convertedKey = convertId(key);
695+
if (value != null) {
696+
writeInternal(convertedKey, value, sink, getNonNullMapValueType(definedType));
697+
}
695698
}
696699

697700
sink.close();
@@ -706,7 +709,11 @@ private void writeCollection(
706709
sink.add(attribute, ValueType.ARRAY);
707710

708711
for (final Object entry : asCollection(source)) {
709-
writeInternal(null, entry, sink, getNonNullComponentType(definedType));
712+
if (entry == null) {
713+
writeSimple(null, null, sink);
714+
} else {
715+
writeInternal(null, entry, sink, getNonNullComponentType(definedType));
716+
}
710717
}
711718

712719
sink.close();
@@ -726,7 +733,11 @@ private void writeArray(
726733
sink.add(attribute, ValueType.ARRAY);
727734
for (int i = 0; i < Array.getLength(source); ++i) {
728735
final Object element = Array.get(source, i);
729-
writeInternal(null, element, sink, getNonNullComponentType(definedType));
736+
if (element == null) {
737+
writeSimple(null, null, sink);
738+
} else {
739+
writeInternal(null, element, sink, getNonNullComponentType(definedType));
740+
}
730741
}
731742
sink.close();
732743
}

src/test/java/com/arangodb/springframework/core/mapping/GeneralMappingTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.*;
5050

5151
import static org.hamcrest.Matchers.*;
52+
import static org.junit.Assert.assertEquals;
5253
import static org.junit.Assert.assertThat;
5354

5455
/**
@@ -589,4 +590,57 @@ public void readMapInMap() {
589590
assertThat(find.isPresent(), is(true));
590591
assertThat(find.get().value, is(map));
591592
}
593+
594+
@Test
595+
public void readMapInMapWithNull() {
596+
final MapInMapTestEntity entity = new MapInMapTestEntity();
597+
final Map<String, Object> map = new HashMap<>();
598+
Map<String, Object> nested = new HashMap<>();
599+
nested.put("key1", "test");
600+
nested.put("key2", null);
601+
map.put("nested", nested);
602+
entity.value = map;
603+
template.insert(entity);
604+
final Optional<MapInMapTestEntity> find = template.find(entity.id, MapInMapTestEntity.class);
605+
assertThat(find.isPresent(), is(true));
606+
Map<String, Object> nestedResult = (Map<String, Object>) find.get().value.get("nested");
607+
assertThat(nestedResult.size(), is(1));
608+
assertThat(nestedResult.get("key1"), is("test"));
609+
}
610+
611+
@Test
612+
public void readMapInMapWithArray() {
613+
final MapInMapTestEntity entity = new MapInMapTestEntity();
614+
final Map<String, Object> map = new HashMap<>();
615+
Map<String, Object> nested = new HashMap<>();
616+
String[] nestedArray = {"test", null};
617+
nested.put("key1", nestedArray);
618+
map.put("nested", nested);
619+
entity.value = map;
620+
template.insert(entity);
621+
final Optional<MapInMapTestEntity> find = template.find(entity.id, MapInMapTestEntity.class);
622+
assertThat(find.isPresent(), is(true));
623+
Map<String, Object> nestedResult = (Map<String, Object>) find.get().value.get("nested");
624+
assertThat(nestedResult.size(), is(1));
625+
assertThat(((List<String>) nestedResult.get("key1")).size(), is(2));
626+
assertEquals(Arrays.asList(nestedArray), nestedResult.get("key1"));
627+
}
628+
629+
@Test
630+
public void readMapInMapWithCollection() {
631+
final MapInMapTestEntity entity = new MapInMapTestEntity();
632+
final Map<String, Object> map = new HashMap<>();
633+
Map<String, Object> nested = new HashMap<>();
634+
Collection<String> nestedCollection = Arrays.asList("test", null);
635+
nested.put("key1", nestedCollection);
636+
map.put("nested", nested);
637+
entity.value = map;
638+
template.insert(entity);
639+
final Optional<MapInMapTestEntity> find = template.find(entity.id, MapInMapTestEntity.class);
640+
assertThat(find.isPresent(), is(true));
641+
Map<String, Object> nestedResult = (Map<String, Object>) find.get().value.get("nested");
642+
assertThat(nestedResult.size(), is(1));
643+
assertThat(((List<String>) nestedResult.get("key1")).size(), is(2));
644+
assertEquals(nestedCollection, nestedResult.get("key1"));
645+
}
592646
}

0 commit comments

Comments
 (0)