I have a kotlin class with a non-nullable property, which is not using a primitive type in the jvm, and I'm trying to use the properties of that class as request parameters. If I don't include that property as a request parameter, I would expect a bad request response. However, it instead crashes with this exception:
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method no.finntech.iaap.data_import.Params.<init>, parameter a
This is the code I'm using:
class Params(
val a: String
)
@SpringBootApplication
@RestController
class Application {
@GetMapping("/test")
fun test(params: Params) = ""
}
If a
is of type Int
instead, or if I use a non-nullable String
directly in the arguments to test
as a RequestParam
, I get a bad request as expected.
Comment From: sdeleuze
First, sorry for the delay for a response.
For Int
, in ModelAttributeMethodProcessor#constructAttribute
we get a TypeMismatchException: Failed to convert value of type 'null' to required type 'int'; Failed to convert from type [null] to type [int] for value 'null'
which makes senses since int
is a primitive type so not nullable at Java/JVM level.
For String
, the Java type is nullable, so that's only at BeanUtils.instantiateClass(ctor, args)
level that Kotlin awareness of nullness is involved and raises this exception that is from what I can see java.lang.NullPointerException: Parameter specified as non-null is null: method com.example.demokotlinnative.Params.<init>, parameter a
.
I will discuss with the team if there is something to refine here or not.
Comment From: sdeleuze
We can potentially try to wrap it for Kotlin parameter use case, I will give it a try and see how it goes.