Skip to content

Commit f976899

Browse files
committed
Parameter type info missing depending on code style (ApiTool/Swagger) fix jooby-project#987
1 parent 72c5a87 commit f976899

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed

modules/jooby-apitool/src/main/java/org/jooby/internal/apitool/BytecodeRouteParser.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,13 +985,22 @@ private java.lang.reflect.Type parameterType(final ClassLoader loader, final Abs
985985
// mv.visitLdcInsn("req.param(\"p1\")");
986986
// mv.visitMethodInsn(INVOKESTATIC, "kotlin/jvm/internal/Intrinsics", "checkExpressionValueIsNotNull", "(Ljava/lang/Object;Ljava/lang/String;)V", false);
987987
// mv.visitMethodInsn(INVOKESTATIC, "org/jooby/JoobyKt", "getValue", "(Lorg/jooby/Mutant;)Ljava/lang/String;", false);
988-
AbstractInsnNode methodInsnNode = new Insn<>(null, n)
988+
AbstractInsnNode next = new Insn<>(null, n)
989989
.next()
990990
.filter(MethodInsnNode.class::isInstance)
991991
.skip(1)
992992
.findFirst()
993993
.orElse(null);
994-
return parameterType(loader, methodInsnNode);
994+
java.lang.reflect.Type result = parameterType(loader, next);
995+
if (result == Object.class) {
996+
next = new Insn<>(null, n)
997+
.next()
998+
.filter(TypeInsnNode.class::isInstance)
999+
.findFirst()
1000+
.orElse(null);
1001+
result = parameterType(loader, next);
1002+
}
1003+
return result;
9951004
}
9961005
return Object.class;
9971006
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package issues
2+
3+
import com.typesafe.config.Config
4+
import org.jooby.Err
5+
import org.jooby.Kooby
6+
7+
data class LoginRequest(val username: String)
8+
9+
class App987 : Kooby({
10+
/**
11+
Authenticates a user, generating an Authorization token.
12+
@param body contains the user's credentials.
13+
@return contains the apiToken
14+
*/
15+
post("/api/login") { req ->
16+
val params: LoginRequest
17+
try {
18+
params = req.body(LoginRequest::class.java)
19+
} catch (e: Exception) {
20+
throw Err(401, "Could not parse request: ${e.message}")
21+
}
22+
params.username
23+
}
24+
25+
get("/use/config") {
26+
val conf = require(Config::class.java)
27+
val useName = conf.getBoolean("name")
28+
val params: LoginRequest
29+
try {
30+
params = body(LoginRequest::class.java)
31+
} catch (e: Exception) {
32+
throw Err(401, "Could not parse request: ${e.message} ${useName}")
33+
}
34+
params.username
35+
}
36+
37+
})
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package issues;
2+
3+
import org.jooby.apitool.ApiParser;
4+
import org.jooby.apitool.RouteMethodAssert;
5+
import org.jooby.apitool.RouteParameter;
6+
import org.junit.Test;
7+
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
11+
public class Issue987 {
12+
13+
@Test
14+
public void shouldNotLostBodyParameterType() throws Exception {
15+
new RouteMethodAssert(new ApiParser(dir()).parseFully(new App987()))
16+
.next(r -> {
17+
r.returnType(String.class);
18+
r.pattern("/api/login");
19+
r.description("Authenticates a user, generating an Authorization token.");
20+
r.summary(null);
21+
r.param(p -> {
22+
p.name("body")
23+
.type(LoginRequest.class)
24+
.kind(RouteParameter.Kind.BODY);
25+
});
26+
})
27+
.next(r -> {
28+
r.returnType(String.class);
29+
r.pattern("/use/config");
30+
r.description(null);
31+
r.summary(null);
32+
r.param(p -> {
33+
p.name("body")
34+
.type(LoginRequest.class)
35+
.kind(RouteParameter.Kind.BODY);
36+
});
37+
}).done();
38+
}
39+
40+
private Path dir() {
41+
Path userdir = Paths.get(System.getProperty("user.dir"));
42+
if (!userdir.toString().endsWith("jooby-apitool")) {
43+
userdir = userdir.resolve("modules").resolve("jooby-apitool");
44+
}
45+
return userdir;
46+
}
47+
}

modules/jooby-apitool/src/test/java/org/jooby/apitool/RouteMethodAssert.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.util.Map;
1212
import java.util.function.Consumer;
1313

14-
class RouteMethodAssert {
14+
public class RouteMethodAssert {
1515

1616
public static class RouteParameterMatcher {
1717
private final Iterator<RouteParameter> parameters;

0 commit comments

Comments
 (0)