mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19: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
|
@CheckReturnValue
|
||||||
public <T> JsonAdapter<T> adapter(Type type, Class<? extends Annotation> annotationType) {
|
public <T> JsonAdapter<T> adapter(Type type, Class<? extends Annotation> annotationType) {
|
||||||
|
if (annotationType == null) {
|
||||||
|
throw new NullPointerException("annotationType == null");
|
||||||
|
}
|
||||||
return adapter(type,
|
return adapter(type,
|
||||||
Collections.singleton(Types.createJsonQualifierImplementation(annotationType)));
|
Collections.singleton(Types.createJsonQualifierImplementation(annotationType)));
|
||||||
}
|
}
|
||||||
@@ -73,6 +76,10 @@ public final class Moshi {
|
|||||||
@CheckReturnValue
|
@CheckReturnValue
|
||||||
@SuppressWarnings("unchecked") // Factories are required to return only matching JsonAdapters.
|
@SuppressWarnings("unchecked") // Factories are required to return only matching JsonAdapters.
|
||||||
public <T> JsonAdapter<T> adapter(Type type, Set<? extends Annotation> annotations) {
|
public <T> JsonAdapter<T> adapter(Type type, Set<? extends Annotation> annotations) {
|
||||||
|
if (annotations == null) {
|
||||||
|
throw new NullPointerException("annotations == null");
|
||||||
|
}
|
||||||
|
|
||||||
type = Types.canonicalize(type);
|
type = Types.canonicalize(type);
|
||||||
|
|
||||||
// If there's an equivalent adapter in the cache, we're done!
|
// 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\"}");
|
.isEqualTo("{\"shout\":\"WHAT'S UP\",\"speak\":\"Yo dog\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Uppercase
|
@Test public void adapterLookupDisallowsNullAnnotations() {
|
||||||
static String uppercaseString;
|
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 {
|
@Test public void nextJsonAdapterDisallowsNullAnnotations() throws Exception {
|
||||||
JsonAdapter.Factory badFactory = new JsonAdapter.Factory() {
|
JsonAdapter.Factory badFactory = new JsonAdapter.Factory() {
|
||||||
@@ -744,6 +757,9 @@ public final class MoshiTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Uppercase
|
||||||
|
static String uppercaseString;
|
||||||
|
|
||||||
@Test public void delegatingJsonAdapterFactory() throws Exception {
|
@Test public void delegatingJsonAdapterFactory() throws Exception {
|
||||||
Moshi moshi = new Moshi.Builder()
|
Moshi moshi = new Moshi.Builder()
|
||||||
.add(new UppercaseAdapterFactory())
|
.add(new UppercaseAdapterFactory())
|
||||||
|
Reference in New Issue
Block a user