Fail earlier with null annotation set.

This commit is contained in:
Eric Cochran
2017-07-19 11:29:06 -07:00
parent 76f50df2cc
commit 00694e9878
2 changed files with 20 additions and 0 deletions

View File

@@ -119,6 +119,8 @@ public final class Moshi {
@SuppressWarnings("unchecked") // Factories are required to return only matching JsonAdapters. @SuppressWarnings("unchecked") // Factories are required to return only matching JsonAdapters.
public <T> JsonAdapter<T> nextAdapter(JsonAdapter.Factory skipPast, Type type, public <T> JsonAdapter<T> nextAdapter(JsonAdapter.Factory skipPast, Type type,
Set<? extends Annotation> annotations) { Set<? extends Annotation> annotations) {
if (annotations == null) throw new NullPointerException("annotations == null");
type = Types.canonicalize(type); type = Types.canonicalize(type);
int skipPastIndex = factories.indexOf(skipPast); int skipPastIndex = factories.indexOf(skipPast);

View File

@@ -32,6 +32,7 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.annotation.Nullable;
import javax.crypto.KeyGenerator; import javax.crypto.KeyGenerator;
import okio.Buffer; import okio.Buffer;
import org.junit.Test; import org.junit.Test;
@@ -704,6 +705,23 @@ public final class MoshiTest {
@Uppercase @Uppercase
static String uppercaseString; static String uppercaseString;
@Test public void nextJsonAdapterDisallowsNullAnnotations() throws Exception {
JsonAdapter.Factory badFactory = new JsonAdapter.Factory() {
@Nullable @Override
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations,
Moshi moshi) {
return moshi.nextAdapter(this, type, null);
}
};
Moshi moshi = new Moshi.Builder().add(badFactory).build();
try {
moshi.adapter(Object.class);
fail();
} catch (NullPointerException expected) {
assertThat(expected).hasMessage("annotations == null");
}
}
@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())