Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

[PAN-2721] Fix TopicParameter deserialization #1562

Merged
merged 4 commits into from
Jun 13, 2019
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 @@ -36,12 +36,11 @@ public class JsonRpcParameter {
* @return Returns the parameter cast as T if available, otherwise throws exception.
*/
public <T> T required(final Object[] params, final int index, final Class<T> paramClass) {
final Optional<T> optionalParam = optional(params, index, paramClass);
if (!optionalParam.isPresent()) {
throw new InvalidJsonRpcParameters("Missing required json rpc parameter at index " + index);
}

return optionalParam.get();
return optional(params, index, paramClass)
.orElseThrow(
() ->
new InvalidJsonRpcParameters(
"Missing required json rpc parameter at index " + index));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no functional change, just streamlined

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ public TopicsParameter deserialize(
List<String> topicsList = new ArrayList<>();

try {
// try standard method
// parse as list of lists
return jsonparser.readValueAs(TopicsParameter.class);
} catch (MismatchedInputException mie) {
// is there a single string value instead of expected list of list
// single list case
String topics = jsonparser.getText();
jsonparser.nextToken(); // consume end of array character
if (topics == null) {
return new TopicsParameter(Collections.singletonList(topicsList));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ public void jsonWithSingleAddressAndMultipleListsOfTopicsShouldSerializeSuccessf
.isEqualToComparingFieldByFieldRecursively(expectedFilterParameter);
}

@Test
public void jsonWithParamsInDifferentOrderShouldDeserializeIntoFilterParameterSuccessfully()
throws Exception {
final String jsonWithTopicsFirst =
"{\"topics\":[\"0x492e34c7da2a87c57444aa0f6143558999bceec63065f04557cfb20932e0d591\"], \"address\":\"0x8CdaF0CD259887258Bc13a92C0a6dA92698644C0\",\"fromBlock\":\"earliest\",\"toBlock\":\"latest\"}";
final String jsonWithTopicsLast =
"{\"address\":\"0x8CdaF0CD259887258Bc13a92C0a6dA92698644C0\",\"fromBlock\":\"earliest\",\"toBlock\":\"latest\",\"topics\":[\"0x492e34c7da2a87c57444aa0f6143558999bceec63065f04557cfb20932e0d591\"]}";

assertThat(readJsonAsFilterParameter(jsonWithTopicsFirst))
.isEqualToComparingFieldByFieldRecursively(readJsonAsFilterParameter(jsonWithTopicsLast));
}

private FilterParameter filterParameterWithAddresses(final String... addresses) {
return new FilterParameter("latest", "latest", Arrays.asList(addresses), null, null);
}
Expand All @@ -137,4 +149,8 @@ private JsonRpcRequest readJsonAsJsonRpcRequest(final String jsonWithSingleAddre
throws java.io.IOException {
return new ObjectMapper().readValue(jsonWithSingleAddress, JsonRpcRequest.class);
}

private FilterParameter readJsonAsFilterParameter(final String json) throws java.io.IOException {
return new ObjectMapper().readValue(json, FilterParameter.class);
}
}