Skip to content

Commit 4ad4cfe

Browse files
authored
Add fallback for unknown types in KSP (#1263)
This PR is attempting to solve #1255 Currently KSP module will throw exception when it encounters an arbitrary type without hibernate annotations, because it believes this is invalid and will alert the user to make the correct configuration. But there seems to exist cases where arbitrary types are allowed (possibly through plugins?). This PR adds a fallback where it uses ComparablePath if the type implements Compararable interface and otherwise it uses SimplePath.
2 parents 73164a5 + 48037b7 commit 4ad4cfe

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

querydsl-examples/querydsl-example-ksp-codegen/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ dependencies {
2222
implementation("io.github.openfeign.querydsl:querydsl-core:${querydslVersion}")
2323
implementation("org.hibernate.orm:hibernate-core:${hibernateVersion}")
2424
ksp("io.github.openfeign.querydsl:querydsl-ksp-codegen:${querydslVersion}")
25+
implementation("org.locationtech.jts:jts-core:1.19.0")
2526
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.19.0")
2627
testImplementation("io.github.openfeign.querydsl:querydsl-jpa:${querydslVersion}")
2728
testImplementation("org.assertj:assertj-core:${assertjVersion}")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.querydsl.example.ksp
2+
3+
import jakarta.persistence.Column
4+
import jakarta.persistence.Entity
5+
import jakarta.persistence.Id
6+
import org.locationtech.jts.geom.Point
7+
8+
@Entity
9+
class Geolocation(
10+
@Id
11+
val id: Int,
12+
@Column(columnDefinition = "geography(Point, 4326)")
13+
var location: Point? = null,
14+
)

querydsl-examples/querydsl-example-ksp-codegen/src/test/kotlin/Tests.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.querydsl.example.ksp.QBear
77
import com.querydsl.example.ksp.QBearSimplifiedProjection
88
import com.querydsl.example.ksp.QCat
99
import com.querydsl.example.ksp.QDog
10+
import com.querydsl.example.ksp.QGeolocation
1011
import com.querydsl.example.ksp.QPerson
1112
import com.querydsl.example.ksp.QPersonClassDTO
1213
import com.querydsl.example.ksp.QPersonClassConstructorDTO
@@ -248,6 +249,16 @@ class Tests {
248249
}
249250
}
250251

252+
@Test
253+
fun `is detecting comparable interface on unknown type`() {
254+
val locationTypeName = QGeolocation::class
255+
.members
256+
.first { it.name == "location" }
257+
.returnType
258+
.toString()
259+
assertThat(locationTypeName).isEqualTo("com.querydsl.core.types.dsl.ComparablePath<org.locationtech.jts.geom.Point>")
260+
}
261+
251262
private fun initialize(): EntityManagerFactory {
252263
val configuration = Configuration()
253264
.setProperty(AvailableSettings.JAKARTA_JDBC_DRIVER, org.h2.Driver::class.qualifiedName!!)

querydsl-tooling/querydsl-ksp-codegen/src/main/kotlin/com/querydsl/ksp/codegen/TypeExtractor.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class TypeExtractor(
2424
?: simpleType(type)
2525
?: referenceType(type)
2626
?: collectionType(type)
27-
?: throwError("Type was not recognised, This may be an entity that has not been annotated with @Entity, or maybe you are using javax instead of jakarta.")
27+
?: fallbackType(type)
2828
}
2929
}
3030

@@ -45,6 +45,27 @@ class TypeExtractor(
4545
}
4646
}
4747

48+
private fun fallbackType(type: KSType): QPropertyType.Simple {
49+
val declaration = type.declaration
50+
val isComparable = if (declaration is KSClassDeclaration) {
51+
val comparableNames = listOfNotNull(
52+
Comparable::class.java.canonicalName,
53+
java.lang.Comparable::class.qualifiedName
54+
)
55+
declaration.getAllSuperTypes().any {
56+
comparableNames.contains(it.toClassName().canonicalName)
57+
}
58+
} else {
59+
false
60+
}
61+
val className = type.toClassNameSimple()
62+
if (isComparable) {
63+
return QPropertyType.Simple(SimpleType.Comparable(className))
64+
} else {
65+
return QPropertyType.Simple(SimpleType.Simple(className))
66+
}
67+
}
68+
4869
private fun simpleType(type: KSType): QPropertyType.Simple? {
4970
val className = type.toClassNameSimple()
5071
val simpleType = SimpleType.Mapper.get(className)

0 commit comments

Comments
 (0)