Skip to content

Commit 1a7f7f1

Browse files
committed
fix: apply dkhawk's changes - countdown maxDepth and updated tests
1 parent f37823d commit 1a7f7f1

2 files changed

Lines changed: 32 additions & 12 deletions

File tree

library/src/main/java/com/google/maps/android/data/geojson/GeoJsonParser.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@ private static LatLngBounds parseBoundingBox(JSONArray coordinates) throws JSONE
181181
private static final int MAX_GEOMETRY_DEPTH = 20;
182182

183183
public static Geometry parseGeometry(JSONObject geoJsonGeometry) {
184-
return parseGeometry(geoJsonGeometry, 0);
184+
return parseGeometry(geoJsonGeometry, MAX_GEOMETRY_DEPTH);
185185
}
186186

187-
private static Geometry parseGeometry(JSONObject geoJsonGeometry, int depth) {
188-
if (depth > MAX_GEOMETRY_DEPTH) {
189-
Log.w(LOG_TAG, "GeoJSON geometry nesting depth exceeds maximum (" + MAX_GEOMETRY_DEPTH + "), ignoring.");
187+
public static Geometry parseGeometry(JSONObject geoJsonGeometry, int maxDepth) {
188+
if (maxDepth < 0) {
189+
Log.w(LOG_TAG, "GeoJSON geometry nesting depth limit exhausted, ignoring.");
190190
return null;
191191
}
192192
try {
@@ -201,7 +201,7 @@ private static Geometry parseGeometry(JSONObject geoJsonGeometry, int depth) {
201201
// No geometries or coordinates array
202202
return null;
203203
}
204-
return createGeometry(geometryType, geometryArray, depth);
204+
return createGeometry(geometryType, geometryArray, maxDepth);
205205
} catch (JSONException e) {
206206
return null;
207207
}
@@ -250,7 +250,7 @@ private static HashMap<String, String> parseProperties(JSONObject properties)
250250
* @return Geometry object
251251
* @throws JSONException if the coordinates or geometries could be parsed
252252
*/
253-
private static Geometry createGeometry(String geometryType, JSONArray geometryArray, int depth)
253+
private static Geometry createGeometry(String geometryType, JSONArray geometryArray, int maxDepth)
254254
throws JSONException {
255255
switch (geometryType) {
256256
case POINT:
@@ -266,7 +266,7 @@ private static Geometry createGeometry(String geometryType, JSONArray geometryAr
266266
case MULTIPOLYGON:
267267
return createMultiPolygon(geometryArray);
268268
case GEOMETRY_COLLECTION:
269-
return createGeometryCollection(geometryArray, depth + 1);
269+
return createGeometryCollection(geometryArray, maxDepth - 1);
270270
}
271271
return null;
272272
}
@@ -371,13 +371,13 @@ private static GeoJsonMultiPolygon createMultiPolygon(JSONArray coordinates)
371371
* @return GeoJsonGeometryCollection object
372372
* @throws JSONException if geometries cannot be parsed
373373
*/
374-
private static GeoJsonGeometryCollection createGeometryCollection(JSONArray geometries, int depth)
374+
private static GeoJsonGeometryCollection createGeometryCollection(JSONArray geometries, int maxDepth)
375375
throws JSONException {
376376
ArrayList<Geometry> geometryCollectionElements
377377
= new ArrayList<>();
378378
for (int i = 0; i < geometries.length(); i++) {
379379
JSONObject geometryElement = geometries.getJSONObject(i);
380-
Geometry geometry = parseGeometry(geometryElement, depth);
380+
Geometry geometry = parseGeometry(geometryElement, maxDepth);
381381
if (geometry != null) {
382382
// Do not add geometries that could not be parsed
383383
geometryCollectionElements.add(geometry);

library/src/test/java/com/google/maps/android/data/geojson/GeoJsonParserTest.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,17 +414,37 @@ public void testDeeplyNestedGeometryCollection_doesNotThrowStackOverflow() throw
414414
GeoJsonParser.parseGeometry(deeplyNested);
415415
} catch (StackOverflowError e) {
416416
throw new AssertionError(
417-
"StackOverflowError thrown for deeply nested GeometryCollection — fix tidak bekerja!", e);
417+
"StackOverflowError thrown for deeply nested GeometryCollection — fix did not work!", e);
418418
}
419419
}
420420

421421

422422
@Test
423+
public void testGeometryBeyondMaxDepth_returnsNull() throws Exception {
424+
JSONObject point = new JSONObject();
425+
point.put("type", "Point");
426+
point.put("coordinates", new org.json.JSONArray("[0, 0]"));
427+
Geometry result = GeoJsonParser.parseGeometry(point, -1);
428+
assertNull("Geometry exceeding max depth (countdown < 0) should be null", result);
429+
}
430+
431+
@Test
432+
public void testCustomMaxDepth_respectsLimit() throws Exception {
433+
JSONObject point = new JSONObject();
434+
point.put("type", "Point");
435+
point.put("coordinates", new org.json.JSONArray("[0, 0]"));
436+
Geometry valid = GeoJsonParser.parseGeometry(point, 0);
437+
assertNotNull("Geometry at maxDepth 0 should not be null", valid);
438+
Geometry invalid = GeoJsonParser.parseGeometry(point, -1);
439+
assertNull("Geometry at maxDepth -1 should be null", invalid);
440+
}
441+
442+
@Test
423443
public void testShallowNestedGeometryCollection_parsedCorrectly() throws Exception {
424444
JSONObject shallow = buildNestedGeometryCollection(3);
425445
Geometry result = GeoJsonParser.parseGeometry(shallow);
426-
assertNotNull("GeometryCollection dengan nesting normal seharusnya tidak null", result);
427-
assertTrue("Tipe geometry harus GeoJsonGeometryCollection",
446+
assertNotNull("GeometryCollection with normal nesting should not be null", result);
447+
assertTrue("Geometry type must be GeoJsonGeometryCollection",
428448
result instanceof GeoJsonGeometryCollection);
429449
}
430450
}

0 commit comments

Comments
 (0)