Skip to content

Commit cc3f76e

Browse files
committed
WIP: openapi: javadoc for script/lambda routes
- WIP
1 parent f9104d3 commit cc3f76e

5 files changed

Lines changed: 119 additions & 5 deletions

File tree

modules/jooby-openapi/src/main/java/io/jooby/internal/openapi/javadoc/ClassDoc.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
public class ClassDoc extends JavaDocNode {
2525
private final Map<String, FieldDoc> fields = new LinkedHashMap<>();
2626
private final Map<String, MethodDoc> methods = new LinkedHashMap<>();
27+
private final Map<String, MethodDoc> scripts = new LinkedHashMap<>();
2728
private final List<Server> servers;
2829
private final List<Contact> contact;
2930
private final List<License> license;
@@ -160,6 +161,10 @@ public void addMethod(MethodDoc method) {
160161
this.methods.put(toMethodSignature(method), method);
161162
}
162163

164+
public void addScript(String pattern, MethodDoc method) {
165+
this.scripts.put(pattern, method);
166+
}
167+
163168
public void addField(FieldDoc field) {
164169
this.fields.put(field.getName(), field);
165170
}
@@ -172,6 +177,10 @@ public Optional<MethodDoc> getMethod(String name, List<String> parameterNames) {
172177
return Optional.ofNullable(methods.get(toMethodSignature(name, parameterNames)));
173178
}
174179

180+
public Optional<MethodDoc> getScript(String pattern) {
181+
return Optional.ofNullable(scripts.get(pattern));
182+
}
183+
175184
private String toMethodSignature(MethodDoc method) {
176185
return toMethodSignature(method.getName(), method.getParameterNames());
177186
}

modules/jooby-openapi/src/main/java/io/jooby/internal/openapi/javadoc/JavaDocParser.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.puppycrawl.tools.checkstyle.JavaParser;
2323
import com.puppycrawl.tools.checkstyle.api.DetailAST;
2424
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
25+
import com.puppycrawl.tools.checkstyle.utils.XpathUtil;
26+
import io.jooby.Router;
2527

2628
public class JavaDocParser {
2729

@@ -57,6 +59,7 @@ public Map<String, ClassDoc> traverse(DetailAST tree) {
5759
counter.addAndGet(comment == JavaDocNode.EMPTY_AST ? 0 : 1);
5860
var classDoc = new ClassDoc(this, scope, comment);
5961

62+
// MVC routes
6063
traverse(
6164
scope,
6265
tokens(TokenTypes.VARIABLE_DEF, TokenTypes.METHOD_DEF),
@@ -72,10 +75,37 @@ public Map<String, ClassDoc> traverse(DetailAST tree) {
7275
}
7376
}
7477
});
75-
76-
if (classDoc.isRecord()) {
77-
// complement with record parameter
78+
// Script routes
79+
for (var script :
80+
tree(scope)
81+
.filter(tokens(TokenTypes.METHOD_CALL))
82+
// Test for HTTP method name
83+
.filter(
84+
it ->
85+
tree(it)
86+
.filter(tokens(TokenTypes.IDENT))
87+
.anyMatch(e -> Router.METHODS.contains(e.getText().toUpperCase())))
88+
.toList()) {
89+
var scriptComment =
90+
children(script)
91+
.filter(tokens(TokenTypes.BLOCK_COMMENT_BEGIN))
92+
.findFirst()
93+
.orElse(null);
94+
if (scriptComment != null) {
95+
// ELIST -> EXPR -> STRING_LITERAL
96+
children(script)
97+
.filter(tokens(TokenTypes.ELIST))
98+
.findFirst()
99+
.flatMap(it -> children(it).filter(tokens(TokenTypes.EXPR)).findFirst())
100+
.flatMap(it -> children(it).filter(tokens(TokenTypes.STRING_LITERAL)).findFirst())
101+
.map(XpathUtil::getTextAttributeValue)
102+
.ifPresent(
103+
pattern -> {
104+
classDoc.addScript(pattern, new MethodDoc(this, script, scriptComment));
105+
});
106+
}
78107
}
108+
79109
if (counter.get() > 0) {
80110
classes.put(classDoc.getName(), classDoc);
81111
}

modules/jooby-openapi/src/test/java/javadoc/JavaDocParserTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,37 @@
2424

2525
public class JavaDocParserTest {
2626

27+
@Test
28+
public void scriptDoc() throws Exception {
29+
withDoc(
30+
javadoc.input.ScriptApp.class,
31+
doc -> {
32+
assertEquals("ScriptApp", doc.getSimpleName());
33+
assertEquals("javadoc.input.ScriptApp", doc.getName());
34+
assertEquals("Script App.", doc.getSummary());
35+
assertEquals("Some description.", doc.getDescription());
36+
37+
withScript(
38+
doc,
39+
"/static",
40+
method -> {
41+
assertEquals("This is a static path.", method.getSummary());
42+
assertEquals("No parameters", method.getDescription());
43+
assertEquals("Request Path.", method.getReturnDoc());
44+
});
45+
46+
withScript(
47+
doc,
48+
"/path/{id}",
49+
method -> {
50+
assertEquals("Path param.", method.getSummary());
51+
assertNull(method.getDescription());
52+
assertEquals("Some value.", method.getReturnDoc());
53+
assertEquals("Path ID.", method.getParameterDoc("id"));
54+
});
55+
});
56+
}
57+
2758
@Test
2859
public void apiDoc() throws Exception {
2960
withDoc(
@@ -258,6 +289,12 @@ private void withMethod(
258289
consumer.accept(method.get());
259290
}
260291

292+
private void withScript(ClassDoc doc, String pattern, Consumer<MethodDoc> consumer) {
293+
var method = doc.getScript(pattern);
294+
assertTrue(method.isPresent());
295+
consumer.accept(method.get());
296+
}
297+
261298
private void withField(ClassDoc doc, String name, Consumer<FieldDoc> consumer) {
262299
var method = doc.getField(name);
263300
assertTrue(method.isPresent());

modules/jooby-openapi/src/test/java/javadoc/PrintAstTree.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.io.IOException;
1010
import java.nio.file.Path;
1111
import java.nio.file.Paths;
12-
import javadoc.input.ApiDoc;
12+
import javadoc.input.ScriptApp;
1313

1414
import com.puppycrawl.tools.checkstyle.AstTreeStringPrinter;
1515
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
@@ -25,7 +25,7 @@ public static void main(String[] args) throws CheckstyleException, IOException {
2525
.resolve("java");
2626
var stringAst =
2727
AstTreeStringPrinter.printJavaAndJavadocTree(
28-
baseDir.resolve(toPath(ApiDoc.class)).toFile());
28+
baseDir.resolve(toPath(ScriptApp.class)).toFile());
2929
System.out.println(stringAst);
3030
}
3131

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package javadoc.input;
7+
8+
import io.jooby.Context;
9+
import io.jooby.Jooby;
10+
11+
/** Script App. Some description. */
12+
public class ScriptApp extends Jooby {
13+
{
14+
// Ignored
15+
/*
16+
* This is a static path. No parameters
17+
*
18+
* @return Request Path.
19+
*/
20+
get(
21+
"/static",
22+
ctx -> {
23+
// Ignored.
24+
return ctx.getRequestPath();
25+
// Ignored
26+
});
27+
28+
// Ignored
29+
/*
30+
* Path param.
31+
*
32+
* @param id Path ID.
33+
* @return Some value.
34+
*/
35+
// Ignored
36+
get("/path/{id}", Context::getRequestPath);
37+
}
38+
}

0 commit comments

Comments
 (0)