Skip to content

Commit 518e883

Browse files
author
Łukasz Paczos
committed
fixes an issue where RouteOptions weren't deserialized from JSON DirectionsResponse
1 parent 78ecdcd commit 518e883

3 files changed

Lines changed: 2291 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ public static TypeAdapter<DirectionsResponse> typeAdapter(Gson gson) {
134134
* @since 3.0.0
135135
*/
136136
public static DirectionsResponse fromJson(@NonNull String json) {
137-
return fromJson(json, null, null);
137+
GsonBuilder gson = new GsonBuilder();
138+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
139+
gson.registerTypeAdapter(Point.class, new PointAsCoordinatesTypeAdapter());
140+
// rebuilding to ensure that underlying routes have assigned indices
141+
return gson.create().fromJson(json, DirectionsResponse.class).toBuilder().build();
138142
}
139143

140144
/**

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class DirectionsResponseTest extends TestUtils {
1717

1818
private static final String DIRECTIONS_V5_PRECISION6_FIXTURE = "directions_v5_precision_6.json";
1919
private static final String DIRECTIONS_V5_MULTIPLE_ROUTES = "directions_v5_multiple_routes.json";
20+
private static final String DIRECTIONS_V5_MULTIPLE_ROUTES_WITH_OPTIONS =
21+
"directions_v5_multiple_routes_with_options.json";
2022

2123
@Test
2224
public void sanity() throws Exception {
@@ -73,4 +75,14 @@ public void fromJson_multipleRoutesHaveCorrectIndices() throws Exception {
7375
assertEquals("0", routes.get(0).routeIndex());
7476
assertEquals("1", routes.get(1).routeIndex());
7577
}
78+
79+
@Test
80+
public void fromJson_deserializesOptions() throws Exception {
81+
String json = loadJsonFixture(DIRECTIONS_V5_MULTIPLE_ROUTES_WITH_OPTIONS);
82+
83+
List<DirectionsRoute> routes = DirectionsResponse.fromJson(json).routes();
84+
85+
assertNotNull(routes.get(0).routeOptions());
86+
assertNotNull(routes.get(1).routeOptions());
87+
}
7688
}

0 commit comments

Comments
 (0)