Skip to content

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

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

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class JsonBUtil {
public final class JsonBUtil {

private JsonBUtil() {}

public static boolean isJsonMimeType(String producesMimeType) {
return producesMimeType == null || producesMimeType.toLowerCase().contains("application/json");
}

public static Map<String, UType> jsonTypes(ControllerReader reader) {

final Map<String, UType> jsonTypes = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@

import java.util.List;

import io.avaje.http.generator.core.Append;
import io.avaje.http.generator.core.CoreWebMethod;
import io.avaje.http.generator.core.MethodParam;
import io.avaje.http.generator.core.MethodReader;
import io.avaje.http.generator.core.PathSegments;
import io.avaje.http.generator.core.UType;
import io.avaje.http.generator.core.Util;
import io.avaje.http.generator.core.WebMethod;
import io.avaje.http.generator.core.*;
import io.avaje.http.generator.core.openapi.MediaType;

/** Write code to register Web route for a given controller method. */
Expand Down Expand Up @@ -158,8 +151,9 @@ private void writeContextReturn() {
}

private void writeContextReturn(final String resultVariableName) {
final var produces = method.produces();
if (produces == null || MediaType.APPLICATION_JSON.getValue().equalsIgnoreCase(produces)) {
var produces = method.produces();
boolean applicationJson = produces == null || MediaType.APPLICATION_JSON.getValue().equalsIgnoreCase(produces);
if (applicationJson || JsonBUtil.isJsonMimeType(produces)) {
if (useJsonB) {
var uType = UType.parse(method.returnType());
final boolean isfuture = "java.util.concurrent.CompletableFuture".equals(uType.mainType());
Expand All @@ -169,12 +163,19 @@ private void writeContextReturn(final String resultVariableName) {
}
writer.append(" try {");
}
writer.append(" %sJsonType.toJson(%s, ctx.contentType(\"application/json\").res().getOutputStream());", uType.shortName(), resultVariableName);
if (produces == null) {
produces = MediaType.APPLICATION_JSON.getValue();
}
writer.append(" %sJsonType.toJson(%s, ctx.contentType(\"%s\").res().getOutputStream());", uType.shortName(), resultVariableName, produces);
if (isfuture || method.isErrorMethod()) {
writer.append(" } catch (java.io.IOException e) { throw new java.io.UncheckedIOException(e); }");
}
} else {
writer.append(" ctx.json(%s);", resultVariableName);
if (applicationJson) {
writer.append(" ctx.json(%s);", resultVariableName);
} else {
writer.append(" ctx.contentType(\"%s\").json(%s);", produces, resultVariableName);
}
}
} else if (MediaType.TEXT_HTML.getValue().equalsIgnoreCase(produces)) {
writer.append(" ctx.html(%s);", resultVariableName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
import static io.avaje.http.generator.core.ProcessingContext.platform;
import java.util.List;

import io.avaje.http.generator.core.Append;
import io.avaje.http.generator.core.MethodParam;
import io.avaje.http.generator.core.MethodReader;
import io.avaje.http.generator.core.PathSegments;
import io.avaje.http.generator.core.Util;
import io.avaje.http.generator.core.WebMethod;
import io.avaje.http.generator.core.*;
import io.avaje.http.generator.core.openapi.MediaType;

/**
Expand Down Expand Up @@ -102,6 +97,8 @@ private void writeContextReturn() {
writer.append("ctx.html(");
} else if (produces.equalsIgnoreCase(MediaType.TEXT_PLAIN.getValue())) {
writer.append("ctx.text(");
} else if (JsonBUtil.isJsonMimeType(produces)) {
writer.append("ctx.contentType(\"%s\").json(", produces);
} else {
writer.append("ctx.contentType(\"%s\").write(", produces);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ List<HelloDto> findByName(String name, @QueryParam("my-param") @Default("one") S
/**
* Simple example post with JSON body response.
*/
@Produces(MediaType.APPLICATION_JSON_PATCH_JSON)
@Post
HelloDto post(HelloDto dto) {
dto.name = "posted";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"openapi" : "3.0.1",
"info" : {
"title" : "Example service showing off the Path extension method of controller",
"title" : "Example service",
"description" : "Example Javalin controllers with Java and Maven",
"version" : ""
},
Expand All @@ -14,11 +14,11 @@
"tags" : [
{
"name" : "tag1",
"description" : "this is added to openapi tags"
"description" : "it's somethin"
},
{
"name" : "tag1",
"description" : "it's somethin"
"description" : "this is added to openapi tags"
}
],
"paths" : {
Expand Down Expand Up @@ -320,7 +320,7 @@
"201" : {
"description" : "",
"content" : {
"application/json" : {
"application/json-patch+json" : {
"schema" : {
"$ref" : "#/components/schemas/HelloDto"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ List<HelloDto> findByName(String name, @QueryParam("my-param") @Default("one") S
/**
* Simple example post with JSON body response.
*/
@Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
@Post
HelloDto post(HelloDto dto) {
dto.name = "posted";
Expand Down
2 changes: 1 addition & 1 deletion tests/test-javalin/src/main/resources/public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@
"201" : {
"description" : "",
"content" : {
"application/json" : {
"application/json;charset=UTF-8" : {
"schema" : {
"$ref" : "#/components/schemas/HelloDto"
}
Expand Down