Skip to content

Commit c394986

Browse files
committed
DATAMONGO-2138 - Restrict GeoJson properties
1 parent 01c7edc commit c394986

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/query/TypedCriteriaBuilder.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class TypedCriteriaBuilder {
237237
* $centerSphere](https://docs.mongodb.com/manual/reference/operator/query/centerSphere/)
238238
* @see Criteria.withinSphere
239239
*/
240-
infix fun KProperty<*>.withinSphere(circle: Circle) = addOperation { withinSphere(circle) }
240+
infix fun KProperty<GeoJson<*>>.withinSphere(circle: Circle) = addOperation { withinSphere(circle) }
241241

242242
/**
243243
* Creates a geospatial criterion using a $geoWithin operation.
@@ -246,15 +246,15 @@ class TypedCriteriaBuilder {
246246
* $geoWithin](https://docs.mongodb.com/manual/reference/operator/query/geoWithin/)
247247
* @see Criteria.within
248248
*/
249-
infix fun KProperty<*>.within(shape: Shape) = addOperation { within(shape) }
249+
infix fun KProperty<GeoJson<*>>.within(shape: Shape) = addOperation { within(shape) }
250250

251251
/**
252252
* Creates a geospatial criterion using a $near operation.
253253
*
254254
* See [MongoDB Query operator: $near](https://docs.mongodb.com/manual/reference/operator/query/near/)
255255
* @see Criteria.near
256256
*/
257-
infix fun KProperty<*>.near(point: Point) = addOperation { near(point) }
257+
infix fun KProperty<GeoJson<*>>.near(point: Point) = addOperation { near(point) }
258258

259259
/**
260260
* Creates a geospatial criterion using a $nearSphere operation. This is only available for Mongo 1.7 and
@@ -264,14 +264,14 @@ class TypedCriteriaBuilder {
264264
* $nearSphere](https://docs.mongodb.com/manual/reference/operator/query/nearSphere/)
265265
* @see Criteria.nearSphere
266266
*/
267-
infix fun KProperty<*>.nearSphere(point: Point) = addOperation { nearSphere(point) }
267+
infix fun KProperty<GeoJson<*>>.nearSphere(point: Point) = addOperation { nearSphere(point) }
268268

269269
/**
270270
* Creates criterion using `$geoIntersects` operator which matches intersections of the given `geoJson`
271271
* structure and the documents one. Requires MongoDB 2.4 or better.
272272
* @see Criteria.intersects
273273
*/
274-
infix fun KProperty<*>.intersects(geoJson: GeoJson<*>) = addOperation { intersects(geoJson) }
274+
infix fun KProperty<GeoJson<*>>.intersects(geoJson: GeoJson<*>) = addOperation { intersects(geoJson) }
275275

276276
/**
277277
* Creates a geo-spatial criterion using a $maxDistance operation, for use with $near
@@ -280,14 +280,14 @@ class TypedCriteriaBuilder {
280280
* $maxDistance](https://docs.mongodb.com/manual/reference/operator/query/maxDistance/)
281281
* @see Criteria.maxDistance
282282
*/
283-
infix fun KProperty<*>.maxDistance(d: Double) = addOperation { maxDistance(d) }
283+
infix fun KProperty<GeoJson<*>>.maxDistance(d: Double) = addOperation { maxDistance(d) }
284284

285285
/**
286286
* Creates a geospatial criterion using a $minDistance operation, for use with $near or
287287
* $nearSphere.
288288
* @see Criteria.minDistance
289289
*/
290-
infix fun KProperty<*>.minDistance(d: Double) = addOperation { minDistance(d) }
290+
infix fun KProperty<GeoJson<*>>.minDistance(d: Double) = addOperation { minDistance(d) }
291291

292292
/**
293293
* Creates a criterion using the $elemMatch operator

spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/query/TypedCriteriaExtensionsTests.kt

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@ import java.util.regex.Pattern
3232
*/
3333
class TypedCriteriaExtensionsTests {
3434

35-
data class Book(
36-
val title: String = "Moby-Dick",
37-
val price: Int = 123,
38-
val available: Boolean = true,
39-
val categories: List<String> = emptyList(),
40-
val author: Author = Author("Herman Melville")
41-
)
42-
43-
data class Author(
44-
val name: String
45-
)
46-
4735
@Test
4836
fun `typedQuery should equal Query`() {
4937

@@ -272,60 +260,60 @@ class TypedCriteriaExtensionsTests {
272260
fun `withinSphere() typed criteria should equal classic criteria`() {
273261

274262
val value = Circle(Point(1.0, 2.0), 3.0)
275-
val typed = typedCriteria { Book::title withinSphere value }
276-
val classic = Criteria("title").withinSphere(value)
263+
val typed = typedCriteria { Building::location withinSphere value }
264+
val classic = Criteria("location").withinSphere(value)
277265
assertEqualCriteria(typed, classic)
278266
}
279267

280268
@Test
281269
fun `within() typed criteria should equal classic criteria`() {
282270

283271
val value = Circle(Point(1.0, 2.0), 3.0)
284-
val typed = typedCriteria { Book::title within value }
285-
val classic = Criteria("title").within(value)
272+
val typed = typedCriteria { Building::location within value }
273+
val classic = Criteria("location").within(value)
286274
assertEqualCriteria(typed, classic)
287275
}
288276

289277
@Test
290278
fun `near() typed criteria should equal classic criteria`() {
291279

292280
val value = Point(1.0, 2.0)
293-
val typed = typedCriteria { Book::title near value }
294-
val classic = Criteria("title").near(value)
281+
val typed = typedCriteria { Building::location near value }
282+
val classic = Criteria("location").near(value)
295283
assertEqualCriteria(typed, classic)
296284
}
297285

298286
@Test
299287
fun `nearSphere() typed criteria should equal classic criteria`() {
300288

301289
val value = Point(1.0, 2.0)
302-
val typed = typedCriteria { Book::title nearSphere value }
303-
val classic = Criteria("title").nearSphere(value)
290+
val typed = typedCriteria { Building::location nearSphere value }
291+
val classic = Criteria("location").nearSphere(value)
304292
assertEqualCriteria(typed, classic)
305293
}
306294

307295
@Test
308296
fun `intersects() typed criteria should equal classic criteria`() {
309297

310298
val value = GeoJsonPoint(1.0, 2.0)
311-
val typed = typedCriteria { Book::title intersects value }
312-
val classic = Criteria("title").intersects(value)
299+
val typed = typedCriteria { Building::location intersects value }
300+
val classic = Criteria("location").intersects(value)
313301
assertEqualCriteria(typed, classic)
314302
}
315303

316304
@Test
317305
fun `maxDistance() typed criteria should equal classic criteria`() {
318306

319-
val typed = typedCriteria { Book::title maxDistance 3.0 }
320-
val classic = Criteria("title").maxDistance(3.0)
307+
val typed = typedCriteria { Building::location maxDistance 3.0 }
308+
val classic = Criteria("location").maxDistance(3.0)
321309
assertEqualCriteria(typed, classic)
322310
}
323311

324312
@Test
325313
fun `minDistance() typed criteria should equal classic criteria`() {
326314

327-
val typed = typedCriteria { Book::title minDistance 3.0 }
328-
val classic = Criteria("title").minDistance(3.0)
315+
val typed = typedCriteria { Building::location minDistance 3.0 }
316+
val classic = Criteria("location").minDistance(3.0)
329317
assertEqualCriteria(typed, classic)
330318
}
331319

@@ -452,4 +440,20 @@ class TypedCriteriaExtensionsTests {
452440
assertThat(typed.criteriaObject).isEqualTo(classic.criteriaObject)
453441
assertThat(typed).isEqualTo(classic)
454442
}
443+
444+
data class Book(
445+
val title: String = "Moby-Dick",
446+
val price: Int = 123,
447+
val available: Boolean = true,
448+
val categories: List<String> = emptyList(),
449+
val author: Author = Author()
450+
)
451+
452+
data class Author(
453+
val name: String = "Herman Melville"
454+
)
455+
456+
data class Building(
457+
val location: GeoJsonPoint = GeoJsonPoint(5.481573, 51.451726)
458+
)
455459
}

0 commit comments

Comments
 (0)