Skip to content

Commit 2b139db

Browse files
committed
fix critical bug in client-hc for Matrix API
1 parent 3a0f976 commit 2b139db

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public OkHttpClient getDownloader() {
8282
protected JsonNode createPostRequest(GHMRequest ghRequest) {
8383
if (ghRequest.getHints().getObject("profile", null) != null)
8484
throw new IllegalArgumentException("use setProfile instead of hint 'profile'");
85+
if (ghRequest.getProfile() == null)
86+
throw new IllegalArgumentException("profile cannot be empty");
8587
if (ghRequest.getHints().getObject("fail_fast", null) != null)
8688
throw new IllegalArgumentException("use setFailFast instead of hint 'fail_fast'");
8789

@@ -114,6 +116,7 @@ protected JsonNode createPostRequest(GHMRequest ghRequest) {
114116
putStrings(requestJson, "snap_preventions", ghRequest.getSnapPreventions());
115117
putStrings(requestJson, "out_arrays", ghRequest.getOutArrays());
116118
requestJson.put("fail_fast", ghRequest.getFailFast());
119+
requestJson.put("profile", ghRequest.getProfile());
117120

118121
Map<String, Object> hintsMap = ghRequest.getHints().toMap();
119122
for (String hintKey : hintsMap.keySet()) {

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static GHMRequest createRequest() {
3535
new GHPoint(51.473685, -0.211487)
3636
));
3737
req.setOutArrays(Arrays.asList("weights"));
38+
req.setProfile("car");
3839
return req;
3940
}
4041

@@ -80,6 +81,7 @@ public void testReadingMatrixConnectionsNotFound_noFailFast() throws IOException
8081
));
8182
req.setOutArrays(Arrays.asList("weights", "distances", "times"));
8283
req.setFailFast(false);
84+
req.setProfile("car");
8385

8486
MatrixResponse rsp = matrixWeb.route(req);
8587
assertFalse(rsp.hasErrors());
@@ -108,6 +110,7 @@ public void testReadingMatrixPointsNotFound_noFailFast() throws IOException {
108110
new GHPoint(2, 3),
109111
new GHPoint(4, 5)
110112
));
113+
req.setProfile("car");
111114
req.setOutArrays(Arrays.asList("weights", "distances", "times"));
112115
req.setFailFast(false);
113116

@@ -187,19 +190,32 @@ public void testReadingWeights_TimesAndDistances() throws IOException {
187190
@Test
188191
public void noProfileWhenNotSpecified() {
189192
GHMatrixBatchRequester requester = new GHMatrixBatchRequester("url");
190-
JsonNode json = requester.createPostRequest(new GHMRequest().setOutArrays(Collections.singletonList("weights")).setPoints(Arrays.asList(new GHPoint(11, 12))));
191-
assertEquals("{\"points\":[[12.0,11.0]],\"out_arrays\":[\"weights\"],\"fail_fast\":true}", json.toString());
193+
JsonNode json = requester.createPostRequest(new GHMRequest().setOutArrays(Collections.singletonList("weights")).
194+
setPoints(Arrays.asList(new GHPoint(11, 12))).setProfile("car"));
195+
assertEquals("{\"points\":[[12.0,11.0]],\"out_arrays\":[\"weights\"],\"fail_fast\":true,\"profile\":\"car\"}", json.toString());
196+
}
197+
198+
@Test
199+
public void hasProfile() {
200+
GHMatrixAbstractRequester requester = createRequester("url");
201+
GHMRequest ghmRequest = new GHMRequest();
202+
ghmRequest.setOutArrays(Collections.singletonList("weights"));
203+
ghmRequest.setPoints(Arrays.asList(new GHPoint(11, 12)));
204+
ghmRequest.setProfile("bike");
205+
JsonNode json = requester.createPostRequest(ghmRequest);
206+
assertEquals("{\"points\":[[12.0,11.0]],\"out_arrays\":[\"weights\"],\"fail_fast\":true,\"profile\":\"bike\"}", json.toString());
192207
}
193208

194209
@Test
195210
public void hasHintsWhenSpecified() {
196211
GHMatrixAbstractRequester requester = createRequester("url");
197212
GHMRequest ghmRequest = new GHMRequest();
213+
ghmRequest.setProfile("car");
198214
ghmRequest.putHint("some_property", "value");
199215
ghmRequest.setOutArrays(Collections.singletonList("weights"));
200216
ghmRequest.setPoints(Arrays.asList(new GHPoint(11, 12)));
201217
JsonNode json = requester.createPostRequest(ghmRequest);
202-
assertEquals("{\"points\":[[12.0,11.0]],\"out_arrays\":[\"weights\"],\"fail_fast\":true,\"some_property\":\"value\"}", json.toString());
218+
assertEquals("{\"points\":[[12.0,11.0]],\"out_arrays\":[\"weights\"],\"fail_fast\":true,\"profile\":\"car\",\"some_property\":\"value\"}", json.toString());
203219

204220
ghmRequest.putHint("profile", "car");
205221
Exception ex = assertThrows(IllegalArgumentException.class, () -> requester.createPostRequest(ghmRequest));

0 commit comments

Comments
 (0)