Apache Lucene open-source search software
GeoPointField now validates incoming lat/lon
Mike McCandless6a5e935aa6fa
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/document/GeoPointField.java b/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/document/GeoPointField.javaindex 688315ac134..00db23a8d0f 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/document/GeoPointField.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/geopoint/document/GeoPointField.java@@ -16,13 +16,14 @@*/package org.apache.lucene.spatial.geopoint.document;+import org.apache.lucene.analysis.Analyzer;+import org.apache.lucene.analysis.TokenStream;import org.apache.lucene.document.Field;import org.apache.lucene.document.FieldType;import org.apache.lucene.index.DocValuesType;import org.apache.lucene.index.IndexOptions;-import org.apache.lucene.analysis.Analyzer;-import org.apache.lucene.analysis.TokenStream;import org.apache.lucene.spatial.util.GeoEncodingUtils;+import org.apache.lucene.spatial.util.GeoUtils;/*** <p>@@ -162,6 +163,14 @@ public final class GeoPointField extends Field {public GeoPointField(String name, double lat, double lon, FieldType type) {super(name, type);+ if (GeoUtils.isValidLat(lat) == false) {+ throw new IllegalArgumentException("invalid lat=" + lat + " for field \"" + name + "\"");+ }++ if (GeoUtils.isValidLon(lon) == false) {+ throw new IllegalArgumentException("invalid lon=" + lon + " for field \"" + name + "\"");+ }+// field must be indexed// todo does it make sense here to provide the ability to store a GeoPointField but not index?if (type.indexOptions() == IndexOptions.NONE && type.stored() == false) {diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/geopoint/search/TestGeoPointField.java b/lucene/spatial/src/test/org/apache/lucene/spatial/geopoint/search/TestGeoPointField.javaindex 41136b95413..123769ee984 100644--- a/lucene/spatial/src/test/org/apache/lucene/spatial/geopoint/search/TestGeoPointField.java+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/geopoint/search/TestGeoPointField.java@@ -237,4 +237,19 @@ public class TestGeoPointField extends LuceneTestCase {TopDocs td = geoDistanceRangeQuery(0.0, 0.0, 10, 20000000, 20);assertEquals("GeoDistanceRangeQuery failed", 24, td.totalHits);}++ public void testInvalidLatLon() throws Exception {+ IllegalArgumentException e;+ e= expectThrows(IllegalArgumentException.class,+ () -> {+ new GeoPointField("field", 180.0, 0.0, Field.Store.NO);+ });+ assertEquals("invalid lat=180.0 for field \"field\"", e.getMessage());++ e = expectThrows(IllegalArgumentException.class,+ () -> {+ new GeoPointField("field", 0.0, 190.0, Field.Store.NO);+ });+ assertEquals("invalid lon=190.0 for field \"field\"", e.getMessage());+ }}