Skip to content

Commit 3a89558

Browse files
mp911dechristophstrobl
authored andcommitted
DATAMONGO-2086 - Fix Fluent API Kotlin extension generics to allow projections.
We now fixed Kotlin extension generics to properly use projections by ignoring the source type of the Fluent API object. Previously, the source and target type were linked which prevented the use of a different result type.
1 parent f79d98c commit 3a89558

File tree

4 files changed

+24
-28
lines changed

4 files changed

+24
-28
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@ import kotlin.reflect.KClass
2525
* @since 2.0
2626
*/
2727
fun <T : Any> ExecutableFindOperation.query(entityClass: KClass<T>): ExecutableFindOperation.ExecutableFind<T> =
28-
query(entityClass.java)
28+
query(entityClass.java)
2929

3030
/**
3131
* Extension for [ExecutableFindOperation.query] leveraging reified type parameters.
@@ -35,8 +35,7 @@ fun <T : Any> ExecutableFindOperation.query(entityClass: KClass<T>): ExecutableF
3535
* @since 2.0
3636
*/
3737
inline fun <reified T : Any> ExecutableFindOperation.query(): ExecutableFindOperation.ExecutableFind<T> =
38-
query(T::class.java)
39-
38+
query(T::class.java)
4039

4140
/**
4241
* Extension for [ExecutableFindOperation.FindWithProjection. as] providing a [KClass] based variant.
@@ -45,8 +44,8 @@ inline fun <reified T : Any> ExecutableFindOperation.query(): ExecutableFindOper
4544
* @author Mark Paluch
4645
* @since 2.0
4746
*/
48-
fun <T : Any> ExecutableFindOperation.FindWithProjection<T>.asType(resultType: KClass<T>): ExecutableFindOperation.FindWithQuery<T> =
49-
`as`(resultType.java)
47+
fun <T : Any> ExecutableFindOperation.FindWithProjection<*>.asType(resultType: KClass<T>): ExecutableFindOperation.FindWithQuery<T> =
48+
`as`(resultType.java)
5049

5150
/**
5251
* Extension for [ExecutableFindOperation.FindWithProjection. as] leveraging reified type parameters.
@@ -55,7 +54,5 @@ fun <T : Any> ExecutableFindOperation.FindWithProjection<T>.asType(resultType: K
5554
* @author Mark Paluch
5655
* @since 2.0
5756
*/
58-
inline fun <reified T : Any> ExecutableFindOperation.FindWithProjection<T>.asType(): ExecutableFindOperation.FindWithQuery<T> =
59-
`as`(T::class.java)
60-
61-
57+
inline fun <reified T : Any> ExecutableFindOperation.FindWithProjection<*>.asType(): ExecutableFindOperation.FindWithQuery<T> =
58+
`as`(T::class.java)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,7 +41,7 @@ inline fun <reified T : Any> ReactiveFindOperation.query(): ReactiveFindOperatio
4141
* @author Mark Paluch
4242
* @since 2.0
4343
*/
44-
fun <T : Any> ReactiveFindOperation.FindWithProjection<T>.asType(resultType: KClass<T>): ReactiveFindOperation.FindWithQuery<T> =
44+
fun <T : Any> ReactiveFindOperation.FindWithProjection<*>.asType(resultType: KClass<T>): ReactiveFindOperation.FindWithQuery<T> =
4545
`as`(resultType.java)
4646

4747
/**
@@ -50,7 +50,7 @@ fun <T : Any> ReactiveFindOperation.FindWithProjection<T>.asType(resultType: KCl
5050
* @author Mark Paluch
5151
* @since 2.0
5252
*/
53-
inline fun <reified T : Any> ReactiveFindOperation.FindWithProjection<T>.asType(): ReactiveFindOperation.FindWithQuery<T> =
53+
inline fun <reified T : Any> ReactiveFindOperation.FindWithProjection<*>.asType(): ReactiveFindOperation.FindWithQuery<T> =
5454
`as`(T::class.java)
5555

5656

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,18 +50,17 @@ class ExecutableFindOperationExtensionsTests {
5050
verify(operation).query(First::class.java)
5151
}
5252

53-
@Test // DATAMONGO-1689
53+
@Test // DATAMONGO-1689, DATAMONGO-2086
5454
fun `ExecutableFindOperation#FindOperationWithProjection#asType(KClass) extension should call its Java counterpart`() {
5555

56-
operationWithProjection.asType(First::class)
57-
verify(operationWithProjection).`as`(First::class.java)
56+
operationWithProjection.asType(User::class)
57+
verify(operationWithProjection).`as`(User::class.java)
5858
}
5959

60-
@Test // DATAMONGO-1689
60+
@Test // DATAMONGO-1689, DATAMONGO-2086
6161
fun `ExecutableFindOperation#FindOperationWithProjection#asType() with reified type parameter extension should call its Java counterpart`() {
6262

63-
operationWithProjection.asType()
64-
verify(operationWithProjection).`as`(First::class.java)
63+
operationWithProjection.asType<User>()
64+
verify(operationWithProjection).`as`(User::class.java)
6565
}
66-
6766
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,17 +49,17 @@ class ReactiveFindOperationExtensionsTests {
4949
verify(operation).query(First::class.java)
5050
}
5151

52-
@Test // DATAMONGO-1719
52+
@Test // DATAMONGO-1719, DATAMONGO-2086
5353
fun `ReactiveFind#FindOperatorWithProjection#asType(KClass) extension should call its Java counterpart`() {
5454

55-
operationWithProjection.asType(First::class)
56-
verify(operationWithProjection).`as`(First::class.java)
55+
operationWithProjection.asType(User::class)
56+
verify(operationWithProjection).`as`(User::class.java)
5757
}
5858

59-
@Test // DATAMONGO-1719
59+
@Test // DATAMONGO-1719, DATAMONGO-2086
6060
fun `ReactiveFind#FindOperatorWithProjection#asType() with reified type parameter extension should call its Java counterpart`() {
6161

62-
operationWithProjection.asType()
63-
verify(operationWithProjection).`as`(First::class.java)
62+
operationWithProjection.asType<User>()
63+
verify(operationWithProjection).`as`(User::class.java)
6464
}
6565
}

0 commit comments

Comments
 (0)