Skip to content

[Jex generation] Raw JSON String handling in generated code #528

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 1 commit into from
Dec 3, 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 @@ -283,11 +283,15 @@ private void writeContextReturn(ResponseMode responseMode, String resultVariable
}

private void writeJsonReturn(String produces) {
if (produces == null) {
produces = MediaType.APPLICATION_JSON.getValue();
}
var uType = UType.parse(method.returnType());
if ("java.lang.String".equals(method.returnType().toString())) {
writer.append("ctx.contentType(\"%s\").write(result); // raw json", produces);
return;
}
if (useJsonB) {
var uType = UType.parse(method.returnType());
if (produces == null) {
produces = MediaType.APPLICATION_JSON.getValue();
}
writer.append(
"%sJsonType.toJson(result, ctx.contentType(\"%s\").outputStream());",
uType.shortName(), produces);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,14 @@ void put(HelloDto dto) {
dto.hashCode();
}

@Produces("text/plain")
@Get("/bigInt/{val}")
String testBigInt(BigInteger val) {
return "hi|" + val;
}

@Get("rawJson")
String rawJsonString() {
return "{\"key\": 42 }";
}
}
23 changes: 22 additions & 1 deletion tests/test-jex/src/main/resources/public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@
"200" : {
"description" : "",
"content" : {
"application/json" : {
"text/plain" : {
"schema" : {
"type" : "string"
}
Expand Down Expand Up @@ -985,6 +985,27 @@
}
}
},
"/rawJson" : {
"get" : {
"tags" : [

],
"summary" : "",
"description" : "",
"responses" : {
"200" : {
"description" : "",
"content" : {
"application/json" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}
},
"/security/first" : {
"get" : {
"tags" : [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ void splat2() {
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());
}

@Test
void plainText() {
final HttpResponse<String> hres = client.request()
.path("bigInt/42")
.GET().asString();

assertThat(hres.statusCode()).isEqualTo(200);
assertThat(hres.body()).contains("hi|42");
}

@Test
void rawJson() {
final HttpResponse<String> hres = client.request()
.path("rawJson")
.GET().asString();

assertThat(hres.statusCode()).isEqualTo(200);
assertThat(hres.headers().firstValue("Content-Type").orElseThrow()).isEqualTo("application/json");
assertThat(hres.body()).contains("{\"key\": 42 }");
}

@Test
void validation() {
HelloDto helloDto = new HelloDto();
Expand Down
Loading