Skip to content

Commit d618194

Browse files
authored
do not add null values to the url (#1302)
1 parent 6fd96dd commit d618194

2 files changed

Lines changed: 107 additions & 31 deletions

File tree

services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/RouteOptions.java

Lines changed: 89 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -860,36 +860,95 @@ public URL toUrl(@NonNull String accessToken) {
860860
.append(String.format("/%s", profile()))
861861
.append(String.format("/%s", coordinates()))
862862
.append(String.format("?access_token=%s", accessToken))
863-
.append(String.format("&alternatives=%s", alternatives()))
864-
.append(String.format("&geometries=%s", geometries()))
865-
.append(String.format("&overview=%s", overview()))
866-
.append(String.format("&radiuses=%s", radiuses()))
867-
.append(String.format("&steps=%s", steps()))
868-
.append(String.format("&bearings=%s", bearings()))
869-
.append(String.format("&layers=%s", layers()))
870-
.append(String.format("&continue_straight=%s", continueStraight()))
871-
.append(String.format("&annotations=%s", annotations()))
872-
.append(String.format("&language=%s", language()))
873-
.append(String.format("&roundabout_exits=%s", roundaboutExits()))
874-
.append(String.format("&voice_instructions=%s", voiceInstructions()))
875-
.append(String.format("&banner_instructions=%s", bannerInstructions()))
876-
.append(String.format("&voice_units=%s", voiceUnits()))
877-
.append(String.format("&exclude=%s", exclude()))
878-
.append(String.format("&include=%s", include()))
879-
.append(String.format("&approaches=%s", approaches()))
880-
.append(String.format("&waypoints=%s", waypointIndices()))
881-
.append(String.format("&waypoint_names=%s", waypointNames()))
882-
.append(String.format("&waypoint_targets=%s", waypointTargets()))
883-
.append(String.format("&enable_refresh=%s", enableRefresh()))
884-
.append(String.format("&walking_speed=%s", walkingSpeed()))
885-
.append(String.format("&walkway_bias=%s", walkwayBias()))
886-
.append(String.format("&alley_bias=%s", alleyBias()))
887-
.append(String.format("&snapping_include_closures=%s", snappingIncludeClosures()))
888-
.append(String.format("&arrive_by=%s", arriveBy()))
889-
.append(String.format("&depart_at=%s", departAt()))
890-
.append(String.format("&max_height=%s", maxHeight()))
891-
.append(String.format("&max_width=%s", maxWidth()))
892-
.append(String.format("&metadata=%s", metadata()));
863+
.append(String.format("&geometries=%s", geometries()));
864+
865+
if (alternatives() != null) {
866+
sb.append(String.format("&alternatives=%s", alternatives()));
867+
}
868+
if (overview() != null) {
869+
sb.append(String.format("&overview=%s", overview()));
870+
}
871+
if (radiuses() != null) {
872+
sb.append(String.format("&radiuses=%s", radiuses()));
873+
}
874+
if (steps() != null) {
875+
sb.append(String.format("&steps=%s", steps()));
876+
}
877+
if (bearings() != null) {
878+
sb.append(String.format("&bearings=%s", bearings()));
879+
}
880+
if (layers() != null) {
881+
sb.append(String.format("&layers=%s", layers()));
882+
}
883+
if (continueStraight() != null) {
884+
sb.append(String.format("&continue_straight=%s", continueStraight()));
885+
}
886+
if (annotations() != null) {
887+
sb.append(String.format("&annotations=%s", annotations()));
888+
}
889+
if (language() != null) {
890+
sb.append(String.format("&language=%s", language()));
891+
}
892+
if (roundaboutExits() != null) {
893+
sb.append(String.format("&roundabout_exits=%s", roundaboutExits()));
894+
}
895+
if (voiceInstructions() != null) {
896+
sb.append(String.format("&voice_instructions=%s", voiceInstructions()));
897+
}
898+
if (bannerInstructions() != null) {
899+
sb.append(String.format("&banner_instructions=%s", bannerInstructions()));
900+
}
901+
if (voiceUnits() != null) {
902+
sb.append(String.format("&voice_units=%s", voiceUnits()));
903+
}
904+
if (exclude() != null) {
905+
sb.append(String.format("&exclude=%s", exclude()));
906+
}
907+
if (include() != null) {
908+
sb.append(String.format("&include=%s", include()));
909+
}
910+
if (approaches() != null) {
911+
sb.append(String.format("&approaches=%s", approaches()));
912+
}
913+
if (waypointIndices() != null) {
914+
sb.append(String.format("&waypoints=%s", waypointIndices()));
915+
}
916+
if (waypointNames() != null) {
917+
sb.append(String.format("&waypoint_names=%s", waypointNames()));
918+
}
919+
if (waypointTargets() != null) {
920+
sb.append(String.format("&waypoint_targets=%s", waypointTargets()));
921+
}
922+
if (enableRefresh() != null) {
923+
sb.append(String.format("&enable_refresh=%s", enableRefresh()));
924+
}
925+
if (walkingSpeed() != null) {
926+
sb.append(String.format("&walking_speed=%s", walkingSpeed()));
927+
}
928+
if (walkwayBias() != null) {
929+
sb.append(String.format("&walkway_bias=%s", walkwayBias()));
930+
}
931+
if (alleyBias() != null) {
932+
sb.append(String.format("&alley_bias=%s", alleyBias()));
933+
}
934+
if (snappingIncludeClosures() != null) {
935+
sb.append(String.format("&snapping_include_closures=%s", snappingIncludeClosures()));
936+
}
937+
if (arriveBy() != null) {
938+
sb.append(String.format("&arrive_by=%s", arriveBy()));
939+
}
940+
if (departAt() != null) {
941+
sb.append(String.format("&depart_at=%s", departAt()));
942+
}
943+
if (maxHeight() != null) {
944+
sb.append(String.format("&max_height=%s", maxHeight()));
945+
}
946+
if (maxWidth() != null) {
947+
sb.append(String.format("&max_width=%s", maxWidth()));
948+
}
949+
if (metadata() != null) {
950+
sb.append(String.format("&metadata=%s", metadata()));
951+
}
893952

894953
try {
895954
return new URL(sb.toString());

services-directions-models/src/test/java/com/mapbox/api/directions/v5/models/RouteOptionsTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class RouteOptionsTest extends TestUtils {
1818
* Always update this file when new option is introduced.
1919
*/
2020
private static final String ROUTE_OPTIONS_JSON = "route_options_v5.json";
21-
private static final String ROUTE_OPTIONS_URL = "https://api.mapbox.com/directions/v5/mapbox/driving/-122.4003312,37.7736941;-122.4187529,37.7689715;-122.4255172,37.7775835?access_token=pk.token&alternatives=false&geometries=polyline6&overview=full&radiuses=;unlimited;5.1&steps=true&bearings=0,90;90,0;&layers=-42;;0&continue_straight=false&annotations=congestion,distance,duration&language=ru&roundabout_exits=false&voice_instructions=true&banner_instructions=true&voice_units=metric&exclude=toll,ferry&include=hot,hov2&approaches=;curb;&waypoints=0;1;2&waypoint_names=;two;&waypoint_targets=;12.2,21.2;&enable_refresh=true&walking_speed=5.11&walkway_bias=-0.2&alley_bias=0.75&snapping_include_closures=;false;true&arrive_by=2021-01-01'T'01:01&depart_at=2021-02-02'T'02:02&max_height=1.5&max_width=1.4&metadata=true";
21+
private static final String ROUTE_OPTIONS_URL = "https://api.mapbox.com/directions/v5/mapbox/driving/-122.4003312,37.7736941;-122.4187529,37.7689715;-122.4255172,37.7775835?access_token=pk.token&geometries=polyline6&alternatives=false&overview=full&radiuses=;unlimited;5.1&steps=true&bearings=0,90;90,0;&layers=-42;;0&continue_straight=false&annotations=congestion,distance,duration&language=ru&roundabout_exits=false&voice_instructions=true&banner_instructions=true&voice_units=metric&exclude=toll,ferry&include=hot,hov2&approaches=;curb;&waypoints=0;1;2&waypoint_names=;two;&waypoint_targets=;12.2,21.2;&enable_refresh=true&walking_speed=5.11&walkway_bias=-0.2&alley_bias=0.75&snapping_include_closures=;false;true&arrive_by=2021-01-01'T'01:01&depart_at=2021-02-02'T'02:02&max_height=1.5&max_width=1.4&metadata=true";
2222
private static final String ACCESS_TOKEN = "pk.token";
2323

2424
private final String optionsJson = loadJsonFixture(ROUTE_OPTIONS_JSON);
@@ -347,6 +347,23 @@ public void routeOptions_toUrl() {
347347
assertEquals(ROUTE_OPTIONS_URL, url.toString());
348348
}
349349

350+
@Test
351+
public void routeOptionsWithDefaults_toUrl() {
352+
String expectedUrl = "https://api.mapbox.com/directions/v5/mapbox/driving/-122.4003312,37.7736941;-122.4187529,37.7689715?access_token=pk.token&geometries=polyline6";
353+
List<Point> coordinates = new ArrayList<>();
354+
coordinates.add(Point.fromLngLat(-122.4003312, 37.7736941));
355+
coordinates.add(Point.fromLngLat(-122.4187529, 37.7689715));
356+
357+
RouteOptions options = RouteOptions.builder()
358+
.profile(DirectionsCriteria.PROFILE_DRIVING)
359+
.coordinatesList(coordinates)
360+
.build();
361+
362+
URL url = options.toUrl(ACCESS_TOKEN);
363+
364+
assertEquals(expectedUrl, url.toString());
365+
}
366+
350367
/**
351368
* Fills up all the options using string variants. Values need ot be equal to the ones in {@link #optionsJson}.
352369
*/

0 commit comments

Comments
 (0)