Merge pull request #333 from square/eric.0719.nonnull_annotations

Fail earlier with null annotation set.
This commit is contained in:
Jesse Wilson
2017-07-19 15:07:41 -04:00
committed by GitHub
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.
public <T> JsonAdapter<T> nextAdapter(JsonAdapter.Factory skipPast, Type type,
Set<? extends Annotation> annotations) {
if (annotations == null) throw new NullPointerException("annotations == null");
type = Types.canonicalize(type);
int skipPastIndex = factories.indexOf(skipPast);

View File

@@ -32,6 +32,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import javax.crypto.KeyGenerator;
import okio.Buffer;
import org.junit.Test;
@@ -704,6 +705,23 @@ public final class MoshiTest {
@Uppercase
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 {
Moshi moshi = new Moshi.Builder()
.add(new UppercaseAdapterFactory())