Skip to content

Commit e53dd2f

Browse files
committed
open-api: parse javadoc #3729
- parse @server
1 parent 0b2d4d2 commit e53dd2f

8 files changed

Lines changed: 56 additions & 8 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
1818
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
1919
import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
20+
import io.swagger.v3.oas.models.servers.Server;
2021

2122
public class ClassDoc extends JavaDocNode {
2223
private final Map<String, FieldDoc> fields = new LinkedHashMap<>();
2324
private final Map<String, MethodDoc> methods = new LinkedHashMap<>();
25+
private final List<Server> servers;
2426

2527
public ClassDoc(JavaDocParser ctx, DetailAST node, DetailAST javaDoc) {
2628
super(ctx, node, javaDoc);
@@ -29,6 +31,11 @@ public ClassDoc(JavaDocParser ctx, DetailAST node, DetailAST javaDoc) {
2931
} else if (isEnum()) {
3032
defaultEnumMembers();
3133
}
34+
this.servers = JavaDocTag.servers(this.javadoc);
35+
}
36+
37+
public List<Server> getServers() {
38+
return servers;
3239
}
3340

3441
public String getVersion() {

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,51 @@
1717
import io.jooby.SneakyThrows.Consumer2;
1818
import io.jooby.SneakyThrows.Consumer3;
1919
import io.jooby.StatusCode;
20+
import io.swagger.v3.oas.models.servers.Server;
2021

2122
public class JavaDocTag {
2223
private static final Predicate<DetailNode> CUSTOM_TAG =
2324
javadocToken(JavadocTokenTypes.CUSTOM_NAME);
2425
private static final Predicate<DetailNode> TAG =
2526
CUSTOM_TAG.and(it -> it.getText().equals("@tag"));
27+
private static final Predicate<DetailNode> SERVER =
28+
CUSTOM_TAG.and(it -> it.getText().startsWith("@server."));
2629
private static final Predicate<DetailNode> EXTENSION =
2730
CUSTOM_TAG.and(it -> it.getText().startsWith("@x-"));
2831
private static final Predicate<DetailNode> THROWS =
2932
it -> tree(it).anyMatch(javadocToken(JavadocTokenTypes.THROWS_LITERAL));
3033

34+
@SuppressWarnings("unchecked")
35+
public static List<Server> servers(DetailNode node) {
36+
var values = new ArrayList<String>();
37+
javaDocTag(
38+
node,
39+
SERVER,
40+
(tag, value) -> {
41+
values.add(tag.getText().substring(1));
42+
values.add(value);
43+
});
44+
List<Server> result = new ArrayList<>();
45+
if (!values.isEmpty()) {
46+
var serverMap = ExtensionJavaDocParser.parse(values);
47+
var servers = serverMap.get("server");
48+
if (!(servers instanceof List<?>)) {
49+
servers = List.of(servers);
50+
}
51+
((List) servers)
52+
.forEach(
53+
it -> {
54+
if (it instanceof Map<?, ?> hash) {
55+
var server = new Server();
56+
server.setDescription((String) hash.get("description"));
57+
server.setUrl((String) hash.get("url"));
58+
result.add(server);
59+
}
60+
});
61+
}
62+
return result;
63+
}
64+
3165
public static Map<StatusCode, ThrowsDoc> throwList(DetailNode node) {
3266
var result = new LinkedHashMap<StatusCode, ThrowsDoc>();
3367
javaDocTag(

modules/jooby-openapi/src/main/java/io/jooby/openapi/OpenAPIGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ public String toString(OpenAPIGenerator tool, OpenAPI result) {
167167
if (!doc.getExtensions().isEmpty()) {
168168
info.setExtensions(doc.getExtensions());
169169
}
170+
doc.getServers().forEach(openapi::addServersItem);
170171
});
171172
}
172173

modules/jooby-openapi/src/test/java/issues/i3729/api/ApiDocTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public void shouldGenerateDoc(OpenAPIResult result) {
2323
+ " x-logo:\n"
2424
+ " url: https://redocly.github.io/redoc/museum-logo.png\n"
2525
+ " altText: Museum logo\n"
26+
+ "servers:\n"
27+
+ "- url: https://api.fake-museum-example.com/v1\n"
2628
+ "tags:\n"
2729
+ "- name: Library\n"
2830
+ " description: Access to all books.\n"

modules/jooby-openapi/src/test/java/issues/i3729/api/AppLibrary.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* <p>Available data: Books and authors.
1616
*
1717
* @version 4.0.0
18+
* @server.url https://api.fake-museum-example.com/v1
1819
* @x-logo.url https://redocly.github.io/redoc/museum-logo.png
1920
* @x-logo.altText Museum logo
2021
*/

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ public void apiDoc() throws Exception {
3737
+ " ante id vestibulum congue. Nam et tortor at magna tempor congue.",
3838
doc.getDescription());
3939

40+
var servers = doc.getServers();
41+
assertEquals(2, servers.size());
42+
assertEquals("https://api.example.com/v1", servers.get(0).getUrl());
43+
assertEquals("Production server (uses live data)", servers.get(0).getDescription());
44+
assertEquals("https://sandbox-api.example.com:8443/v1", servers.get(1).getUrl());
45+
assertEquals("Sandbox server (uses test data)", servers.get(1).getDescription());
46+
4047
withMethod(
4148
doc,
4249
"hello",

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

Lines changed: 0 additions & 8 deletions
This file was deleted.

modules/jooby-openapi/src/test/java/javadoc/input/ApiDoc.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
* @x-badges.position before
2323
* @x-badges.color purple
2424
* @tag ApiTag
25+
* @server.url https://api.example.com/v1
26+
* @server.description Production server (uses live data)
27+
* @server.url https://sandbox-api.example.com:8443/v1
28+
* @server.description Sandbox server (uses test data)
2529
*/
2630
@Path("/api")
2731
public class ApiDoc {

0 commit comments

Comments
 (0)