Skip to content

Commit e8a65cb

Browse files
authored
Fix JAXB collections deserializing null (#351)
The JAXB collections don't have setters but instead lazy initialise collections on getter methods, the generated code uses <getter>.addAll() which doesn't support the case when NULL is returned (vs empty collection). Fix is to add a nullToEmpty() helper method for this case (JAXB style collections).
1 parent a977bed commit e8a65cb

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

blackbox-test/src/test/java/org/example/customer/jaxb/MyJaxbTypeTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,13 @@ void emptyList() {
4949
assertThat(fromJson2.getTags2()).isEmpty();
5050
assertThat(fromJson2.getTags3()).isEmpty();
5151
}
52+
53+
@Test
54+
void nullList() {
55+
final var fromJson = jsonb.type(MyJaxbType.class).fromJson("{\"tags\":null,\"tags2\":[],\"name\":\"red\"}");
56+
assertThat(fromJson.name()).isEqualTo("red");
57+
assertThat(fromJson.getTags()).isEmpty();
58+
assertThat(fromJson.getTags2()).isEmpty();
59+
assertThat(fromJson.getTags3()).isEmpty();
60+
}
5261
}

jsonb-generator/src/main/java/io/avaje/jsonb/generator/FieldProperty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public void writeFromJsonSwitch(Append writer, String varName, boolean defaultCo
344344
} else if (publicField) {
345345
writer.append(" _$%s.%s = %s.fromJson(reader);", varName, fieldName, adapterFieldName);
346346
} else if (useGetterAddAll) {
347-
writer.append(" _$%s.%s().addAll(%s.fromJson(reader));", varName, getter.getName(), adapterFieldName);
347+
writer.append(" _$%s.%s().addAll(Types.nullToEmpty(%s.fromJson(reader)));", varName, getter.getName(), adapterFieldName);
348348
}
349349
} else {
350350
writer.append(" _val$%s = %s.fromJson(reader);", fieldName, adapterFieldName);

jsonb/src/main/java/io/avaje/jsonb/Types.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@
1818
import io.avaje.jsonb.core.Util;
1919

2020
import java.lang.reflect.*;
21-
import java.util.List;
22-
import java.util.Map;
23-
import java.util.Optional;
24-
import java.util.Set;
21+
import java.util.*;
2522
import java.util.stream.Stream;
2623

2724
/**
2825
* Factory methods for types.
2926
*/
30-
public class Types {
27+
public final class Types {
3128

3229
private Types() {
3330
// hide
3431
}
3532

33+
public static <T> Collection<T> nullToEmpty(Collection<T> source) {
34+
return source == null ? Collections.emptyList() : source;
35+
}
36+
3637
/**
3738
* Returns an array type whose elements are all instances of {@code componentType}.
3839
*/
@@ -70,7 +71,7 @@ public static ParameterizedType streamOf(Type elementType) {
7071
public static ParameterizedType mapOf(Type valueElementType) {
7172
return newParameterizedType(Map.class, String.class, valueElementType);
7273
}
73-
74+
7475
/**
7576
* Returns a Type that is an Optional of the given element type.
7677
*/

0 commit comments

Comments
 (0)