Skip to content

Commit 5d6aa32

Browse files
committed
responseCode is ignored from controllers returning kotlin.Unit #1545
Second try. 204 is set as default response for void/unit DELETE routes
1 parent 92826cf commit 5d6aa32

10 files changed

Lines changed: 50 additions & 13 deletions

File tree

modules/jooby-apt/src/main/java/io/jooby/internal/apt/HandlerCompiler.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import static org.objectweb.asm.Opcodes.ASTORE;
4343
import static org.objectweb.asm.Opcodes.CHECKCAST;
4444
import static org.objectweb.asm.Opcodes.GETSTATIC;
45+
import static org.objectweb.asm.Opcodes.IFEQ;
4546
import static org.objectweb.asm.Opcodes.INVOKEINTERFACE;
4647
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
4748
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
@@ -222,14 +223,14 @@ private void processArguments(ClassWriter classWriter, MethodVisitor visitor,
222223

223224
private void setDefaultResponseType(MethodVisitor visitor) throws Exception {
224225
TypeKind kind = executable.getReturnType().getKind();
225-
if (kind == TypeKind.VOID) {
226+
if (kind == TypeKind.VOID && getHttpMethod().equalsIgnoreCase(Router.DELETE)) {
226227
visitor.visitVarInsn(ALOAD, 1);
227228
visitor
228229
.visitFieldInsn(GETSTATIC, STATUS_CODE.getInternalName(), "NO_CONTENT",
229230
STATUS_CODE.getDescriptor());
230-
Method sendStatusCode = Context.class.getDeclaredMethod("send", StatusCode.class);
231-
visitor.visitMethodInsn(INVOKEINTERFACE, CTX.getInternalName(), sendStatusCode.getName(),
232-
getMethodDescriptor(sendStatusCode), true);
231+
Method setResponseCode = Context.class.getDeclaredMethod("setResponseCode", StatusCode.class);
232+
visitor.visitMethodInsn(INVOKEINTERFACE, CTX.getInternalName(), setResponseCode.getName(),
233+
getMethodDescriptor(setResponseCode), true);
233234
visitor.visitInsn(POP);
234235
}
235236
}
@@ -238,6 +239,24 @@ private void processReturnType(MethodVisitor visitor) throws Exception {
238239
TypeKind kind = executable.getReturnType().getKind();
239240
if (kind == TypeKind.VOID) {
240241
visitor.visitVarInsn(ALOAD, 1);
242+
Method isResponseStarted = Context.class.getDeclaredMethod("isResponseStarted");
243+
visitor.visitMethodInsn(INVOKEINTERFACE, CTX.getInternalName(), isResponseStarted.getName(),
244+
getMethodDescriptor(isResponseStarted), true);
245+
Label label0 = new Label();
246+
visitor.visitJumpInsn(IFEQ, label0);
247+
visitor.visitVarInsn(ALOAD, 1);
248+
visitor.visitInsn(ARETURN);
249+
visitor.visitLabel(label0);
250+
visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
251+
252+
visitor.visitVarInsn(ALOAD, 1);
253+
visitor.visitVarInsn(ALOAD, 1);
254+
Method getResponseCode = Context.class.getDeclaredMethod("getResponseCode");
255+
visitor.visitMethodInsn(INVOKEINTERFACE, CTX.getInternalName(), getResponseCode.getName(),
256+
getMethodDescriptor(getResponseCode), true);
257+
Method sendStatusCode = Context.class.getDeclaredMethod("send", StatusCode.class);
258+
visitor.visitMethodInsn(INVOKEINTERFACE, CTX.getInternalName(), sendStatusCode.getName(),
259+
getMethodDescriptor(sendStatusCode), true);
241260
} else {
242261
Method wrapper = Primitives.wrapper(kind);
243262
if (wrapper == null) {

modules/jooby-apt/src/test/java/output/MyControllerHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public MyControllerHandler(Provider<MyController> provider) {
2222
@Nonnull @Override public Object apply(@Nonnull Context ctx) throws Exception {
2323
ctx.setResponseCode(StatusCode.NO_CONTENT);
2424
provider.get().controllerMethod();
25-
return ctx;
25+
if (ctx.isResponseStarted()) {
26+
return ctx;
27+
} else {
28+
return ctx.send(ctx.getResponseCode());
29+
}
2630
}
2731
}

modules/jooby-apt/src/test/java/source/Controller1545.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public class Controller1545 {
1313
public void voidDefault() {
1414
}
1515

16+
@DELETE("/success")
17+
public void voidDeleteSuccess(Context ctx) {
18+
ctx.setResponseCode(StatusCode.OK);
19+
}
20+
1621
@POST
1722
public void voidCreated(Context ctx) {
1823
ctx.setResponseCode(StatusCode.CREATED);

modules/jooby-apt/src/test/java/source/Provisioning.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.jooby.Session;
1010
import io.jooby.StatusCode;
1111
import io.jooby.annotations.CookieParam;
12+
import io.jooby.annotations.DELETE;
1213
import io.jooby.annotations.FlashParam;
1314
import io.jooby.annotations.FormParam;
1415
import io.jooby.annotations.GET;
@@ -305,7 +306,7 @@ public StatusCode statusCode(@QueryParam StatusCode statusCode, @QueryParam Stri
305306
return statusCode;
306307
}
307308

308-
@GET("/noContent")
309+
@DELETE("/noContent")
309310
public void noContent() {
310311
}
311312

modules/jooby-apt/src/test/java/source/VoidRoute.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package source;
22

3-
import io.jooby.annotations.GET;
3+
import io.jooby.annotations.DELETE;
44
import io.jooby.annotations.Path;
55

66
@Path("/void")
77
public class VoidRoute {
88

9-
@GET
9+
@DELETE
1010
public void noContent() {
1111

1212
}

modules/jooby-apt/src/test/java/tests/HandlerCompilerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public void returnTypes() throws Exception {
334334

335335
ctx = MockContextHelper.mockContext();
336336
assertEquals(ctx,
337-
router.get("/p/noContent", ctx.setQueryString(null)).value());
337+
router.delete("/p/noContent", ctx.setQueryString(null)).value());
338338
assertEquals(StatusCode.NO_CONTENT, ctx.getResponseCode());
339339

340340
ctx = MockContextHelper.mockContext();

modules/jooby-apt/src/test/java/tests/Issue1545.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public void shouldSetNoContentCodeForVoidRoute() throws Exception {
1919
assertEquals(StatusCode.NO_CONTENT, rsp.getStatusCode());
2020
});
2121

22+
router.delete("/1545/success", rsp -> {
23+
assertEquals(StatusCode.OK, rsp.getStatusCode());
24+
});
25+
2226
router.post("/1545", rsp -> {
2327
assertEquals(StatusCode.CREATED, rsp.getStatusCode());
2428
});

modules/jooby-apt/src/test/java/tests/ModuleCompilerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void voidRoutes() throws Exception {
7878
new MvcModuleCompilerRunner(new VoidRoute())
7979
.module(app -> {
8080
MockRouter router = new MockRouter(app);
81-
router.get("/void", rsp -> {
81+
router.delete("/void", rsp -> {
8282
assertEquals(StatusCode.NO_CONTENT, rsp.getStatusCode());
8383
});
8484
});

tests/src/test/java/examples/InstanceRouter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import io.jooby.Context;
44
import io.jooby.Route;
5+
import io.jooby.annotations.DELETE;
56
import io.jooby.annotations.GET;
67
import io.jooby.annotations.POST;
78
import io.jooby.annotations.Path;
9+
import org.slf4j.LoggerFactory;
810

911
import static org.junit.jupiter.api.Assertions.assertEquals;
1012

@@ -25,7 +27,7 @@ public String subpath() {
2527
return "OK";
2628
}
2729

28-
@GET
30+
@DELETE
2931
@Path("/void")
3032
public void noContent() {
3133

@@ -34,6 +36,7 @@ public void noContent() {
3436
@GET
3537
@Path("/voidwriter")
3638
public void writer(Context ctx) throws Exception {
39+
LoggerFactory.getLogger(getClass()).info("blocking");
3740
ctx.responseWriter(writer -> {
3841
writer.println("writer");
3942
});

tests/src/test/java/io/jooby/MvcTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.jooby.json.JacksonModule;
1515
import io.jooby.junit.ServerTest;
1616
import io.jooby.junit.ServerTestRunner;
17+
import io.jooby.netty.Netty;
1718
import okhttp3.FormBody;
1819
import okhttp3.MediaType;
1920
import okhttp3.MultipartBody;
@@ -51,7 +52,7 @@ public void routerInstance(ServerTestRunner runner) {
5152
assertEquals("OK", rsp.body().string());
5253
});
5354

54-
client.get("/void", rsp -> {
55+
client.delete("/void", rsp -> {
5556
assertEquals("", rsp.body().string());
5657
assertEquals(204, rsp.code());
5758
});
@@ -81,7 +82,7 @@ public void routerImporting(ServerTestRunner runner) {
8182
assertEquals("OK", rsp.body().string());
8283
});
8384

84-
client.get("/sub/void", rsp -> {
85+
client.delete("/sub/void", rsp -> {
8586
assertEquals("", rsp.body().string());
8687
assertEquals(204, rsp.code());
8788
});

0 commit comments

Comments
 (0)