Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Fix json formatter for consistency with SQL (#492)
Browse files Browse the repository at this point in the history
* Remove row field in json response

* Fix IT
  • Loading branch information
dai-chen authored May 27, 2020
1 parent 5db0412 commit 5ae2203
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void testQueryEndpointShouldOK() throws IOException {
" \"type\": \"string\"\n" +
" }],\n" +
" \"total\": 1,\n" +
" \"datarows\": [{\"row\": [\"hello\"]}],\n" +
" \"datarows\": [[\"hello\"]],\n" +
" \"size\": 1\n" +
"}\n",
response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public void testSourceFieldQuery() throws IOException {
" }],\n" +
" \"total\": 2,\n" +
" \"datarows\": [\n" +
" {\"row\": [\"hello\"]},\n" +
" {\"row\": [\"world\"]}\n" +
" [\"hello\"],\n" +
" [\"world\"]\n" +
" ],\n" +
" \"size\": 2\n" +
"}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,17 @@ public Object buildJsonObject(QueryResult response) {

response.columnNameTypes().forEach((name, type) -> json.column(new Column(name, type)));

json.datarows(fetchDataRows(response));
return json.build();
}

private Object[][] fetchDataRows(QueryResult response) {
Object[][] rows = new Object[response.size()][];
int i = 0;
for (Object[] values : response) {
json.row(new DataRow(values));
rows[i++] = values;
}
return json.build();
return rows;
}

/**
Expand All @@ -74,8 +81,7 @@ public static class JsonResponse {
@Singular("column")
private final List<Column> schema;

@Singular("row")
private final List<DataRow> datarows;
private final Object[][] datarows;

private long total;
private long size;
Expand All @@ -88,10 +94,4 @@ public static class Column {
private final String type;
}

@RequiredArgsConstructor
@Getter
public static class DataRow {
private final Object[] row;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void formatResponse() {
SimpleJsonResponseFormatter formatter = new SimpleJsonResponseFormatter(COMPACT);
assertEquals(
"{\"schema\":[{\"name\":\"firstname\",\"type\":\"string\"},{\"name\":\"age\",\"type\":\"integer\"}]," +
"\"total\":2,\"datarows\":[{\"row\":[\"John\",20]},{\"row\":[\"Smith\",30]}],\"size\":2}",
"\"total\":2,\"datarows\":[[\"John\",20],[\"Smith\",30]],\"size\":2}",
formatter.format(response)
);
}
Expand All @@ -65,14 +65,14 @@ void formatResponsePretty() {
" ],\n" +
" \"total\": 2,\n" +
" \"datarows\": [\n" +
" {\"row\": [\n" +
" [\n" +
" \"John\",\n" +
" 20\n" +
" ]},\n" +
" {\"row\": [\n" +
" ],\n" +
" [\n" +
" \"Smith\",\n" +
" 30\n" +
" ]}\n" +
" ]\n" +
" ],\n" +
" \"size\": 2\n" +
"}",
Expand Down

0 comments on commit 5ae2203

Please sign in to comment.