Skip to content

Commit 2b3e0e5

Browse files
qcdyxdavidgamez
andauthored
feat: 1930 unknown geojson element (#1973)
* add json map type adapter Co-authored-by: David Gamez Diaz <[email protected]>
1 parent 22ee726 commit 2b3e0e5

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.mobilitydata.gtfsvalidator.notice;
2+
3+
import static org.mobilitydata.gtfsvalidator.notice.SeverityLevel.INFO;
4+
5+
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice;
6+
7+
/** Unknown elements in locations.geojson file. */
8+
@GtfsValidationNotice(severity = INFO)
9+
public class GeoJsonUnknownElementNotice extends ValidationNotice {
10+
/** The name of the file where the unknown element was found. */
11+
private final String filename;
12+
13+
/** The unknown element in the GeoJSON file. */
14+
private final String unknownElement;
15+
16+
public GeoJsonUnknownElementNotice(String filename, String unknownElement) {
17+
this.filename = filename;
18+
this.unknownElement = unknownElement;
19+
}
20+
}

main/src/main/java/org/mobilitydata/gtfsvalidator/table/GeoJsonFileLoader.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ public List<GtfsGeoJsonFeature> extractFeaturesFromStream(
8181
throw new JsonParseException("Expected a JSON object at the root");
8282
}
8383
JsonObject jsonObject = root.getAsJsonObject();
84+
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
85+
String key = entry.getKey();
86+
if (!"type".equals(key) && !"features".equals(key)) {
87+
noticeContainer.addValidationNotice(
88+
new GeoJsonUnknownElementNotice(GtfsGeoJsonFeature.FILENAME, key));
89+
}
90+
}
8491
if (!jsonObject.has("type")) {
8592
noticeContainer.addValidationNotice(new MissingRequiredElementNotice(null, "type", null));
8693
throw new UnparsableGeoJsonFeatureException("Missing required field 'type'");
@@ -119,6 +126,19 @@ public GtfsGeoJsonFeature extractFeature(
119126
String featureId = null;
120127
if (feature.isJsonObject()) {
121128
JsonObject featureObject = feature.getAsJsonObject();
129+
130+
// Check for unknown elements in the featureObject
131+
for (Map.Entry<String, JsonElement> entry : featureObject.entrySet()) {
132+
String key = entry.getKey();
133+
if (!GtfsGeoJsonFeature.FEATURE_ID_FIELD_NAME.equals(key)
134+
&& !GtfsGeoJsonFeature.FEATURE_TYPE_FIELD_NAME.equals(key)
135+
&& !GtfsGeoJsonFeature.FEATURE_PROPERTIES_FIELD_NAME.equals(key)
136+
&& !GtfsGeoJsonFeature.GEOMETRY_FIELD_NAME.equals(key)) {
137+
noticeContainer.addValidationNotice(
138+
new GeoJsonUnknownElementNotice(GtfsGeoJsonFeature.FILENAME, key));
139+
}
140+
}
141+
122142
// Handle feature id
123143
if (!featureObject.has(GtfsGeoJsonFeature.FEATURE_ID_FIELD_NAME)) {
124144
missingRequiredFields.add(
@@ -168,6 +188,15 @@ public GtfsGeoJsonFeature extractFeature(
168188
+ GtfsGeoJsonFeature.GEOMETRY_FIELD_NAME);
169189
} else {
170190
JsonObject geometry = featureObject.getAsJsonObject(GtfsGeoJsonFeature.GEOMETRY_FIELD_NAME);
191+
// Check for unknown elements in the geometry object
192+
for (Map.Entry<String, JsonElement> entry : geometry.entrySet()) {
193+
String key = entry.getKey();
194+
if (!GtfsGeoJsonFeature.GEOMETRY_TYPE_FIELD_NAME.equals(key)
195+
&& !GtfsGeoJsonFeature.GEOMETRY_COORDINATES_FIELD_NAME.equals(key)) {
196+
noticeContainer.addValidationNotice(
197+
new GeoJsonUnknownElementNotice(GtfsGeoJsonFeature.FILENAME, key));
198+
}
199+
}
171200
// Handle geometry type and coordinates
172201
if (!geometry.has(GtfsGeoJsonFeature.GEOMETRY_TYPE_FIELD_NAME)) {
173202
missingRequiredFields.add(
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.mobilitydata.gtfsvalidator.util.geojson;
2+
3+
public class UnknownJsonKeyException extends RuntimeException {
4+
private final String message;
5+
private String key;
6+
7+
public UnknownJsonKeyException(String key, String message) {
8+
this.key = key;
9+
this.message = message;
10+
}
11+
12+
public String getKey() {
13+
return key;
14+
}
15+
16+
public String getMessage() {
17+
return message;
18+
}
19+
}

main/src/test/java/org/mobilitydata/gtfsvalidator/validator/NoticeFieldsTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ public void testNoticeClassFieldNames() {
222222
"tripIdFieldName",
223223
"validator",
224224
"value",
225-
"duplicatedElement");
225+
"duplicatedElement",
226+
"unknownElement");
226227
}
227228

228229
private static List<String> discoverValidationNoticeFieldNames() {

0 commit comments

Comments
 (0)