From a16323db020fe9148bce6cd9dda87ea37db954dd Mon Sep 17 00:00:00 2001 From: Eric Cochran Date: Tue, 17 Jan 2017 17:51:04 -0800 Subject: [PATCH 1/2] Disallow Moshi.Builder.add(null) --- .../main/java/com/squareup/moshi/Moshi.java | 2 + .../java/com/squareup/moshi/MoshiTest.java | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/moshi/src/main/java/com/squareup/moshi/Moshi.java b/moshi/src/main/java/com/squareup/moshi/Moshi.java index 64844ed..39e83f3 100644 --- a/moshi/src/main/java/com/squareup/moshi/Moshi.java +++ b/moshi/src/main/java/com/squareup/moshi/Moshi.java @@ -182,11 +182,13 @@ public final class Moshi { } public Builder add(JsonAdapter.Factory factory) { + if (factory == null) throw new IllegalArgumentException("factory == null"); factories.add(factory); return this; } public Builder add(Object adapter) { + if (adapter == null) throw new IllegalArgumentException("adapter == null"); return add(AdapterMethodsFactory.get(adapter)); } diff --git a/moshi/src/test/java/com/squareup/moshi/MoshiTest.java b/moshi/src/test/java/com/squareup/moshi/MoshiTest.java index ab8b6f5..cea1d36 100644 --- a/moshi/src/test/java/com/squareup/moshi/MoshiTest.java +++ b/moshi/src/test/java/com/squareup/moshi/MoshiTest.java @@ -532,6 +532,63 @@ public final class MoshiTest { assertThat(adapter.toJson(null)).isEqualTo("null"); } + @Test public void addNullFails() throws Exception { + JsonAdapter jsonAdapter = new JsonAdapter() { + @Override public Object fromJson(JsonReader reader) throws IOException { + throw new AssertionError(); + } + + @Override public void toJson(JsonWriter writer, Object value) throws IOException { + throw new AssertionError(); + } + }; + Type type = Object.class; + Class annotation = Annotation.class; + Moshi.Builder builder = new Moshi.Builder(); + try { + builder.add((JsonAdapter.Factory) null); + fail(); + } catch (IllegalArgumentException expected) { + assertThat(expected).hasMessage("factory == null"); + } + try { + builder.add(null); + fail(); + } catch (IllegalArgumentException expected) { + assertThat(expected).hasMessage("adapter == null"); + } + try { + builder.add(null, null); + fail(); + } catch (IllegalArgumentException expected) { + assertThat(expected).hasMessage("type == null"); + } + try { + builder.add(type, null); + fail(); + } catch (IllegalArgumentException expected) { + assertThat(expected).hasMessage("jsonAdapter == null"); + } + try { + builder.add(null, null, null); + fail(); + } catch (IllegalArgumentException expected) { + assertThat(expected).hasMessage("type == null"); + } + try { + builder.add(type, null, null); + fail(); + } catch (IllegalArgumentException expected) { + assertThat(expected).hasMessage("annotation == null"); + } + try { + builder.add(type, annotation, null); + fail(); + } catch (IllegalArgumentException expected) { + assertThat(expected).hasMessage("jsonAdapter == null"); + } + } + @Test public void customJsonAdapter() throws Exception { Moshi moshi = new Moshi.Builder() .add(Pizza.class, new PizzaAdapter()) From fb3e9e8b478adc795b1e76d666f8fc2e0073f6bf Mon Sep 17 00:00:00 2001 From: Eric Cochran Date: Tue, 17 Jan 2017 18:08:18 -0800 Subject: [PATCH 2/2] Fix null --- moshi/src/test/java/com/squareup/moshi/MoshiTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/moshi/src/test/java/com/squareup/moshi/MoshiTest.java b/moshi/src/test/java/com/squareup/moshi/MoshiTest.java index cea1d36..cb0169c 100644 --- a/moshi/src/test/java/com/squareup/moshi/MoshiTest.java +++ b/moshi/src/test/java/com/squareup/moshi/MoshiTest.java @@ -546,13 +546,13 @@ public final class MoshiTest { Class annotation = Annotation.class; Moshi.Builder builder = new Moshi.Builder(); try { - builder.add((JsonAdapter.Factory) null); + builder.add((null)); fail(); } catch (IllegalArgumentException expected) { assertThat(expected).hasMessage("factory == null"); } try { - builder.add(null); + builder.add((Object) null); fail(); } catch (IllegalArgumentException expected) { assertThat(expected).hasMessage("adapter == null");