Skip to content
This repository has been archived by the owner on Jun 30, 2023. It is now read-only.

Endpoint Support for Kotlin Data Classes #163

Open
aaronvital opened this issue Sep 7, 2018 · 0 comments
Open

Endpoint Support for Kotlin Data Classes #163

aaronvital opened this issue Sep 7, 2018 · 0 comments

Comments

@aaronvital
Copy link

I'm trying to use Kotlin for Google Java Cloud Endpoints, but I'm getting the following Exception deserializing Kotlin data classes

INFO: exception occurred while calling backend method
com.google.api.server.spi.response.BadRequestException: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of my.package.Foo: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
 at [Source: UNKNOWN; line: -1, column: -1]
        at com.google.api.server.spi.request.RestServletRequestParamReader.read(RestServletRequestParamReader.java:128)
        at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:349)
        at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:119)
        at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:102)
        at com.google.api.server.spi.dispatcher.PathDispatcher.dispatch(PathDispatcher.java:50)
        at com.google.api.server.spi.EndpointsServlet.service(EndpointsServlet.java:71)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
        ...

My Kotlin files...

 // file: Foo.kt

 data class Foo(val bar: String) {
 }


 // file: MyEndpoint.kt

 // My GET works fine as Serialization Does work
 @ApiMethod(name = "getFoo",
         description = "testing get",
         path = "getFoo",
         httpMethod = ApiMethod.HttpMethod.GET)
 fun getFoo(): Foo {
     val myFoo = Foo("myBar")
     return myFoo
 }

 // My Post doesn't work, de-serialization is broken
 @ApiMethod(name = "postFoo",
         description = "testing post",
         path = "postFoo",
         httpMethod = ApiMethod.HttpMethod.POST)
 fun postFoo(myFoo: Foo): Foo {
     val postFoo = Foo(myFoo.bar)
     return postFoo
 }

I can see that I need to add the KotlinModule to the Jackson ObjectMapper

 // file: build.gradle    
 compile("com.fasterxml.jackson.module:jackson-module-kotlin:2.8.4")

 // adding this will fix the problem, but Where??
 objectMapper.registerModule(new KotlinModule());

Where do I manupulate the 'Global' ObjectMapper for Java Endpoints?

I can see that I could add a Transformer to either the @Api or the data classes themselves.
If I add it to the @Api, I don't appear to get any type information, so that's out

Adding it to the data classes involves me making another Transformer for Every data class. Which
seems it would work, but adds A LOT of cruft.

Is there an elegant solution for setting/modifying the default ObjectMapper?

my endpointsFrameworkVersion = "2.0.8"

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant