Skip to content

Commit

Permalink
feat: 1930 unknown geojson element (#1973)
Browse files Browse the repository at this point in the history
* add json map type adapter
Co-authored-by: David Gamez Diaz <1192523+davidgamez@users.noreply.github.com>
  • Loading branch information
qcdyx authored Feb 17, 2025
1 parent 22ee726 commit 2b3e0e5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.mobilitydata.gtfsvalidator.notice;

import static org.mobilitydata.gtfsvalidator.notice.SeverityLevel.INFO;

import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice;

/** Unknown elements in locations.geojson file. */
@GtfsValidationNotice(severity = INFO)
public class GeoJsonUnknownElementNotice extends ValidationNotice {
/** The name of the file where the unknown element was found. */
private final String filename;

/** The unknown element in the GeoJSON file. */
private final String unknownElement;

public GeoJsonUnknownElementNotice(String filename, String unknownElement) {
this.filename = filename;
this.unknownElement = unknownElement;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ public List<GtfsGeoJsonFeature> extractFeaturesFromStream(
throw new JsonParseException("Expected a JSON object at the root");
}
JsonObject jsonObject = root.getAsJsonObject();
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
String key = entry.getKey();
if (!"type".equals(key) && !"features".equals(key)) {
noticeContainer.addValidationNotice(
new GeoJsonUnknownElementNotice(GtfsGeoJsonFeature.FILENAME, key));
}
}
if (!jsonObject.has("type")) {
noticeContainer.addValidationNotice(new MissingRequiredElementNotice(null, "type", null));
throw new UnparsableGeoJsonFeatureException("Missing required field 'type'");
Expand Down Expand Up @@ -119,6 +126,19 @@ public GtfsGeoJsonFeature extractFeature(
String featureId = null;
if (feature.isJsonObject()) {
JsonObject featureObject = feature.getAsJsonObject();

// Check for unknown elements in the featureObject
for (Map.Entry<String, JsonElement> entry : featureObject.entrySet()) {
String key = entry.getKey();
if (!GtfsGeoJsonFeature.FEATURE_ID_FIELD_NAME.equals(key)
&& !GtfsGeoJsonFeature.FEATURE_TYPE_FIELD_NAME.equals(key)
&& !GtfsGeoJsonFeature.FEATURE_PROPERTIES_FIELD_NAME.equals(key)
&& !GtfsGeoJsonFeature.GEOMETRY_FIELD_NAME.equals(key)) {
noticeContainer.addValidationNotice(
new GeoJsonUnknownElementNotice(GtfsGeoJsonFeature.FILENAME, key));
}
}

// Handle feature id
if (!featureObject.has(GtfsGeoJsonFeature.FEATURE_ID_FIELD_NAME)) {
missingRequiredFields.add(
Expand Down Expand Up @@ -168,6 +188,15 @@ public GtfsGeoJsonFeature extractFeature(
+ GtfsGeoJsonFeature.GEOMETRY_FIELD_NAME);
} else {
JsonObject geometry = featureObject.getAsJsonObject(GtfsGeoJsonFeature.GEOMETRY_FIELD_NAME);
// Check for unknown elements in the geometry object
for (Map.Entry<String, JsonElement> entry : geometry.entrySet()) {
String key = entry.getKey();
if (!GtfsGeoJsonFeature.GEOMETRY_TYPE_FIELD_NAME.equals(key)
&& !GtfsGeoJsonFeature.GEOMETRY_COORDINATES_FIELD_NAME.equals(key)) {
noticeContainer.addValidationNotice(
new GeoJsonUnknownElementNotice(GtfsGeoJsonFeature.FILENAME, key));
}
}
// Handle geometry type and coordinates
if (!geometry.has(GtfsGeoJsonFeature.GEOMETRY_TYPE_FIELD_NAME)) {
missingRequiredFields.add(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.mobilitydata.gtfsvalidator.util.geojson;

public class UnknownJsonKeyException extends RuntimeException {
private final String message;
private String key;

public UnknownJsonKeyException(String key, String message) {
this.key = key;
this.message = message;
}

public String getKey() {
return key;
}

public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ public void testNoticeClassFieldNames() {
"tripIdFieldName",
"validator",
"value",
"duplicatedElement");
"duplicatedElement",
"unknownElement");
}

private static List<String> discoverValidationNoticeFieldNames() {
Expand Down

0 comments on commit 2b3e0e5

Please sign in to comment.