Razvan
10/10/2020, 8:31 PM@Test
fun `Lens exemple`() {
data class User(val name: String, val age: Int)
val idLens = <http://Path.int|Path.int>().of("id")
val jsonBody = Body.auto<User>().toLens()
val user: HttpHandler = routes(
"users/{id}" bind GET to { r: Request ->
jsonBody.inject(User("Tom", 30 + idLens.extract(r)), Response(OK))
}
)
val responseOk = user(Request(GET, "users/2"))
val resposeObject = jsonBody.extract(responseOk)
resposeObject.age shouldBe 32
}
But I get a Parser error:
Signature Parse error: expected '<' or ';' but got
Remaining input: exemple$User;>;
If I move the User
class out of the function is all good, but I was wandering it it’s supposed to not work or it’s a bug and should I report it (and if so, to whom is it your fault or Jackson’s).dave
10/10/2020, 8:50 PMRazvan
10/11/2020, 9:57 AM@Test
fun jacksonLocalClass() {
val json = """{"name":"Tom","age":32}"""
data class User(val name: String, val age: Int)
val mapper = jacksonObjectMapper()
val user = mapper.readValue(json, User::class.java)
val user2: User = mapper.readValue(json)
println(user)
println(user2)
user shouldNot beNull()
}
I’ll try to dig around the Body.auto
lens to see if I can figure it out.ConfigurableJackson.kt
of http4k-format-jackson
in the ObjectMapper.write()
on the line
val typeRef = jacksonTypeRef<T>()
then in Jackson’s TypeReference
class this is an object like Htto4kTest$Lens exemple$$inlined$auto$2$1@2972
(inlined class looks like something you talked about). So that function calls a JDK Class.java
to `getGenericSuperclass()`that calls getGenericInfo()
and there the line
String signature = getGenericSignature0();
appears the problem.
signature = "Lcom/fasterxml/jackson/core/type/TypeReference<Ltest/Http4KTest$Lens exemple$User;>;"
dave
10/11/2020, 3:06 PMRazvan
10/11/2020, 3:07 PMgetGenericSignature0()
is implemented as it’s defined as
// Generic signature handling
private native String getGenericSignature0();
dave
10/11/2020, 3:22 PMpackage org.http4k
import org.http4k.core.Body
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.format.Jackson.auto
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class `Bug??` {
@Test
fun lensTest() {
val json = """{"name":"Tom","age":32}"""
data class User(val name: String, val age: Int)
assertEquals(Body.auto<User>().toLens()(Request(<http://Method.POST|Method.POST>, "").body(json)), User("Tom", 32))
}
}
Razvan
10/11/2020, 3:26 PM@Test
fun `Lens exemple`() {
data class User(val name: String, val age: Int)
val jsonBody: BiDiBodyLens<User> = Body.auto<User>().toLens()
val tom = User("Tom", 30)
val response = jsonBody.inject(tom, Response(OK))
println(response.bodyString())
}
dave
10/12/2020, 8:01 AMRazvan
10/12/2020, 10:37 AM