Skip to content

Commit 4584424

Browse files
committed
OpenAPI: mvc routes argument and return types
1 parent f984fda commit 4584424

File tree

12 files changed

+693
-117
lines changed

12 files changed

+693
-117
lines changed

modules/jooby-openapi/pom.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@
2525
<version>${jooby.version}</version>
2626
</dependency>
2727

28-
<!-- https://mvnrepository.com/artifact/io.github.classgraph/classgraph -->
28+
<!-- JAXRS -->
2929
<dependency>
30-
<groupId>io.github.classgraph</groupId>
31-
<artifactId>classgraph</artifactId>
32-
<version>4.8.60</version>
30+
<groupId>jakarta.ws.rs</groupId>
31+
<artifactId>jakarta.ws.rs-api</artifactId>
3332
</dependency>
3433

3534
<!-- ASM -->
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package io.jooby.internal.openapi;
2+
3+
import org.objectweb.asm.Opcodes;
4+
import org.objectweb.asm.Type;
5+
import org.objectweb.asm.signature.SignatureReader;
6+
import org.objectweb.asm.signature.SignatureVisitor;
7+
8+
import java.util.ArrayList;
9+
import java.util.LinkedList;
10+
import java.util.List;
11+
import java.util.function.Predicate;
12+
import java.util.stream.Collectors;
13+
14+
public class ASMType {
15+
private static class TypeName {
16+
String name;
17+
String prefix;
18+
String suffix;
19+
List<TypeName> arguments = new ArrayList<>();
20+
21+
@Override public String toString() {
22+
StringBuilder buff = new StringBuilder();
23+
if (prefix != null) {
24+
buff.append(prefix);
25+
}
26+
buff.append(name.replace("/", "."));
27+
if (arguments.size() > 0) {
28+
String argstring = arguments.stream().map(TypeName::toString)
29+
.collect(Collectors.joining(",", "<", ">"));
30+
buff.append(argstring);
31+
}
32+
if (suffix != null) {
33+
buff.append(suffix);
34+
}
35+
return buff.toString();
36+
}
37+
}
38+
39+
public static String parse(String signature) {
40+
return parse(signature, type -> true);
41+
}
42+
43+
public static String parse(String signature, Predicate<String> filter) {
44+
String primitive = primitive(signature);
45+
if (primitive != null) {
46+
return primitive;
47+
}
48+
SignatureReader reader = new SignatureReader(signature);
49+
LinkedList<TypeName> stack = new LinkedList<>();
50+
SignatureVisitor visitor = new SignatureVisitor(Opcodes.ASM7) {
51+
@Override public void visitClassType(String name) {
52+
if (filter.test(name)) {
53+
if (stack.isEmpty()) {
54+
TypeName type = new TypeName();
55+
type.name = name;
56+
stack.push(type);
57+
} else {
58+
TypeName type = stack.peek();
59+
if (type.name == null) {
60+
type.name = name;
61+
} else {
62+
TypeName arg = new TypeName();
63+
arg.name = name;
64+
type.arguments.add(arg);
65+
stack.push(arg);
66+
}
67+
}
68+
}
69+
}
70+
71+
@Override public void visitEnd() {
72+
if (stack.size() > 1) {
73+
stack.pop();
74+
}
75+
}
76+
77+
@Override public void visitBaseType(char descriptor) {
78+
visitClassType(String.valueOf(descriptor));
79+
TypeName type = stack.peek();
80+
type.prefix = "[";
81+
type.suffix = null;
82+
}
83+
84+
@Override public SignatureVisitor visitArrayType() {
85+
TypeName type = new TypeName();
86+
type.prefix = "[L";
87+
type.suffix = ";";
88+
stack.push(type);
89+
return this;
90+
}
91+
};
92+
reader.accept(visitor);
93+
TypeName type = stack.pop();
94+
return type.toString();
95+
}
96+
97+
private static String primitive(String value) {
98+
switch (value) {
99+
case "Z": return "boolean";
100+
case "C": return "char";
101+
case "B": return "byte";
102+
case "S": return "short";
103+
case "I": return "int";
104+
case "J": return "long";
105+
case "F": return "float";
106+
case "D": return "double";
107+
}
108+
return null;
109+
}
110+
111+
}

modules/jooby-openapi/src/main/java/io/jooby/internal/openapi/ExecutionContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.jooby.internal.openapi;
22

3+
import org.objectweb.asm.AnnotationVisitor;
34
import org.objectweb.asm.ClassReader;
45
import org.objectweb.asm.ClassVisitor;
56
import org.objectweb.asm.Opcodes;
@@ -80,12 +81,14 @@ public void debugHandler(MethodNode node) {
8081
}
8182

8283
private void debug(MethodNode node) {
84+
System.out.println(Type.getReturnType(node.desc).getClassName() + " " + node.name + " {");
8385
Printer printer = new ASMifier();
8486
TraceMethodVisitor traceClassVisitor = new TraceMethodVisitor(null, printer);
8587
node.accept(traceClassVisitor);
8688
PrintWriter writer = new PrintWriter(System.out);
8789
printer.print(writer);
8890
writer.flush();
91+
System.out.println("}");
8992
}
9093

9194
public void debugHandlerLink(MethodNode node) {
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package io.jooby.internal.openapi;
22

33
public enum HttpType {
4+
CONTEXT,
5+
HEADER,
6+
COOKIE,
47
PATH,
58
QUERY,
6-
FORM;
9+
FORM,
10+
11+
BODY
712
}

0 commit comments

Comments
 (0)