Search code examples
gradlejacksonmicronautswagger-codegengraalvm

How do I configure micronaut serialization to work with open api generated models?


Problem Description I have a project in which I am building a RESTful api with micronaut. The models returned in the response body are generated from an open api specification (using a gradle plugin).

However, for some reason, it micronaut always returns any model object as an empty json object {}, even when it should be a populated object.

Repository Link https://gitlab.com/connorbutch/open-api-micronaut-quarkus

Examples of output

Expected:

[
     {
           "id": 123
           "name": "Scruffy"
           "tag": "Chuck's pup"

     }
]

Actual: [{}]

How to reproduce

  • Clone code locally (git clone ....)
  • Navigate to the root directory (cd .......)
  • Run a gradle build (gradle clean build)
  • Navigate into reading-comprehension-server-impl (cd reading-comprehension-server-impl)
  • Run docker build (either docker build . -t .... or just run ./docker-build.sh) <-- this step will take a little bit of time and memory, so potentially close other applications
  • Run the docker image that was just built (docker run -p 8080:8080 reading-comprehension-server-impl)
  • Make a get request to the server (http://localhost:8080/pets?limit=4)

Ideas I have Some things I was curious about include:

  • could this be a (gradle) scope issue? Should I use api instead of implementation for the @Introspected annotation (just to see if it works)?
  • could this be an issue with the model being generated in another module/jar? Should I move the generation to the same project (just to see if it works)?
  • could this be an issue with different micronaut version across modules?
  • Is there somehow I can inject (or otherwise) get a reference to the bean serializer to debug further?

Other explorations

  • I modified one of the endpoints to return an object model from the same module (just a dummy response) and that was returned in the proper format.... so it might be either a version issue, or a build issue (since it's in another module)
  • I modified the included module to use the same micronaut version (1.3.0 for now, would like to bump to 2.x.x in future), but still doesn't serialize correctly
  • I created a copy of the class attempting to be serialized in the response body into this module, and it worked (see NotARealPet.java). Now the question is, can I configure this to work, or should I just (temporarily) change the api to generate in the build dir of this project?

Solution

  • I found the answer. Even if a class is compiled with @Introspected, the graal vm (native-image cli) does not look at those classes. In order to include these, I eventually ended up including a class specifying to introspect these classes @Introspected(classes = { Pet.class, Error.class})

    If anyone is interested, I'll post a gitlab link with examples