Disallow null annotation set in adapter lookup. (#460)

This commit is contained in:
Eric Cochran
2018-03-10 03:42:07 -08:00
committed by Jesse Wilson
parent ce879634cc
commit e6c2ebedde
2 changed files with 25 additions and 2 deletions

View File

@@ -66,6 +66,9 @@ public final class Moshi {
@CheckReturnValue
public <T> JsonAdapter<T> adapter(Type type, Class<? extends Annotation> annotationType) {
if (annotationType == null) {
throw new NullPointerException("annotationType == null");
}
return adapter(type,
Collections.singleton(Types.createJsonQualifierImplementation(annotationType)));
}
@@ -73,6 +76,10 @@ 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 (annotations == null) {
throw new NullPointerException("annotations == null");
}
type = Types.canonicalize(type);
// If there's an equivalent adapter in the cache, we're done!

View File

@@ -724,8 +724,21 @@ public final class MoshiTest {
.isEqualTo("{\"shout\":\"WHAT'S UP\",\"speak\":\"Yo dog\"}");
}
@Uppercase
static String uppercaseString;
@Test public void adapterLookupDisallowsNullAnnotations() {
Moshi moshi = new Moshi.Builder().build();
try {
moshi.adapter(String.class, (Class<? extends Annotation>) null);
fail();
} catch (NullPointerException expected) {
assertThat(expected).hasMessage("annotationType == null");
}
try {
moshi.adapter(String.class, (Set<? extends Annotation>) null);
fail();
} catch (NullPointerException expected) {
assertThat(expected).hasMessage("annotations == null");
}
}
@Test public void nextJsonAdapterDisallowsNullAnnotations() throws Exception {
JsonAdapter.Factory badFactory = new JsonAdapter.Factory() {
@@ -744,6 +757,9 @@ public final class MoshiTest {
}
}
@Uppercase
static String uppercaseString;
@Test public void delegatingJsonAdapterFactory() throws Exception {
Moshi moshi = new Moshi.Builder()
.add(new UppercaseAdapterFactory())