mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Disallow null annotation set in adapter lookup. (#460)
This commit is contained in:
committed by
Jesse Wilson
parent
ce879634cc
commit
e6c2ebedde
@@ -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!
|
||||
|
@@ -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())
|
||||
|
Reference in New Issue
Block a user