Skip to content

Commit

Permalink
Add missing check for null json token (#188)
Browse files Browse the repository at this point in the history
This would trigger an exception if the model is null.
  • Loading branch information
RicoYao authored and rahul-malik committed Mar 19, 2019
1 parent 06925b8 commit 2dd9ed8
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Examples/Java/Sources/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.annotation.Retention;
Expand Down Expand Up @@ -388,6 +389,10 @@ public void write(JsonWriter writer, Board value) throws IOException {

@Override
public Board read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
JsonElement tree = this.elementTypeAdapter.read(reader);
Board model = this.delegateTypeAdapter.fromJsonTree(tree);
Set<String> keys = tree.getAsJsonObject().keySet();
Expand Down
5 changes: 5 additions & 0 deletions Examples/Java/Sources/Everything.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.annotation.Retention;
Expand Down Expand Up @@ -1029,6 +1030,10 @@ public void write(JsonWriter writer, Everything value) throws IOException {

@Override
public Everything read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
JsonElement tree = this.elementTypeAdapter.read(reader);
Everything model = this.delegateTypeAdapter.fromJsonTree(tree);
Set<String> keys = tree.getAsJsonObject().keySet();
Expand Down
5 changes: 5 additions & 0 deletions Examples/Java/Sources/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.annotation.Retention;
Expand Down Expand Up @@ -208,6 +209,10 @@ public void write(JsonWriter writer, Image value) throws IOException {

@Override
public Image read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
JsonElement tree = this.elementTypeAdapter.read(reader);
Image model = this.delegateTypeAdapter.fromJsonTree(tree);
Set<String> keys = tree.getAsJsonObject().keySet();
Expand Down
5 changes: 5 additions & 0 deletions Examples/Java/Sources/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.annotation.Retention;
Expand Down Expand Up @@ -148,6 +149,10 @@ public void write(JsonWriter writer, Model value) throws IOException {

@Override
public Model read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
JsonElement tree = this.elementTypeAdapter.read(reader);
Model model = this.delegateTypeAdapter.fromJsonTree(tree);
Set<String> keys = tree.getAsJsonObject().keySet();
Expand Down
5 changes: 5 additions & 0 deletions Examples/Java/Sources/Pin.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.annotation.Retention;
Expand Down Expand Up @@ -646,6 +647,10 @@ public void write(JsonWriter writer, Pin value) throws IOException {

@Override
public Pin read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
JsonElement tree = this.elementTypeAdapter.read(reader);
Pin model = this.delegateTypeAdapter.fromJsonTree(tree);
Set<String> keys = tree.getAsJsonObject().keySet();
Expand Down
5 changes: 5 additions & 0 deletions Examples/Java/Sources/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.annotation.Retention;
Expand Down Expand Up @@ -422,6 +423,10 @@ public void write(JsonWriter writer, User value) throws IOException {

@Override
public User read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
JsonElement tree = this.elementTypeAdapter.read(reader);
User model = this.delegateTypeAdapter.fromJsonTree(tree);
Set<String> keys = tree.getAsJsonObject().keySet();
Expand Down
5 changes: 5 additions & 0 deletions Examples/Java/Sources/VariableSubtitution.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.annotation.Retention;
Expand Down Expand Up @@ -238,6 +239,10 @@ public void write(JsonWriter writer, VariableSubtitution value) throws IOExcepti

@Override
public VariableSubtitution read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
JsonElement tree = this.elementTypeAdapter.read(reader);
VariableSubtitution model = this.delegateTypeAdapter.fromJsonTree(tree);
Set<String> keys = tree.getAsJsonObject().keySet();
Expand Down
5 changes: 5 additions & 0 deletions Sources/Core/JavaModelRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ public struct JavaModelRenderer: JavaFileRenderer {
className + " read(JsonReader reader)",
["IOException"]
) { [
JavaIR.ifBlock(condition: "reader.peek() == JsonToken.NULL") { [
"reader.nextNull();",
"return null;",
] },
"JsonElement tree = this.elementTypeAdapter.read(reader);",
className + " model = this.delegateTypeAdapter.fromJsonTree(tree);",
"Set<String> keys = tree.getAsJsonObject().keySet();",
Expand Down Expand Up @@ -293,6 +297,7 @@ public struct JavaModelRenderer: JavaFileRenderer {
"com.google.gson.TypeAdapterFactory",
"com.google.gson.reflect.TypeToken",
"com.google.gson.stream.JsonReader",
"com.google.gson.stream.JsonToken",
"com.google.gson.stream.JsonWriter",
"java.io.IOException",
"java.util.Date",
Expand Down

0 comments on commit 2dd9ed8

Please sign in to comment.