Skip to content

Commit 847ea68

Browse files
committed
avoid serializing empty lists
1 parent fc1a4e6 commit 847ea68

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

client-hc/src/main/java/com/graphhopper/api/GHMatrixBatchRequester.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,24 @@ public MatrixResponse route(GHMRequest ghRequest) {
8989
outArraysList.add("weights");
9090
}
9191

92-
ArrayNode outArrayListJson = objectMapper.createArrayNode();
93-
for (String str : outArraysList) {
94-
outArrayListJson.add(str);
95-
}
96-
9792
boolean hasElevation = false;
9893
if (ghRequest.identicalLists) {
99-
requestJson.putArray("points").addAll(createPointList(ghRequest.getFromPoints()));
100-
requestJson.putArray("point_hints").addAll(createStringList(ghRequest.getFromPointHints()));
101-
requestJson.putArray("curbsides").addAll(createStringList(ghRequest.getFromCurbsides()));
94+
createPointArray(requestJson, "points", ghRequest.getFromPoints());
95+
createStringArray(requestJson, "point_hints", ghRequest.getFromPointHints());
96+
createStringArray(requestJson, "curbsides", ghRequest.getFromCurbsides());
10297
} else {
103-
ArrayNode fromPointList = createPointList(ghRequest.getFromPoints());
104-
ArrayNode toPointList = createPointList(ghRequest.getToPoints());
105-
requestJson.putArray("from_points").addAll(fromPointList);
106-
requestJson.putArray("from_point_hints").addAll(createStringList(ghRequest.getFromPointHints()));
107-
requestJson.putArray("to_points").addAll(toPointList);
108-
requestJson.putArray("to_point_hints").addAll(createStringList(ghRequest.getToPointHints()));
109-
requestJson.putArray("from_curbsides").addAll(createStringList(ghRequest.getFromCurbsides()));
110-
requestJson.putArray("to_curbsides").addAll(createStringList(ghRequest.getToPointHints()));
98+
createPointArray(requestJson, "from_points", ghRequest.getFromPoints());
99+
createStringArray(requestJson, "from_point_hints", ghRequest.getFromPointHints());
100+
101+
createPointArray(requestJson, "to_points", ghRequest.getToPoints());
102+
createStringArray(requestJson, "to_point_hints", ghRequest.getToPointHints());
103+
104+
createStringArray(requestJson, "from_curbsides", ghRequest.getFromCurbsides());
105+
createStringArray(requestJson, "to_curbsides", ghRequest.getToCurbsides());
111106
}
112107

113-
requestJson.putArray("snap_preventions").addAll(createStringList(ghRequest.getSnapPreventions()));
114-
requestJson.putArray("out_arrays").addAll(outArrayListJson);
108+
createStringArray(requestJson, "snap_preventions", ghRequest.getSnapPreventions());
109+
createStringArray(requestJson, "out_arrays", outArraysList);
115110
requestJson.put("vehicle", ghRequest.getVehicle());
116111
requestJson.put("elevation", hasElevation);
117112
requestJson.put("fail_fast", ghRequest.getFailFast());
@@ -222,22 +217,26 @@ protected String postJson(String url, JsonNode data) throws IOException {
222217
}
223218
}
224219

225-
private ArrayNode createStringList(List<String> list) {
220+
private void createStringArray(ObjectNode requestJson, String name, List<String> stringList) {
221+
if (stringList.isEmpty())
222+
return;
226223
ArrayNode outList = objectMapper.createArrayNode();
227-
for (String str : list) {
224+
for (String str : stringList) {
228225
outList.add(str);
229226
}
230-
return outList;
227+
requestJson.putArray(name).addAll(outList);
231228
}
232229

233-
private ArrayNode createPointList(List<GHPoint> list) {
230+
private void createPointArray(ObjectNode requestJson, String name, List<GHPoint> pList) {
231+
if (pList.isEmpty())
232+
return;
234233
ArrayNode outList = objectMapper.createArrayNode();
235-
for (GHPoint p : list) {
234+
for (GHPoint p : pList) {
236235
ArrayNode entry = objectMapper.createArrayNode();
237236
entry.add(p.lon);
238237
entry.add(p.lat);
239238
outList.add(entry);
240239
}
241-
return outList;
240+
requestJson.putArray(name).addAll(outList);
242241
}
243242
}

client-hc/src/test/java/com/graphhopper/api/GraphHopperWebIT.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Arrays;
2525
import java.util.Collection;
2626
import java.util.List;
27+
import java.util.concurrent.atomic.AtomicInteger;
2728

2829
import static org.junit.Assert.*;
2930

@@ -341,6 +342,28 @@ protected String postJson(String url, JsonNode data) throws IOException {
341342
assertEquals("xy", req.getVehicle());
342343
}
343344

345+
@Test
346+
public void doNotIncludeEmptyCurbsidesList() {
347+
final AtomicInteger counter = new AtomicInteger(0);
348+
final GraphHopperMatrixWeb ghMatrix = new GraphHopperMatrixWeb(new GHMatrixBatchRequester() {
349+
@Override
350+
protected String postJson(String url, JsonNode data) throws IOException {
351+
assertFalse(data.has("curbsides"));
352+
assertTrue(data.has("points"));
353+
counter.incrementAndGet();
354+
return "";
355+
}
356+
});
357+
GHMRequest req = new GHMRequest();
358+
req.addPoint(new GHPoint(49.6724, 11.3494));
359+
req.addPoint(new GHPoint(49.6550, 11.4180));
360+
try {
361+
ghMatrix.route(req);
362+
} catch (Exception ex) {
363+
}
364+
assertEquals(1, counter.get());
365+
}
366+
344367
@Test
345368
public void testUnknownInstructionSign() throws IOException {
346369
// Actual path for the request: point=48.354413%2C8.676335&point=48.35442%2C8.676345

0 commit comments

Comments
 (0)