Skip to content

Commit

Permalink
Fix NPE in ksp processing (#862) (#865)
Browse files Browse the repository at this point in the history
### Overview
The `containingFile` of `KSClassDeclaration` is nullable. Update the
processing to handle a null containingFile.

I was able to pull, modify, and build the project artifacts. This change
fixes the NullPointerException (my local project build now fully
completes) and I am able to see the generated Q entities from the
external library. Both my Child class entity and Parent Entity in the
external library are generated.

### Testing
Run build to completion (`mvn -Pquickbuild,all clean install`), pulled
in artifacts to my project, built & ran without issues.

### Note
Please let me know if `master` is the correct branch to get a quick fix
out. Thanks!
  • Loading branch information
IceBlizz6 authored Feb 7, 2025
2 parents 8c2ba56 + 14cfc57 commit 5af90e0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class QueryDslProcessor(
.writeTo(
codeGenerator = codeGenerator,
aggregating = false,
originatingKSFiles = listOf(model.originatingFile)
originatingKSFiles = listOfNotNull(model.originatingFile)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class QueryModel(
val typeParameterCount: Int,
val className: ClassName,
val type: QueryModelType,
val originatingFile: KSFile
val originatingFile: KSFile?
) {
var superclass: QueryModel? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class QueryModelExtractor(
typeParameterCount = classDeclaration.typeParameters.size,
className = queryClassName(classDeclaration, settings),
type = type,
originatingFile = classDeclaration.containingFile!!
originatingFile = classDeclaration.containingFile
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,30 @@ class RenderTest {
code.assertContainLines("val _super: QAnimal by lazy { QAnimal(this) }")
}

@Test
fun superclassNullContainingFile() {
val model = QueryModel(
originalClassName = ClassName("", "Cat"),
typeParameterCount = 0,
className = ClassName("", "QCat"),
type = QueryModelType.ENTITY,
mockk()
)
val superClass = QueryModel(
originalClassName = ClassName("", "Animal"),
typeParameterCount = 0,
className = ClassName("", "QAnimal"),
type = QueryModelType.SUPERCLASS,
null
)
model.superclass = superClass
val typeSpec = QueryModelRenderer.render(model)
val code = typeSpec.toString()
code.assertCompiles()
code.assertContains("class QCat : com.querydsl.core.types.dsl.EntityPathBase<Cat>")
code.assertContainLines("val _super: QAnimal by lazy { QAnimal(this) }")
}

@Test
fun genericTypeArgs() {
val model = QueryModel(
Expand Down

0 comments on commit 5af90e0

Please sign in to comment.