Skip to content

Commit ee0dbd3

Browse files
committed
Collection adapter nullsafety
It seems passing a null map/list for serialization throws an NPE
1 parent 56f31ed commit ee0dbd3

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

json-core/src/main/java/io/avaje/json/core/CollectionAdapter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public C fromJson(JsonReader reader) {
8383

8484
@Override
8585
public void toJson(JsonWriter writer, C value) {
86+
if (value == null) {
87+
writer.nullValue();
88+
return;
89+
}
8690
if (value.isEmpty()) {
8791
writer.emptyArray();
8892
return;

json-core/src/main/java/io/avaje/json/core/MapAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
final class MapAdapter<V> implements JsonAdapter<Map<String, V>> {
3131

3232
static <V> JsonAdapter<Map<String, V>> create(JsonAdapter<V> valueAdapter) {
33-
return new MapAdapter<>(valueAdapter);
33+
return new MapAdapter<>(valueAdapter).nullSafe();
3434
}
3535

3636
private final JsonAdapter<V> valueAdapter;

json-core/src/test/java/io/avaje/json/core/CoreTypesTest.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package io.avaje.json.core;
22

3-
import io.avaje.json.JsonAdapter;
4-
import io.avaje.json.JsonReader;
5-
import io.avaje.json.stream.BufferedJsonWriter;
6-
import io.avaje.json.stream.JsonStream;
7-
import org.junit.jupiter.api.Test;
3+
import static org.assertj.core.api.Assertions.assertThat;
84

95
import java.util.LinkedHashMap;
106
import java.util.List;
117
import java.util.Map;
128

13-
import static org.assertj.core.api.Assertions.*;
9+
import org.junit.jupiter.api.Test;
10+
11+
import io.avaje.json.JsonAdapter;
12+
import io.avaje.json.JsonReader;
13+
import io.avaje.json.stream.BufferedJsonWriter;
14+
import io.avaje.json.stream.JsonStream;
1415

1516
class CoreTypesTest {
1617

@@ -36,6 +37,17 @@ void mapOfScalar() {
3637
assertThat(fromJsonMap).containsOnlyKeys("one", "two");
3738
}
3839

40+
@Test
41+
void nullMap() {
42+
JsonAdapter<Long> longAdapter = CoreTypes.create(Long.class);
43+
JsonAdapter<Map<String, Long>> mapAdapter = CoreTypes.createMap(longAdapter);
44+
45+
BufferedJsonWriter writer = stream.bufferedWriter();
46+
mapAdapter.toJson(writer, null);
47+
String asJson = writer.result();
48+
assertThat(asJson).isEmpty();
49+
}
50+
3951
@Test
4052
void listOfScalar() {
4153
JsonAdapter<Long> longAdapter = CoreTypes.create(Long.class);
@@ -54,6 +66,17 @@ void listOfScalar() {
5466
assertThat(fromJsnoList).containsExactly(54L, 21L, 63L);
5567
}
5668

69+
@Test
70+
void nullList() {
71+
JsonAdapter<Long> longAdapter = CoreTypes.create(Long.class);
72+
JsonAdapter<List<Long>> listAdapter = CoreTypes.createList(longAdapter);
73+
74+
BufferedJsonWriter writer = stream.bufferedWriter();
75+
listAdapter.toJson(writer, null);
76+
String asJson = writer.result();
77+
assertThat(asJson).isEmpty();
78+
}
79+
5780
@SuppressWarnings("unchecked")
5881
@Test
5982
void createCoreAdapters() {

0 commit comments

Comments
 (0)