Disallow null Type from entering user code.

Otherwise, Moshi.adapter(null) will end up calling into user JsonAdapter factories with a null Type parameter.
Kotlin fails with java.lang.IllegalArgumentException: Parameter specified as non-null is null
That message ends up too far away from the real error.
This commit is contained in:
Eric Cochran 2018-04-15 20:45:28 -07:00
parent b860b6da4f
commit 84745b0537
2 changed files with 14 additions and 0 deletions

View file

@ -92,6 +92,9 @@ public final class Moshi {
@CheckReturnValue
@SuppressWarnings("unchecked") // Factories are required to return only matching JsonAdapters.
public <T> JsonAdapter<T> adapter(Type type, Set<? extends Annotation> annotations) {
if (type == null) {
throw new NullPointerException("type == null");
}
if (annotations == null) {
throw new NullPointerException("annotations == null");
}

View file

@ -27,6 +27,7 @@ import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@ -725,6 +726,16 @@ public final class MoshiTest {
.isEqualTo("{\"shout\":\"WHAT'S UP\",\"speak\":\"Yo dog\"}");
}
@Test public void adapterLookupDisallowsNullType() {
Moshi moshi = new Moshi.Builder().build();
try {
moshi.adapter(null, Collections.<Annotation>emptySet());
fail();
} catch (NullPointerException expected) {
assertThat(expected).hasMessage("type == null");
}
}
@Test public void adapterLookupDisallowsNullAnnotations() {
Moshi moshi = new Moshi.Builder().build();
try {