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

s1m0nw1

06/15/2021, 1:13 PM
Does the following make sense? The JSON lens is typed as non-nullable
X
yet gives out a
null
pointer
Copy code
public fun main() {
    val l: BiDiBodyLens<X> = Body.auto<X>().toLens()
    val request: Request = Request(POST, "/").body("null")
    val extracted: X = l.extract(request)
    print(extracted) // null :shrug:
}
d

dave

06/15/2021, 2:17 PM
TBH I'm not sure - which module are you using? I know for instance that GSON doesn't support nullability correctly
also - although null is a valid JSON value, it may depend on if the implementors were expecting an object or an array
s

s1m0nw1

06/15/2021, 2:48 PM
it's format-jackson in the most recent version
IMHO, the lens shouldn't give out a null pointer anyways if it's configured as non-nullable, it's maybe not properly caught coming from a Java module somewhere?
r

Razvan

06/16/2021, 7:48 AM
Yep if you want a failing test:
Copy code
@Test
    fun jacksonNullMappingError() {
        data class TestData(val s: String)
        val l: BiDiBodyLens<TestData> = Body.auto<TestData>().toLens()
        val request: Request = Request(<http://Method.POST|Method.POST>, "/").body("null")
        val extracted: TestData = l.extract(request)
        print(extracted) // null :shrug:
        extracted shouldNot beNull()
    }
using Moshi.auto does as expected exception in lens
Copy code
body 'body' must be string
org.http4k.lens.LensFailure: body 'body' must be string
d

dave

06/16/2021, 7:55 AM
cool. I'll add something to the contract to try and get more info. I suspect Jackson isn't the only one that is a problem...
r

Razvan

06/16/2021, 7:56 AM
I guess all Java based one I guess it's TestData! that the extract returns that's why is null
17 Views