Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introspected POJO which uses Jackson JsonInclude.Include.NON_NULL) does not work with GraalVM #2933

Closed
sdelamo opened this issue Mar 17, 2020 · 1 comment · Fixed by #2935
Labels
relates-to: graal relates-to: jackson status: pr submitted A pull request has been submitted for the issue type: bug Something isn't working
Milestone

Comments

@sdelamo
Copy link
Collaborator

sdelamo commented Mar 17, 2020

Steps to reproduce

Generate a JAR with an introspected POJO and Jackson annotations

Generate a JAR with an introspected POJO:

% mkdir pojojar
% cd pojojar
pojojar %
pojojar % gradle wrapper
touch build.gradle
plugins {
    id 'java-library'
}

ext {
    micronautVersion = '1.3.3'
}

repositories {
    jcenter()
}

dependencies {
    annotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
    annotationProcessor "io.micronaut:micronaut-inject-java"
    annotationProcessor "io.micronaut:micronaut-validation"

    implementation platform("io.micronaut:micronaut-bom:$micronautVersion")
    implementation "io.micronaut:micronaut-inject"
    implementation "io.micronaut:micronaut-validation"

    api 'com.github.spotbugs:spotbugs-annotations:3.1.12'
    api "com.fasterxml.jackson.core:jackson-annotations"
    api "io.micronaut:micronaut-core"
    api "javax.validation:validation-api"
}

Create a POJO

pojojar % mkdir -p src/main/java/example/micronaut/pojos
pojojar % touch src/main/java/example/micronaut/pojos/Book.java

It uses @JsonInclude(JsonInclude.Include.NON_NULL) to specify that author field should be included in a serialised response only when populated.

package example.micronaut.pojos;

import com.fasterxml.jackson.annotation.JsonInclude;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import io.micronaut.core.annotation.Introspected;
import javax.validation.constraints.NotBlank;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Introspected
public class Book {
    @NonNull
    @NotBlank
    private String name;

    @Nullable
    private String author;

    public Book() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

Generate a JAR file

pojojar % ./gradlew assemble

Create a Micronaut App with GraalVM feature which uses the JAR

% ..
% mn create-app hello-world --features graal-native-image
% mkdir hello-world/libs
% cp pojojar/build/libs/pojojar.jar hello-world/libs/

Modify the build file generated by Micronaut CLI. Add in the dependencies block the next line:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    ...

Create a Controller which returns the POJO in the JAR.

Create a controller src/main/java/hello/world/BookController.java

package hello.world;

import example.micronaut.pojos.Book;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

@Controller
public class BookController {

    @Get
    Book index() {
        Book book = new Book();
        book.setName("Sergio");
        return book;
    }
}

Generate a FAT JAR

% cd hello-world
hello-world % ./gradle assemble

Executing the FAT JAR works with Jackson annotation

Jackson annotation @JsonInclude(JsonInclude.Include.NON_NULL) works as expected. Non null fields are not included:

hello-world % java -jar build/libs/hello-world-0.1-all.jar 
 % curl localhost:8080
{"name":"Sergio"}

Generate a Native Image

hello-world % docker build . -t hello-world    
Sending build context to Docker daemon  62.27MB
...
..
.
Removing intermediate container a9d8b99b7fb7
 ---> e35c6e00ce20
Successfully built e35c6e00ce20
Successfully tagged hello-world:latest
hello-world % docker run -p 8080:8080 hello-world         
/app/hello-world: /usr/lib/libstdc++.so.6: no version information available (required by /app/hello-world)
/app/hello-world: /usr/lib/libstdc++.so.6: no version information available (required by /app/hello-world)
06:36:03.398 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 29ms. Server Running: http://13c35759cdca:8080

Calling the controller returns a POJO which includes non-null fields.

hello-world % java -jar build/libs/hello-world-0.1-all.jar 
% curl localhost:8080
{"name":"Sergio","author":null}%       

Versions

Micronaut version: 1.3.3
JDK Version: 1.8
GraalVM Version: 20.0.0-java8

@graemerocher graemerocher added this to the 1.3.4 milestone Mar 17, 2020
sdelamo added a commit that referenced this issue Mar 17, 2020
sdelamo added a commit that referenced this issue Mar 17, 2020
@sdelamo sdelamo added the status: pr submitted A pull request has been submitted for the issue label Mar 17, 2020
jameskleeh pushed a commit to micronaut-projects/micronaut-cache that referenced this issue Mar 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relates-to: graal relates-to: jackson status: pr submitted A pull request has been submitted for the issue type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants