Skip to content

Commit

Permalink
feat: 1927 flex geo json parsing failed (#1947)
Browse files Browse the repository at this point in the history
  • Loading branch information
qcdyx authored Jan 28, 2025
1 parent 2fa6883 commit 54a572a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ public class MalformedJsonNotice extends ValidationNotice {
/** The name of the faulty file. */
private final String filename;

public MalformedJsonNotice(String filename) {
/** The detailed message describing the error. */
private final String message;

public MalformedJsonNotice(String filename, String message) {
this.filename = filename;
this.message = message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public class UnsupportedGeoJsonTypeNotice extends ValidationNotice {
/** The value of the unsupported GeoJSON type. */
private final String geoJsonType;

public UnsupportedGeoJsonTypeNotice(String geoJsonType) {
/** The detailed message describing the error. */
private final String message;

public UnsupportedGeoJsonTypeNotice(String geoJsonType, String message) {
this.geoJsonType = geoJsonType;
this.message = message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public GtfsEntityContainer load(
List<GtfsGeoJsonFeature> entities = extractFeaturesFromStream(inputStream, noticeContainer);
return geoJsonFileDescriptor.createContainerForEntities(entities, noticeContainer);
} catch (JsonParseException jpex) {
noticeContainer.addValidationNotice(new MalformedJsonNotice(GtfsGeoJsonFeature.FILENAME));
noticeContainer.addValidationNotice(
new MalformedJsonNotice(GtfsGeoJsonFeature.FILENAME, jpex.getMessage()));
logger.atSevere().withCause(jpex).log("Malformed JSON in locations.geojson");
return fileDescriptor.createContainerForInvalidStatus(TableStatus.UNPARSABLE_ROWS);
} catch (IOException ioex) {
Expand All @@ -55,6 +56,15 @@ public GtfsEntityContainer load(
}
}

/**
* Extracts features from the provided GeoJSON input stream.
*
* @param inputStream the input stream containing GeoJSON data
* @param noticeContainer the container to collect validation notices
* @return a list of parsed GeoJSON features
* @throws IOException if an I/O error occurs while reading the input stream
* @throws UnparsableGeoJsonFeatureException if any GeoJSON feature is unparsable
*/
public List<GtfsGeoJsonFeature> extractFeaturesFromStream(
InputStream inputStream, NoticeContainer noticeContainer)
throws IOException, UnparsableGeoJsonFeatureException {
Expand All @@ -67,7 +77,11 @@ public List<GtfsGeoJsonFeature> extractFeaturesFromStream(
throw new UnparsableGeoJsonFeatureException("Missing required field 'type'");
} else if (!jsonObject.get("type").getAsString().equals("FeatureCollection")) {
noticeContainer.addValidationNotice(
new UnsupportedGeoJsonTypeNotice(jsonObject.get("type").getAsString()));
new UnsupportedGeoJsonTypeNotice(
jsonObject.get("type").getAsString(),
"Unsupported GeoJSON type: "
+ jsonObject.get("type").getAsString()
+ ". Use 'FeatureCollection' instead."));
throw new UnparsableGeoJsonFeatureException("Unsupported GeoJSON type");
}
JsonArray featuresArray = jsonObject.getAsJsonArray("features");
Expand Down

0 comments on commit 54a572a

Please sign in to comment.