Skip to content

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

Closed
@ponyjoy

Description

@ponyjoy

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?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions