From 55ec022c4d86f86e07bdec40802b35f2b82db525 Mon Sep 17 00:00:00 2001 From: Yury Kanetski Date: Fri, 22 Apr 2022 16:41:21 +0200 Subject: [PATCH] cherry pick: fix '&' char encoding (#1410) (#1413) --- .../mapbox/api/directions/v5/models/RouteOptions.java | 4 +++- .../api/directions/v5/models/RouteOptionsTest.java | 10 +++++----- .../src/test/resources/route_options_v5.json | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/RouteOptions.java b/services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/RouteOptions.java index 1cd072734..13b4c97a7 100644 --- a/services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/RouteOptions.java +++ b/services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/RouteOptions.java @@ -1019,7 +1019,9 @@ public URL toUrl(@NonNull String accessToken) { decodedUrl.getQuery(), decodedUrl.getRef() ); - return new URL(encodedUri.toASCIIString()); + // workaround to encode `&` chars in Strings like "me & you". + // need a better solution to fix encoding in general. + return new URL(encodedUri.toASCIIString().replace("%20&%20", "%20%26%20")); } catch (MalformedURLException | URISyntaxException ex) { throw new RuntimeException(ex); } diff --git a/services-directions-models/src/test/java/com/mapbox/api/directions/v5/models/RouteOptionsTest.java b/services-directions-models/src/test/java/com/mapbox/api/directions/v5/models/RouteOptionsTest.java index 16dd22af4..97301c043 100644 --- a/services-directions-models/src/test/java/com/mapbox/api/directions/v5/models/RouteOptionsTest.java +++ b/services-directions-models/src/test/java/com/mapbox/api/directions/v5/models/RouteOptionsTest.java @@ -28,7 +28,7 @@ public class RouteOptionsTest extends TestUtils { */ private static final String ROUTE_OPTIONS_JSON = "route_options_v5.json"; 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&avoid_maneuver_radius=200.0&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,point(11.0%20-22.0)&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"; + "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&avoid_maneuver_radius=200.0&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,point(11.0%20-22.0)&include=hot,hov2&approaches=;curb;&waypoints=0;1;2&waypoint_names=one;Serangoon%20Garden%20Market%20%26%20Food%20Centre;&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"; private static final String ACCESS_TOKEN = "pk.token"; private final String optionsJson = loadJsonFixture(ROUTE_OPTIONS_JSON); @@ -236,7 +236,7 @@ public void waypointIndicesStringIsValid_fromJson() { public void waypointNamesStringIsValid_fromJson() { RouteOptions routeOptions = RouteOptions.fromJson(optionsJson); - assertEquals(";two;", routeOptions.waypointNames()); + assertEquals("one;Serangoon Garden Market & Food Centre;", routeOptions.waypointNames()); } @Test @@ -663,7 +663,7 @@ private RouteOptions routeOptions() { .roundaboutExits(false) .voiceInstructions(true) .voiceUnits(DirectionsCriteria.METRIC) - .waypointNames(";two;") + .waypointNames("one;Serangoon Garden Market & Food Centre;") .waypointTargets(";12.2,21.2;") .waypointIndices("0;1;2") .alleyBias(0.75) @@ -739,8 +739,8 @@ private RouteOptions routeOptionsList() { .voiceInstructions(true) .voiceUnits(DirectionsCriteria.METRIC) .waypointNamesList(new ArrayList() {{ - add(null); - add("two"); + add("one"); + add("Serangoon Garden Market & Food Centre"); add(null); }}) .waypointTargetsList(new ArrayList() {{ diff --git a/services-directions-models/src/test/resources/route_options_v5.json b/services-directions-models/src/test/resources/route_options_v5.json index 2308a0ceb..79b2c6846 100644 --- a/services-directions-models/src/test/resources/route_options_v5.json +++ b/services-directions-models/src/test/resources/route_options_v5.json @@ -22,7 +22,7 @@ "voice_units": "metric", "approaches": ";curb;", "waypoints": "0;1;2", - "waypoint_names": ";two;", + "waypoint_names": "one;Serangoon Garden Market & Food Centre;", "waypoint_targets": ";12.2,21.2;", "snapping_include_closures": ";false;true", "alley_bias": 0.75,