Skip to content

Commit 373f936

Browse files
committed
Relaxed requirement for GeoJsonMultiPoint to have 2 points.
Only 1 point is required per GeoJson RFC and Mongo works just fine with 1 point as well. Closes spring-projects#3776
1 parent 7538b1a commit 373f936

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonMultiPoint.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* {@link GeoJsonMultiPoint} is defined as list of {@link Point}s.
2929
*
3030
* @author Christoph Strobl
31+
* @author Ivan Volzhev
3132
* @since 1.7
3233
* @see <a href="https://geojson.org/geojson-spec.html#multipoint">https://geojson.org/geojson-spec.html#multipoint</a>
3334
*/
@@ -40,12 +41,12 @@ public class GeoJsonMultiPoint implements GeoJson<Iterable<Point>> {
4041
/**
4142
* Creates a new {@link GeoJsonMultiPoint} for the given {@link Point}s.
4243
*
43-
* @param points points must not be {@literal null} and have at least 2 entries.
44+
* @param points points must not be {@literal null} and have at least 1 entry.
4445
*/
4546
public GeoJsonMultiPoint(List<Point> points) {
4647

4748
Assert.notNull(points, "Points must not be null.");
48-
Assert.isTrue(points.size() >= 2, "Minimum of 2 Points required.");
49+
Assert.isTrue(points.size() >= 1, "Minimum of 1 Point required.");
4950

5051
this.points = new ArrayList<Point>(points);
5152
}
@@ -69,6 +70,19 @@ public GeoJsonMultiPoint(Point first, Point second, Point... others) {
6970
this.points.addAll(Arrays.asList(others));
7071
}
7172

73+
/**
74+
* Creates a new {@link GeoJsonMultiPoint} for the given {@link Point}.
75+
*
76+
* @param point must not be {@literal null}.
77+
*/
78+
public GeoJsonMultiPoint(Point point) {
79+
80+
Assert.notNull(point, "First point must not be null!");
81+
82+
this.points = new ArrayList<Point>();
83+
this.points.add(point);
84+
}
85+
7286
/*
7387
* (non-Javadoc)
7488
* @see org.springframework.data.mongodb.core.geo.GeoJson#getType()

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonTests.java

+16
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
/**
6464
* @author Christoph Strobl
6565
* @author Mark Paluch
66+
* @author Ivan Volzhev
6667
*/
6768
@ExtendWith({ MongoClientExtension.class, SpringExtension.class })
6869
@ContextConfiguration
@@ -329,6 +330,21 @@ public void shouldSaveAndRetrieveDocumentWithGeoJsonMultiPointTypeCorrectly() {
329330
assertThat(result.geoJsonMultiPoint).isEqualTo(obj.geoJsonMultiPoint);
330331
}
331332

333+
@Test // DATAMONGO-3776
334+
public void shouldSaveAndRetrieveDocumentWithGeoJsonMultiPointTypeWithOnePointCorrectly() {
335+
336+
DocumentWithPropertyUsingGeoJsonType obj = new DocumentWithPropertyUsingGeoJsonType();
337+
obj.id = "geoJsonMultiPoint";
338+
obj.geoJsonMultiPoint = new GeoJsonMultiPoint(new Point(0, 0));
339+
340+
template.save(obj);
341+
342+
DocumentWithPropertyUsingGeoJsonType result = template.findOne(query(where("id").is(obj.id)),
343+
DocumentWithPropertyUsingGeoJsonType.class);
344+
345+
assertThat(result.geoJsonMultiPoint).isEqualTo(obj.geoJsonMultiPoint);
346+
}
347+
332348
@Test // DATAMONGO-1137
333349
public void shouldSaveAndRetrieveDocumentWithGeoJsonMultiPolygonTypeCorrectly() {
334350

0 commit comments

Comments
 (0)