Skip to content

Commit 658c42c

Browse files
authored
[Jex generation] Raw JSON String handling in generated code (#528)
JSON response method that returns a Java String is deemed to be raw json (rather than json encoding the string content)
1 parent cd20f05 commit 658c42c

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,15 @@ private void writeContextReturn(ResponseMode responseMode, String resultVariable
283283
}
284284

285285
private void writeJsonReturn(String produces) {
286+
if (produces == null) {
287+
produces = MediaType.APPLICATION_JSON.getValue();
288+
}
289+
var uType = UType.parse(method.returnType());
290+
if ("java.lang.String".equals(method.returnType().toString())) {
291+
writer.append("ctx.contentType(\"%s\").write(result); // raw json", produces);
292+
return;
293+
}
286294
if (useJsonB) {
287-
var uType = UType.parse(method.returnType());
288-
if (produces == null) {
289-
produces = MediaType.APPLICATION_JSON.getValue();
290-
}
291295
writer.append(
292296
"%sJsonType.toJson(result, ctx.contentType(\"%s\").outputStream());",
293297
uType.shortName(), produces);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,14 @@ void put(HelloDto dto) {
6161
dto.hashCode();
6262
}
6363

64+
@Produces("text/plain")
6465
@Get("/bigInt/{val}")
6566
String testBigInt(BigInteger val) {
6667
return "hi|" + val;
6768
}
69+
70+
@Get("rawJson")
71+
String rawJsonString() {
72+
return "{\"key\": 42 }";
73+
}
6874
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@
352352
"200" : {
353353
"description" : "",
354354
"content" : {
355-
"application/json" : {
355+
"text/plain" : {
356356
"schema" : {
357357
"type" : "string"
358358
}
@@ -985,6 +985,27 @@
985985
}
986986
}
987987
},
988+
"/rawJson" : {
989+
"get" : {
990+
"tags" : [
991+
992+
],
993+
"summary" : "",
994+
"description" : "",
995+
"responses" : {
996+
"200" : {
997+
"description" : "",
998+
"content" : {
999+
"application/json" : {
1000+
"schema" : {
1001+
"type" : "string"
1002+
}
1003+
}
1004+
}
1005+
}
1006+
}
1007+
}
1008+
},
9881009
"/security/first" : {
9891010
"get" : {
9901011
"tags" : [

tests/test-jex/src/test/java/org/example/web/HelloControllerTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,27 @@ void splat2() {
5353
assertEquals("got name:one splat0:a/b splat1:x/y/z", client.request().path("splat2/one/a/b/other/x/y/z").GET().asString().body());
5454
}
5555

56+
@Test
57+
void plainText() {
58+
final HttpResponse<String> hres = client.request()
59+
.path("bigInt/42")
60+
.GET().asString();
61+
62+
assertThat(hres.statusCode()).isEqualTo(200);
63+
assertThat(hres.body()).contains("hi|42");
64+
}
65+
66+
@Test
67+
void rawJson() {
68+
final HttpResponse<String> hres = client.request()
69+
.path("rawJson")
70+
.GET().asString();
71+
72+
assertThat(hres.statusCode()).isEqualTo(200);
73+
assertThat(hres.headers().firstValue("Content-Type").orElseThrow()).isEqualTo("application/json");
74+
assertThat(hres.body()).contains("{\"key\": 42 }");
75+
}
76+
5677
@Test
5778
void validation() {
5879
HelloDto helloDto = new HelloDto();

0 commit comments

Comments
 (0)