fix(api): homogenize error messages when required parameters are set to null (#822)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
glethuillier 6 years ago committed by Lucas Saldanha
parent 60a828b5f0
commit 46d0d4890a
  1. 20
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/JsonRpcRequest.java
  2. 2
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/parameters/JsonRpcParameter.java
  3. 22
      ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/AdminAddPeerTest.java
  4. 25
      ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/EthSendRawTransactionTest.java
  5. 25
      ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/EeaSendRawTransactionTest.java

@ -74,12 +74,28 @@ public class JsonRpcRequest {
@JsonIgnore @JsonIgnore
public int getParamLength() { public int getParamLength() {
return params.length; return hasParams() ? params.length : 0;
}
@JsonIgnore
public boolean hasParams() {
// Null Object: "params":null
if (params == null) {
return false;
}
// Null Array: "params":[null]
if (params.length == 0 || params[0] == null) {
return false;
}
return true;
} }
@JsonSetter("id") @JsonSetter("id")
protected void setId(final JsonRpcRequestId id) { protected void setId(final JsonRpcRequestId id) {
// If an id is explicitly set, its not a notification // If an id is explicitly set, it is not a notification
isNotification = false; isNotification = false;
this.id = id; this.id = id;
} }

@ -57,7 +57,7 @@ public class JsonRpcParameter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> Optional<T> optional( public <T> Optional<T> optional(
final Object[] params, final int index, final Class<T> paramClass) { final Object[] params, final int index, final Class<T> paramClass) {
if (params == null || params.length <= index) { if (params == null || params.length <= index || params[index] == null) {
return Optional.empty(); return Optional.empty();
} }

@ -55,6 +55,28 @@ public class AdminAddPeerTest {
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
} }
@Test
public void requestHasNullObjectParameter() {
final JsonRpcRequest request = new JsonRpcRequest("2.0", "admin_addPeer", null);
final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(request.getId(), JsonRpcError.INVALID_PARAMS);
final JsonRpcResponse actualResponse = method.response(request);
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
}
@Test
public void requestHasNullArrayParameter() {
final JsonRpcRequest request = new JsonRpcRequest("2.0", "admin_addPeer", new String[] {null});
final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(request.getId(), JsonRpcError.INVALID_PARAMS);
final JsonRpcResponse actualResponse = method.response(request);
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
}
@Test @Test
public void requestHasInvalidEnode() { public void requestHasInvalidEnode() {
final JsonRpcRequest request = final JsonRpcRequest request =

@ -64,6 +64,31 @@ public class EthSendRawTransactionTest {
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
} }
@Test
public void requestHasNullObjectParameter() {
final JsonRpcRequest request = new JsonRpcRequest("2.0", "eth_sendRawTransaction", null);
final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(request.getId(), JsonRpcError.INVALID_PARAMS);
final JsonRpcResponse actualResponse = method.response(request);
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
}
@Test
public void requestHasNullArrayParameter() {
final JsonRpcRequest request =
new JsonRpcRequest("2.0", "eth_sendRawTransaction", new String[] {null});
final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(request.getId(), JsonRpcError.INVALID_PARAMS);
final JsonRpcResponse actualResponse = method.response(request);
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
}
@Test @Test
public void invalidTransactionRlpDecoding() { public void invalidTransactionRlpDecoding() {
final String rawTransaction = "0x00"; final String rawTransaction = "0x00";

@ -106,6 +106,31 @@ public class EeaSendRawTransactionTest {
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse); assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
} }
@Test
public void requestHasNullObjectParameter() {
final JsonRpcRequest request = new JsonRpcRequest("2.0", "eea_sendRawTransaction", null);
final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(request.getId(), JsonRpcError.INVALID_PARAMS);
final JsonRpcResponse actualResponse = method.response(request);
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
}
@Test
public void requestHasNullArrayParameter() {
final JsonRpcRequest request =
new JsonRpcRequest("2.0", "eea_sendRawTransaction", new String[] {null});
final JsonRpcResponse expectedResponse =
new JsonRpcErrorResponse(request.getId(), JsonRpcError.INVALID_PARAMS);
final JsonRpcResponse actualResponse = method.response(request);
assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
}
@Test @Test
public void invalidTransactionRlpDecoding() { public void invalidTransactionRlpDecoding() {
final String rawTransaction = "0x00"; final String rawTransaction = "0x00";

Loading…
Cancel
Save