https://kotlinlang.org logo
#http4k
Title
# http4k
m

MrNiamh

03/09/2024, 4:03 PM
Hey, I seem to be having an issue where even though my request has a non-nullable string, it seems to allow nulls. I've made sure i'm up to date. Example in thread ⬇️ I was trying to see where this error was happening, is it a http4k or a Jackson or a bit of both?
A simple example:
Copy code
val lens = Jackson.autoBody<TestDTO>().toLens()
fun testEndpoint(): ContractRoute =
    "/test" meta {
        receiving(lens to TestDTO.example)
        returning(Status.OK)
    } bindContract Method.POST to
            { request: Request ->
                val r = lens(request)
                println(r.values.first().length)
                Response(OK)
            }

data class TestDTO(
    val values: List<String>,
) {
    companion object {
        val example = TestDTO(
            listOf("Hello"),
        )
    }
}
And you can curl it
Copy code
curl -X 'POST' \
  '<http://localhost:9000/test>' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -d '{
  "values": [
    null
  ]
}'
You should
500
because it can't call
.length
on a null string
d

dave

03/09/2024, 4:29 PM
this is Jackson :
Copy code
val a = ObjectMapper().registerModules(KotlinModule.Builder().build())
        println(a.readValue("""{"values":["null"]}""", TestDTO::class.java))
I'd suggest maybe asking on the jackson-kotlin module github?
unfortunately you can't even get around this with a value type.
m

MrNiamh

03/09/2024, 4:33 PM
Aye super annoying, tried a couple workarounds, just had to make the thing nullable and deal with it myself. A bit scary though! Thanks for the help, i'll go annoy them
👍 1
d

dave

03/09/2024, 4:37 PM
@MrNiamh you can do this hack:
Copy code
data class TestDTO(
        val values: List<String>,
    ) {
        init {
            require(values.map { it!! }.isNotEmpty())
        }
    }
Or even just
Copy code
data class TestDTO(
        val values: List<String>,
    ) {
        init {
            values.forEach { it!! }
        }
    }
m

MrNiamh

03/09/2024, 4:41 PM
Looks like a long running problem and applies to basically anything, spooky. Thanks i'll use the hack 😎
e

Emil Kantis

03/09/2024, 5:19 PM
Consider switching to KotlinX serialization? 😎