Skip to content

Commit 1d547ec

Browse files
committed
Refine Kotlin extensions for MongoOperations.aggregate and aggregateStream
We now provide extensions accepting the input- and output type as reified generic types. See #3508.
1 parent ce3066d commit 1d547ec

File tree

2 files changed

+97
-13
lines changed

2 files changed

+97
-13
lines changed

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

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,37 +220,73 @@ inline fun <reified T : Any> MongoOperations.group(criteria: Criteria, inputColl
220220
* @author Sebastien Deleuze
221221
* @since 2.0
222222
*/
223-
@Deprecated("Since 2.2, use the reified variant", replaceWith = ReplaceWith("aggregate<T>(aggregation)"))
224-
inline fun <reified O : Any> MongoOperations.aggregate(aggregation: Aggregation, inputType: KClass<*>): AggregationResults<O> =
225-
aggregate(aggregation, inputType.java, O::class.java)
223+
@Deprecated(
224+
"Since 2.2, use the reified variant",
225+
replaceWith = ReplaceWith("aggregate<I, O>(aggregation)")
226+
)
227+
inline fun <reified O : Any> MongoOperations.aggregate(
228+
aggregation: Aggregation,
229+
inputType: KClass<*>
230+
): AggregationResults<O> =
231+
aggregate(aggregation, inputType.java, O::class.java)
232+
233+
/**
234+
* Extension for [MongoOperations.aggregate] leveraging reified type parameters.
235+
*
236+
* @author Mark Paluch
237+
* @since 3.2
238+
*/
239+
inline fun <reified I : Any, reified O : Any> MongoOperations.aggregate(aggregation: Aggregation): AggregationResults<O> =
240+
aggregate(aggregation, I::class.java, O::class.java)
226241

227242
/**
228243
* Extension for [MongoOperations.aggregate] leveraging reified type parameters.
229244
*
230245
* @author Sebastien Deleuze
231246
* @since 2.0
232247
*/
233-
inline fun <reified O : Any> MongoOperations.aggregate(aggregation: Aggregation, collectionName: String): AggregationResults<O> =
234-
aggregate(aggregation, collectionName, O::class.java)
248+
inline fun <reified O : Any> MongoOperations.aggregate(
249+
aggregation: Aggregation,
250+
collectionName: String
251+
): AggregationResults<O> =
252+
aggregate(aggregation, collectionName, O::class.java)
235253

236254
/**
237255
* Extension for [MongoOperations.aggregateStream] leveraging reified type parameters.
238256
*
239257
* @author Sebastien Deleuze
240258
* @since 2.0
241259
*/
242-
@Deprecated("Since 2.2, use the reified variant", replaceWith = ReplaceWith("aggregateStream<T>(aggregation)"))
243-
inline fun <reified O : Any> MongoOperations.aggregateStream(aggregation: Aggregation, inputType: KClass<*>): CloseableIterator<O> =
244-
aggregateStream(aggregation, inputType.java, O::class.java)
260+
@Deprecated(
261+
"Since 2.2, use the reified variant",
262+
replaceWith = ReplaceWith("aggregateStream<I, O>(aggregation)")
263+
)
264+
inline fun <reified O : Any> MongoOperations.aggregateStream(
265+
aggregation: Aggregation,
266+
inputType: KClass<*>
267+
): CloseableIterator<O> =
268+
aggregateStream(aggregation, inputType.java, O::class.java)
269+
270+
/**
271+
* Extension for [MongoOperations.aggregateStream] leveraging reified type parameters.
272+
*
273+
* @author Mark Paluch
274+
* @since 3.2
275+
*/
276+
inline fun <reified I : Any, reified O : Any> MongoOperations.aggregateStream(aggregation: Aggregation): CloseableIterator<O> =
277+
aggregateStream(aggregation, I::class.java, O::class.java)
245278

246279
/**
247280
* Extension for [MongoOperations.aggregateStream] leveraging reified type parameters.
248281
*
249282
* @author Sebastien Deleuze
250283
* @since 2.0
251284
*/
252-
inline fun <reified O : Any> MongoOperations.aggregateStream(aggregation: Aggregation, collectionName: String): CloseableIterator<O> =
253-
aggregateStream(aggregation, collectionName, O::class.java)
285+
inline fun <reified O : Any> MongoOperations.aggregateStream(
286+
aggregation: Aggregation,
287+
collectionName: String
288+
): CloseableIterator<O> =
289+
aggregateStream(aggregation, collectionName, O::class.java)
254290

255291
/**
256292
* Extension for [MongoOperations.mapReduce] leveraging reified type parameters.

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

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,28 @@ class MongoOperationsExtensionsTests {
241241
val aggregation = mockk<Aggregation>()
242242

243243
operations.aggregate<First>(aggregation, Second::class)
244-
verify { operations.aggregate(aggregation, Second::class.java, First::class.java) }
244+
verify {
245+
operations.aggregate(
246+
aggregation,
247+
Second::class.java,
248+
First::class.java
249+
)
250+
}
251+
}
252+
253+
@Test // #3508
254+
fun `aggregate(Aggregation) with reified type parameter extension should call its Java counterpart`() {
255+
256+
val aggregation = mockk<Aggregation>()
257+
258+
operations.aggregate<Second, First>(aggregation)
259+
verify {
260+
operations.aggregate(
261+
aggregation,
262+
Second::class.java,
263+
First::class.java
264+
)
265+
}
245266
}
246267

247268
@Test // DATAMONGO-1689
@@ -261,7 +282,28 @@ class MongoOperationsExtensionsTests {
261282
val aggregation = mockk<Aggregation>()
262283

263284
operations.aggregateStream<First>(aggregation, Second::class)
264-
verify { operations.aggregateStream(aggregation, Second::class.java, First::class.java) }
285+
verify {
286+
operations.aggregateStream(
287+
aggregation,
288+
Second::class.java,
289+
First::class.java
290+
)
291+
}
292+
}
293+
294+
@Test // #3508
295+
fun `aggregateStream(Aggregation) with reified type parameter extension should call its Java counterpart`() {
296+
297+
val aggregation = mockk<Aggregation>()
298+
299+
operations.aggregateStream<Second, First>(aggregation)
300+
verify {
301+
operations.aggregateStream(
302+
aggregation,
303+
Second::class.java,
304+
First::class.java
305+
)
306+
}
265307
}
266308

267309
@Test // DATAMONGO-1689
@@ -271,7 +313,13 @@ class MongoOperationsExtensionsTests {
271313
val collectionName = "foo"
272314

273315
operations.aggregateStream<First>(aggregation, collectionName)
274-
verify { operations.aggregateStream(aggregation, collectionName, First::class.java) }
316+
verify {
317+
operations.aggregateStream(
318+
aggregation,
319+
collectionName,
320+
First::class.java
321+
)
322+
}
275323
}
276324

277325
@Test // DATAMONGO-1689

0 commit comments

Comments
 (0)