Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ public OperationResponse deserialize(
.registerTypeAdapter(Predicate.class, new PredicateDeserializer())
.create();

int type = json.getAsJsonObject().get("type_i").getAsInt();
JsonElement typeElement = json.getAsJsonObject().get("type_i");
if (typeElement == null || typeElement.isJsonNull()) {
throw new JsonParseException(
"Missing required field 'type_i'. Your Horizon version may be outdated.");
}
int type = typeElement.getAsInt();
if (type < 0 || type >= AllOperationTypes.length) {
throw new IllegalArgumentException("Invalid operation type");
throw new JsonParseException(
"Unknown operation type: " + type + ". Your SDK version may be outdated.");
}

switch (AllOperationTypes[type]) {
Expand Down Expand Up @@ -88,7 +94,7 @@ public OperationResponse deserialize(
case RESTORE_FOOTPRINT:
return gson.fromJson(json, RestoreFootprintOperationResponse.class);
default:
throw new IllegalArgumentException("Invalid operation type");
throw new AssertionError("Unhandled operation type: " + AllOperationTypes[type]);
}
}
}
24 changes: 24 additions & 0 deletions src/test/java/org/stellar/sdk/responses/OperationResponseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import com.google.gson.JsonParseException;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -703,4 +705,26 @@ public void testSetTrustLineFlagsOperation() throws IOException {
assertArrayEquals(new Integer[] {4}, response.getClearFlags().toArray());
assertArrayEquals(new String[] {"clawback_enabled"}, response.getClearFlagStrings().toArray());
}

@Test
public void testDeserializeOperationWithMissingTypeI() {
String json = "{\"id\": \"12345\", \"type\": \"payment\"}";
JsonParseException exception =
assertThrows(
JsonParseException.class,
() -> GsonSingleton.getInstance().fromJson(json, OperationResponse.class));
assertTrue(exception.getMessage().contains("Missing required field 'type_i'"));
assertTrue(exception.getMessage().contains("Horizon version may be outdated"));
}

@Test
public void testDeserializeOperationWithUnknownTypeI() {
String json = "{\"id\": \"12345\", \"type\": \"unknown\", \"type_i\": 9999}";
JsonParseException exception =
assertThrows(
JsonParseException.class,
() -> GsonSingleton.getInstance().fromJson(json, OperationResponse.class));
assertTrue(exception.getMessage().contains("Unknown operation type: 9999"));
assertTrue(exception.getMessage().contains("SDK version may be outdated"));
}
}
Loading