Skip to content

Commit bdb7814

Browse files
committed
OpenAPI: checkstyle + modify output of OpenAPI files
1 parent 6b9452d commit bdb7814

8 files changed

Lines changed: 56 additions & 18 deletions

File tree

jooby/src/main/java/io/jooby/AssetSource.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.Arrays;
2020
import java.util.List;
2121
import java.util.Properties;
22-
import java.util.stream.Stream;
2322

2423
/**
2524
* An asset source is a collection or provider of {@link Asset}. There are two implementations:
@@ -51,6 +50,24 @@ public interface AssetSource {
5150
return new ClassPathAssetSource(loader, location);
5251
}
5352

53+
/**
54+
* Creates a webjar asset source. Usage:
55+
*
56+
* <ul>
57+
* <li>Add a webjar to your project, for example swagger-ui</li>
58+
* <li>Create and add an asset handler:
59+
* <pre>{@code
60+
*
61+
* asset("/path/*", AssetSource.webjar(getClassLoader(), "swagger-ui"));
62+
*
63+
* }</pre>
64+
* </li>
65+
* </ul>
66+
*
67+
* @param loader Class loader.
68+
* @param name Web asset name.
69+
* @return A webjar source.
70+
*/
5471
static @Nonnull AssetSource webjars(@Nonnull ClassLoader loader, @Nonnull String name) {
5572
List<String> location = Arrays.asList(
5673
"/META-INF/maven/org.webjars/" + name + "/pom.properties",

jooby/src/main/java/io/jooby/OpenAPIModule.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ public OpenAPIModule format(Format... format) {
5858
.orElse("/")
5959
.replace(".", "/");
6060

61+
String appname = application.getClass().getSimpleName()
62+
.replace("Jooby", "openapi")
63+
.replace("Kooby", "openapi");
6164
for (Format ext : format) {
62-
String filename = "/openapi." + ext.name().toLowerCase();
65+
String filename = String.format("/%s.%s", appname, ext.name().toLowerCase());
6366
String openAPIFileLocation = Router.normalizePath(dir) + filename;
64-
application.assets(fullPath(openAPIPath, filename), openAPIFileLocation);
67+
application.assets(fullPath(openAPIPath, "/openapi." + ext.name().toLowerCase()),
68+
openAPIFileLocation);
6569
}
6670

6771
/** Configure UI: */

modules/jooby-bom/pom.xml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<!-- THIS FILE IS AUTO GENERATED. DON'T EDIT -->
1717

1818
<properties>
19-
<jooby.version>2.6.2</jooby.version>
19+
<jooby.version>2.7.0-SNAPSHOT</jooby.version>
2020
<HikariCP.version>3.4.2</HikariCP.version>
2121
<archetype-packaging.version>3.1.2</archetype-packaging.version>
2222
<asm.version>7.3.1</asm.version>
@@ -52,8 +52,8 @@
5252
<jetty.version>9.4.27.v20200227</jetty.version>
5353
<jfiglet.version>0.0.8</jfiglet.version>
5454
<jmespath-java.version>1.11.743</jmespath-java.version>
55-
<jooby-maven-plugin.version>2.6.2</jooby-maven-plugin.version>
56-
<jooby.version>2.6.2</jooby.version>
55+
<jooby-maven-plugin.version>2.7.0-SNAPSHOT</jooby-maven-plugin.version>
56+
<jooby.version>2.7.0-SNAPSHOT</jooby.version>
5757
<json.version>20190722</json.version>
5858
<jsonwebtoken.version>0.11.0</jsonwebtoken.version>
5959
<jsr305.version>3.0.2</jsr305.version>
@@ -490,12 +490,6 @@
490490
<version>${swagger-ui.version}</version>
491491
<type>jar</type>
492492
</dependency>
493-
<dependency>
494-
<groupId>org.webjars.npm</groupId>
495-
<artifactId>redoc</artifactId>
496-
<version>${redoc.version}</version>
497-
<type>jar</type>
498-
</dependency>
499493
<dependency>
500494
<groupId>org.unbescape</groupId>
501495
<artifactId>unbescape</artifactId>

modules/jooby-openapi/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>io.jooby</groupId>
88
<artifactId>modules</artifactId>
9-
<version>2.6.1-SNAPSHOT</version>
9+
<version>2.7.0-SNAPSHOT</version>
1010
</parent>
1111

1212
<modelVersion>4.0.0</modelVersion>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class OpenAPIExt extends OpenAPI {
2121
@JsonIgnore
2222
private List<OperationExt> operations = Collections.emptyList();
2323

24+
@JsonIgnore
25+
private String source;
26+
2427
public static OpenAPIExt create(Path basedir, ClassLoader classLoader, String templateName) {
2528
try {
2629
Path path = basedir.resolve("conf").resolve(templateName);
@@ -44,4 +47,12 @@ public List<OperationExt> getOperations() {
4447
public void setOperations(List<OperationExt> operations) {
4548
this.operations = operations;
4649
}
50+
51+
public String getSource() {
52+
return source;
53+
}
54+
55+
public void setSource(String classname) {
56+
this.source = classname;
57+
}
4758
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,21 @@ public static List<Format> parse(String value) {
8989
private String excludes;
9090

9191
public void export(OpenAPI openAPI, Format format) throws IOException {
92-
if (!Files.exists(outputDir)) {
93-
Files.createDirectories(outputDir);
92+
Path output;
93+
if (openAPI instanceof OpenAPIExt) {
94+
String source = ((OpenAPIExt) openAPI).getSource();
95+
String[] names = source.split("\\.");
96+
output = Stream.of(names).limit(Math.max(0, names.length - 2))
97+
.reduce(outputDir, Path::resolve, Path::resolve);
98+
output = output.resolve(names[names.length - 1] + "." + format.extension());
99+
} else {
100+
output = outputDir.resolve("openapi." + format.extension());
94101
}
95-
Path output = outputDir.resolve("openapi" + "." + format.extension());
102+
103+
if (!Files.exists(output.getParent())) {
104+
Files.createDirectories(output.getParent());
105+
}
106+
96107
log.info(" writing: " + output);
97108

98109
String content = format.toString(this, openAPI);
@@ -112,6 +123,7 @@ public OpenAPI generate(String classname) {
112123

113124
/** Create OpenAPI from template and make sure min required information is present: */
114125
OpenAPIExt openapi = OpenAPIExt.create(basedir, classLoader, templateName);
126+
openapi.setSource(classname);
115127

116128
defaults(classname, contextPath, openapi);
117129

modules/jooby-redoc/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>io.jooby</groupId>
88
<artifactId>modules</artifactId>
9-
<version>2.6.1-SNAPSHOT</version>
9+
<version>2.7.0-SNAPSHOT</version>
1010
</parent>
1111

1212
<modelVersion>4.0.0</modelVersion>

modules/jooby-swagger-ui/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>io.jooby</groupId>
88
<artifactId>modules</artifactId>
9-
<version>2.6.1-SNAPSHOT</version>
9+
<version>2.7.0-SNAPSHOT</version>
1010
</parent>
1111

1212
<modelVersion>4.0.0</modelVersion>

0 commit comments

Comments
 (0)