Skip to content

Commit 38370c2

Browse files
authored
#452 The generator does not work when using @produces to customize the JSON MIME type (#454)
* #452 - The generator does not work when using `@Produces` to customize the JSON MIME type e.g. ``` @produces(MediaType.APPLICATION_JSON + ";charset=UTF-8") ``` * #452 - The generator does not work when using `@Produces` to customize the JSON MIME type e.g. ``` @produces(MediaType.APPLICATION_JSON + ";charset=UTF-8") ```
1 parent 4771d39 commit 38370c2

File tree

7 files changed

+29
-24
lines changed

7 files changed

+29
-24
lines changed

http-generator-core/src/main/java/io/avaje/http/generator/core/JsonBUtil.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
import java.util.function.Consumer;
66
import java.util.stream.Collectors;
77

8-
public class JsonBUtil {
8+
public final class JsonBUtil {
9+
910
private JsonBUtil() {}
1011

12+
public static boolean isJsonMimeType(String producesMimeType) {
13+
return producesMimeType == null || producesMimeType.toLowerCase().contains("application/json");
14+
}
15+
1116
public static Map<String, UType> jsonTypes(ControllerReader reader) {
1217

1318
final Map<String, UType> jsonTypes = new LinkedHashMap<>();

http-generator-javalin/src/main/java/io/avaje/http/generator/javalin/ControllerMethodWriter.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44

55
import java.util.List;
66

7-
import io.avaje.http.generator.core.Append;
8-
import io.avaje.http.generator.core.CoreWebMethod;
9-
import io.avaje.http.generator.core.MethodParam;
10-
import io.avaje.http.generator.core.MethodReader;
11-
import io.avaje.http.generator.core.PathSegments;
12-
import io.avaje.http.generator.core.UType;
13-
import io.avaje.http.generator.core.Util;
14-
import io.avaje.http.generator.core.WebMethod;
7+
import io.avaje.http.generator.core.*;
158
import io.avaje.http.generator.core.openapi.MediaType;
169

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

160153
private void writeContextReturn(final String resultVariableName) {
161-
final var produces = method.produces();
162-
if (produces == null || MediaType.APPLICATION_JSON.getValue().equalsIgnoreCase(produces)) {
154+
var produces = method.produces();
155+
boolean applicationJson = produces == null || MediaType.APPLICATION_JSON.getValue().equalsIgnoreCase(produces);
156+
if (applicationJson || JsonBUtil.isJsonMimeType(produces)) {
163157
if (useJsonB) {
164158
var uType = UType.parse(method.returnType());
165159
final boolean isfuture = "java.util.concurrent.CompletableFuture".equals(uType.mainType());
@@ -169,12 +163,19 @@ private void writeContextReturn(final String resultVariableName) {
169163
}
170164
writer.append(" try {");
171165
}
172-
writer.append(" %sJsonType.toJson(%s, ctx.contentType(\"application/json\").res().getOutputStream());", uType.shortName(), resultVariableName);
166+
if (produces == null) {
167+
produces = MediaType.APPLICATION_JSON.getValue();
168+
}
169+
writer.append(" %sJsonType.toJson(%s, ctx.contentType(\"%s\").res().getOutputStream());", uType.shortName(), resultVariableName, produces);
173170
if (isfuture || method.isErrorMethod()) {
174171
writer.append(" } catch (java.io.IOException e) { throw new java.io.UncheckedIOException(e); }");
175172
}
176173
} else {
177-
writer.append(" ctx.json(%s);", resultVariableName);
174+
if (applicationJson) {
175+
writer.append(" ctx.json(%s);", resultVariableName);
176+
} else {
177+
writer.append(" ctx.contentType(\"%s\").json(%s);", produces, resultVariableName);
178+
}
178179
}
179180
} else if (MediaType.TEXT_HTML.getValue().equalsIgnoreCase(produces)) {
180181
writer.append(" ctx.html(%s);", resultVariableName);

http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerMethodWriter.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
import static io.avaje.http.generator.core.ProcessingContext.platform;
44
import java.util.List;
55

6-
import io.avaje.http.generator.core.Append;
7-
import io.avaje.http.generator.core.MethodParam;
8-
import io.avaje.http.generator.core.MethodReader;
9-
import io.avaje.http.generator.core.PathSegments;
10-
import io.avaje.http.generator.core.Util;
11-
import io.avaje.http.generator.core.WebMethod;
6+
import io.avaje.http.generator.core.*;
127
import io.avaje.http.generator.core.openapi.MediaType;
138

149
/**
@@ -102,6 +97,8 @@ private void writeContextReturn() {
10297
writer.append("ctx.html(");
10398
} else if (produces.equalsIgnoreCase(MediaType.TEXT_PLAIN.getValue())) {
10499
writer.append("ctx.text(");
100+
} else if (JsonBUtil.isJsonMimeType(produces)) {
101+
writer.append("ctx.contentType(\"%s\").json(", produces);
105102
} else {
106103
writer.append("ctx.contentType(\"%s\").write(", produces);
107104
}

tests/test-javalin-jsonb/src/main/java/org/example/myapp/web/HelloController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ List<HelloDto> findByName(String name, @QueryParam("my-param") @Default("one") S
8282
/**
8383
* Simple example post with JSON body response.
8484
*/
85+
@Produces(MediaType.APPLICATION_JSON_PATCH_JSON)
8586
@Post
8687
HelloDto post(HelloDto dto) {
8788
dto.name = "posted";

tests/test-javalin-jsonb/src/main/resources/public/openapi.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"openapi" : "3.0.1",
33
"info" : {
4-
"title" : "Example service showing off the Path extension method of controller",
4+
"title" : "Example service",
55
"description" : "Example Javalin controllers with Java and Maven",
66
"version" : ""
77
},
@@ -14,11 +14,11 @@
1414
"tags" : [
1515
{
1616
"name" : "tag1",
17-
"description" : "this is added to openapi tags"
17+
"description" : "it's somethin"
1818
},
1919
{
2020
"name" : "tag1",
21-
"description" : "it's somethin"
21+
"description" : "this is added to openapi tags"
2222
}
2323
],
2424
"paths" : {
@@ -320,7 +320,7 @@
320320
"201" : {
321321
"description" : "",
322322
"content" : {
323-
"application/json" : {
323+
"application/json-patch+json" : {
324324
"schema" : {
325325
"$ref" : "#/components/schemas/HelloDto"
326326
}

tests/test-javalin/src/main/java/org/example/myapp/web/HelloController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ List<HelloDto> findByName(String name, @QueryParam("my-param") @Default("one") S
8282
/**
8383
* Simple example post with JSON body response.
8484
*/
85+
@Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
8586
@Post
8687
HelloDto post(HelloDto dto) {
8788
dto.name = "posted";

tests/test-javalin/src/main/resources/public/openapi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@
310310
"201" : {
311311
"description" : "",
312312
"content" : {
313-
"application/json" : {
313+
"application/json;charset=UTF-8" : {
314314
"schema" : {
315315
"$ref" : "#/components/schemas/HelloDto"
316316
}

0 commit comments

Comments
 (0)