Skip to content

#355 Controller methods returning null should produce 204 no content … #360

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 13, 2023
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 @@ -186,19 +186,30 @@ void writeHandler(boolean requestScoped) {
writer.append(");").eol();

if (!method.isVoid() && !isFilter) {
writeContextReturn();
TypeMirror typeMirror = method.returnType();
boolean includeNoContent = !typeMirror.getKind().isPrimitive();
if (includeNoContent) {
writer.append(" if (result == null) {").eol();
writer.append(" res.status(NO_CONTENT_204).send();").eol();
writer.append(" } else {").eol();
}
String indent = includeNoContent ? " " : " ";
writeContextReturn(indent);
if (isInputStream(method.returnType())) {
final var uType = UType.parse(method.returnType());
writer.append(" result.transferTo(res.outputStream());", uType.shortName()).eol();
writer.append(indent).append("result.transferTo(res.outputStream());", uType.shortName()).eol();
} else if (producesJson()) {
if (returnTypeString()) {
writer.append(" res.send(result); // send raw JSON").eol();
writer.append(indent).append("res.send(result); // send raw JSON").eol();
} else {
final var uType = UType.parse(method.returnType());
writer.append(" %sJsonType.toJson(result, JsonOutput.of(res));", uType.shortName()).eol();
writer.append(indent).append("%sJsonType.toJson(result, JsonOutput.of(res));", uType.shortName()).eol();
}
} else {
writer.append(" res.send(result);").eol();
writer.append(indent).append("res.send(result);").eol();
}
if (includeNoContent) {
writer.append(" }").eol();
}
}
writer.append(" }").eol().eol();
Expand Down Expand Up @@ -260,14 +271,14 @@ private boolean usesFormParams() {
return method.params().stream().anyMatch(p -> p.isForm() || ParamType.FORMPARAM.equals(p.paramType()));
}

private void writeContextReturn() {
private void writeContextReturn(String indent) {
final var producesOp = Optional.ofNullable(method.produces());
if (producesOp.isEmpty() && !useJsonB) {
return;
}

final var produces = producesOp.map(MediaType::parse).orElse(MediaType.APPLICATION_JSON);
final var contentTypeString = " res.headers().contentType(MediaTypes.";
final var contentTypeString = "res.headers().contentType(MediaTypes.";
writer.append(indent);
switch (produces) {
case APPLICATION_JSON -> writer.append(contentTypeString).append("APPLICATION_JSON);").eol();
case TEXT_HTML -> writer.append(contentTypeString).append("TEXT_HTML);").eol();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,9 @@ void filter(FilterChain chain, RoutingResponse res) {
String formBean(MyForm form) {
return form.name + "|" + form.email + "|" + form.url;
}

@Get("maybeNoContent")
String maybeNoContent(Boolean empty) {
return Boolean.TRUE.equals(empty) ? null : "Hi";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ void hello() {
assertThat(res.body()).isEqualTo("Hello world - index");
}

@Test
void maybeNoContent() {
HttpResponse<String> res = client.request()
.path("test/maybeNoContent")
.queryParam("empty", false)
.GET()
.asString();

assertThat(res.statusCode()).isEqualTo(200);
assertThat(res.body()).isEqualTo("Hi");

HttpResponse<String> res2 = client.request()
.path("test/maybeNoContent")
.queryParam("empty", true)
.GET()
.asString();

assertThat(res2.statusCode()).isEqualTo(204);
assertThat(res2.body()).isEqualTo("");
}

@Test
void strBody() {
HttpResponse<String> res = client.request()
Expand Down