Skip to content

Commit 2191ab3

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. Original Pull Request: #609
1 parent a791429 commit 2191ab3

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

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

Lines changed: 3 additions & 4 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.
@@ -37,15 +37,14 @@ fun <T : Any> ExecutableFindOperation.query(entityClass: KClass<T>): ExecutableF
3737
inline fun <reified T : Any> ExecutableFindOperation.query(): ExecutableFindOperation.ExecutableFind<T> =
3838
query(T::class.java)
3939

40-
4140
/**
4241
* Extension for [ExecutableFindOperation.FindWithProjection. as] providing a [KClass] based variant.
4342
*
4443
* @author Sebastien Deleuze
4544
* @author Mark Paluch
4645
* @since 2.0
4746
*/
48-
fun <T : Any> ExecutableFindOperation.FindWithProjection<T>.asType(resultType: KClass<T>): ExecutableFindOperation.FindWithQuery<T> =
47+
fun <T : Any> ExecutableFindOperation.FindWithProjection<*>.asType(resultType: KClass<T>): ExecutableFindOperation.FindWithQuery<T> =
4948
`as`(resultType.java)
5049

5150
/**
@@ -55,7 +54,7 @@ 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> =
57+
inline fun <reified T : Any> ExecutableFindOperation.FindWithProjection<*>.asType(): ExecutableFindOperation.FindWithQuery<T> =
5958
`as`(T::class.java)
6059

6160
/**

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: 10 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.
@@ -53,24 +53,24 @@ class ExecutableFindOperationExtensionsTests {
5353
verify(operation).query(First::class.java)
5454
}
5555

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

59-
operationWithProjection.asType(First::class)
60-
verify(operationWithProjection).`as`(First::class.java)
59+
operationWithProjection.asType(User::class)
60+
verify(operationWithProjection).`as`(User::class.java)
6161
}
6262

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

66-
operationWithProjection.asType()
67-
verify(operationWithProjection).`as`(First::class.java)
66+
operationWithProjection.asType<User>()
67+
verify(operationWithProjection).`as`(User::class.java)
6868
}
6969

70-
@Test // DATAMONGO-1761
70+
@Test // DATAMONGO-1761, DATAMONGO-2086
7171
fun `ExecutableFindOperation#DistinctWithProjection#asType(KClass) extension should call its Java counterpart`() {
7272

73-
distinctWithProjection.asType(First::class)
74-
verify(distinctWithProjection).`as`(First::class.java)
73+
distinctWithProjection.asType(User::class)
74+
verify(distinctWithProjection).`as`(User::class.java)
7575
}
7676
}

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

Lines changed: 10 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.
@@ -52,24 +52,24 @@ class ReactiveFindOperationExtensionsTests {
5252
verify(operation).query(First::class.java)
5353
}
5454

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

58-
operationWithProjection.asType(First::class)
59-
verify(operationWithProjection).`as`(First::class.java)
58+
operationWithProjection.asType(User::class)
59+
verify(operationWithProjection).`as`(User::class.java)
6060
}
6161

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

65-
operationWithProjection.asType()
66-
verify(operationWithProjection).`as`(First::class.java)
65+
operationWithProjection.asType<User>()
66+
verify(operationWithProjection).`as`(User::class.java)
6767
}
6868

69-
@Test // DATAMONGO-1761
69+
@Test // DATAMONGO-1761, DATAMONGO-2086
7070
fun `ReactiveFind#DistinctWithProjection#asType(KClass) extension should call its Java counterpart`() {
7171

72-
distinctWithProjection.asType(First::class)
73-
verify(distinctWithProjection).`as`(First::class.java)
72+
distinctWithProjection.asType(User::class)
73+
verify(distinctWithProjection).`as`(User::class.java)
7474
}
7575
}

0 commit comments

Comments
 (0)