avaje/avaje-http

The generator does not work when using @Produces to customize the JSON MIME type

ponyjoy opened this issue · 3 comments

When using the avaje-http-javalin-generator version 2.4, I encountered an issue where, within a Controller, if a MediaType is specified and a return type of an object is expected to produce a JSON result, the generated code causes the compilation to fail.
Test code

ApiResult.java

package nxu.it.model;

public class ApiResult {
    private int code = 200;
    private boolean success = true;
    private String message;

    public ApiResult() {
    }

    public ApiResult(int code, boolean success, String message) {
        this.code = code;
        this.success = success;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public ApiResult setMessage(String message) {
        this.message = message;
        return this;
    }

    public static ApiResult ok(String message) {
        return ok(200, message);
    }

    public static ApiResult ok(int code, String message) {
        return new ApiResult(code, true, message);
    }

    public static ApiResult fail(String message) {
        return fail(400, message);
    }

    public static ApiResult fail(int code, String message) {
        return new ApiResult(code, false, message);
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", success=" + success +
                ", message='" + message + '\'' +
                '}';
    }
}

MainController.java

import io.avaje.http.api.Controller;
import io.avaje.http.api.Get;
import io.avaje.http.api.MediaType;
import io.avaje.http.api.Produces;


@Controller("/")
public class MainController {

    @Get("/json")
    @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
    public ApiResult json(){
        return ApiResult.ok("request success");
    }
}

The generated MainController$Route code is as follows:

@Generated("avaje-javalin-generator")
@Singleton
public class MainController$Route extends AvajeJavalinPlugin{
   //...
private void routes(JavalinDefaultRouting app) {
      app.get("/json", ctx -> {
           ctx.status(200);
           var result = controller.json();
           //compile error here: Context haven't json(ApiResult) method
          ctx.contentType("application/json;charset=UTF-8").result(result);  
      });
}

http-generator-javalin/src/main/java/io/avaje/http/generator/javalin/ControllerMethodWriter.java
in writeContextReturn:
image
Should it be modified to force the use of Context.json for JSON generation when the 'produce' includes the substring 'application/json', or should an annotation such as @JSON be used to explicitly indicate that JSON should be returned?

when the 'produce' includes the substring 'application/json'

I think this option. We know it's json from that.

Just to add, that we should also add a MediaType.APPLICATION_JSON_UTF8

Thanks, excellent bug report !! You can try 2.6-RC2 which has just been released with the fix.

Thank you very much for your hard work. 🌷