Inferred type mismatch from kotlin to java to kotlin to java

#1

Hi all,
I have an issue with Java usage from Kotlin. I’m trying Micronaut with Kotlin. I’m trying to handle exception in the rest stack and when I write the following code:

124       val error: JsonError = jsonError
125            .link(Link.SELF, Link.of(request.uri))
126
127       return HttpResponse
128            .status<JsonError>(httpStatus)
129            .body(error)

I get the following error:
Controller.kt:[129,23] Type mismatch: inferred type is io.micronaut.http.hateoas.JsonError! but io.micronaut.http.hateos.JsonError? was expected

The jsonError object is a Micronaut Java class and the link(…) call line 125 is inferred as a JsonError!
But the HttpResponse.body(@Nullable body) method is annotated with @Nullable so the inferred type is JsonError? and here I get my error. How could I fix this issue?

Thanks,

Romain.

#2

Does it help to declare error as nullable, like this: val error: JsonError?