Skip to content

Commit 692b342

Browse files
committed
OpenAPI: upgrade swagger-ui and redoc
1 parent 0ff4b8f commit 692b342

File tree

6 files changed

+102
-29
lines changed

6 files changed

+102
-29
lines changed

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

Lines changed: 87 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
*/
66
package io.jooby;
77

8-
import io.jooby.SneakyThrows.Consumer2;
9-
import org.apache.commons.io.IOUtils;
10-
11-
import javax.annotation.Nonnull;
12-
import java.io.IOException;
8+
import java.io.ByteArrayInputStream;
139
import java.io.InputStream;
1410
import java.nio.charset.StandardCharsets;
1511
import java.util.Arrays;
@@ -18,6 +14,14 @@
1814
import java.util.Map;
1915
import java.util.Optional;
2016

17+
import javax.annotation.Nonnull;
18+
19+
import org.apache.commons.io.IOUtils;
20+
import org.jetbrains.annotations.NotNull;
21+
import org.jetbrains.annotations.Nullable;
22+
23+
import io.jooby.SneakyThrows.Consumer2;
24+
2125
/**
2226
* OpenAPI supports for Jooby. Basic Usage:
2327
*
@@ -40,6 +44,59 @@
4044
*/
4145
public class OpenAPIModule implements Extension {
4246

47+
private static class OpenAPIAsset implements Asset {
48+
49+
private long lastModified;
50+
51+
private byte[] content;
52+
53+
private MediaType type;
54+
55+
OpenAPIAsset(MediaType type, byte[] content, long lastModified) {
56+
this.content = content;
57+
this.type = type;
58+
this.lastModified = lastModified;
59+
}
60+
61+
@Override public long getSize() {
62+
return content.length;
63+
}
64+
65+
@Override public long getLastModified() {
66+
return lastModified;
67+
}
68+
69+
@Override public boolean isDirectory() {
70+
return false;
71+
}
72+
73+
@NotNull @Override public MediaType getContentType() {
74+
return type;
75+
}
76+
77+
@Override public InputStream stream() {
78+
return new ByteArrayInputStream(content);
79+
}
80+
81+
@Override public void close() throws Exception {
82+
// NOOP
83+
}
84+
}
85+
86+
private static class OpenAPISource implements AssetSource {
87+
88+
private Map<String, Asset> assets = new HashMap<>();
89+
90+
public OpenAPISource put(String key, Asset asset) {
91+
assets.put(key, asset);
92+
return this;
93+
}
94+
95+
@Nullable @Override public Asset resolve(@NotNull String path) {
96+
return assets.get(path);
97+
}
98+
}
99+
43100
/**
44101
* Available formats.
45102
*/
@@ -156,33 +213,41 @@ private void configureUI(Jooby application) {
156213
}
157214
}
158215

159-
private void redoc(Jooby application, AssetSource source) throws IOException {
160-
application.assets(redocPath + "/*", source);
216+
private void redoc(Jooby application, AssetSource source) throws Exception {
217+
161218
String openAPIJSON = fullPath(
162219
fullPath(application.getContextPath(), openAPIPath), "/openapi.json");
163220

164-
String template = readString(source, "index.html")
165-
.replace("${openAPIPath}", openAPIJSON)
166-
.replace("${redocPath}", fullPath(application.getContextPath(), redocPath));
167-
application
168-
.get(redocPath, ctx -> ctx.setResponseType(MediaType.html).send(template));
221+
AssetSource customSource = new OpenAPISource()
222+
.put("index.html",
223+
processAsset(source, MediaType.html, "index.html", "${openAPIPath}", openAPIJSON,
224+
"${redocPath}", fullPath(application.getContextPath(), redocPath)));
225+
226+
application.assets(redocPath + "/?*", customSource, source);
169227
}
170228

171-
private void swaggerUI(Jooby application, AssetSource source) throws IOException {
229+
private void swaggerUI(Jooby application, AssetSource source) throws Exception {
172230
String openAPIJSON = fullPath(
173231
fullPath(application.getContextPath(), openAPIPath), "/openapi.json");
174232

175-
String template = readString(source, "index.html")
176-
.replace("${openAPIPath}", openAPIJSON)
177-
.replace("${swaggerPath}", fullPath(application.getContextPath(), swaggerUIPath));
178-
179-
application.assets(swaggerUIPath + "/*", source);
180-
application.get(swaggerUIPath, ctx -> ctx.setResponseType(MediaType.html).send(template));
233+
AssetSource customSource = new OpenAPISource()
234+
.put("index.html", processAsset(source, MediaType.html, "index.html", "${swaggerPath}",
235+
fullPath(application.getContextPath(), swaggerUIPath)))
236+
.put("swagger-initializer.js",
237+
processAsset(source, MediaType.html, "swagger-initializer.js", "${openAPIPath}",
238+
openAPIJSON));
239+
application.assets(swaggerUIPath + "/?*", customSource, source);
181240
}
182241

183-
private static String readString(AssetSource source, String resource) throws IOException {
184-
try (InputStream stream = source.resolve(resource).stream()) {
185-
return IOUtils.toString(stream, StandardCharsets.UTF_8);
242+
private static Asset processAsset(AssetSource source, MediaType type, String resource,
243+
String... replacements) throws Exception {
244+
try (Asset asset = source.resolve(resource)) {
245+
String content = IOUtils.toString(asset.stream(), StandardCharsets.UTF_8);
246+
for (int i = 0; i < replacements.length; i += 2) {
247+
content = content.replace(replacements[i], replacements[i + 1]);
248+
}
249+
return new OpenAPIAsset(type, content.getBytes(StandardCharsets.UTF_8),
250+
asset.getLastModified());
186251
}
187252
}
188253

modules/jooby-bom/pom.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@
4242
<gradle-plugins.version>0.9-rc-1</gradle-plugins.version>
4343
<gradle-tooling-api.version>2.6</gradle-tooling-api.version>
4444
<graphql-java.version>16.2</graphql-java.version>
45-
<gson.version>2.8.6</gson.version>
45+
<gson.version>2.9.0</gson.version>
4646
<guava.version>30.0-jre</guava.version>
4747
<guice.version>5.0.1</guice.version>
4848
<h2.version>2.1.210</h2.version>
4949
<handlebars.version>4.2.0</handlebars.version>
5050
<hibernate.version>5.6.3.Final</hibernate.version>
51-
<jackson.version>2.13.2.1</jackson.version>
51+
<jackson-databind.version>2.13.2.1</jackson-databind.version>
52+
<jackson.version>2.13.2</jackson.version>
5253
<jacoco-maven-plugin.version>0.8.7</jacoco-maven-plugin.version>
5354
<jacoco.version>0.8.7</jacoco.version>
5455
<jakarta.json.bind-api.version>1.0.2</jakarta.json.bind-api.version>
@@ -115,7 +116,7 @@
115116
<slf4j.version>1.7.32</slf4j.version>
116117
<spring.version>5.3.7</spring.version>
117118
<stork-maven-plugin.version>3.1.1</stork-maven-plugin.version>
118-
<swagger-parser.version>2.0.24</swagger-parser.version>
119+
<swagger-parser.version>2.0.30</swagger-parser.version>
119120
<swagger.version>2.1.13</swagger.version>
120121
<thymeleaf.version>3.0.12.RELEASE</thymeleaf.version>
121122
<truth.version>1.1.2</truth.version>
@@ -491,7 +492,7 @@
491492
<dependency>
492493
<groupId>com.fasterxml.jackson.core</groupId>
493494
<artifactId>jackson-databind</artifactId>
494-
<version>${jackson.version}</version>
495+
<version>${jackson-databind.version}</version>
495496
<type>jar</type>
496497
</dependency>
497498
<dependency>

modules/jooby-redoc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"private": true,
55
"license": "ASF",
66
"dependencies": {
7-
"redoc": "^2.0.0-rc.53"
7+
"redoc": "^2.0.0-rc.66"
88
}
99
}

modules/jooby-swagger-ui/build.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
<property name="swaggerDir" value="${buildDir}${file.separator}swagger-ui" />
77
<move file="node_modules${file.separator}swagger-ui-dist" tofile="${swaggerDir}"/>
88
<replace file="${swaggerDir}/index.html" token="./" value="${swaggerPath}/" />
9-
<replace file="${swaggerDir}/index.html" token="https://petstore.swagger.io/v2/swagger.json" value="${openAPIPath}" />
9+
<replace file="${swaggerDir}/index.html" token="index.css" value="${swaggerPath}/index.css" />
10+
<replace file="${swaggerDir}/swagger-initializer.js" token="https://petstore.swagger.io/v2/swagger.json" value="${openAPIPath}" />
1011
<delete>
1112
<fileset dir="${swaggerDir}" includes="package.json"/>
1213
<fileset dir="${swaggerDir}" includes="absolute-path.js"/>

modules/jooby-swagger-ui/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"private": true,
55
"license": "ASF",
66
"dependencies": {
7-
"swagger-ui-dist": "^4.5.2"
7+
"swagger-ui-dist": "^4.10.3"
88
}
99
}

modules/jooby-swagger-ui/src/test/java/io.jooby.swagger/SwaggerResourceTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public class SwaggerResourceTest {
1414
public void shouldCheckIndexPage() throws IOException {
1515
String index = asset("index.html");
1616
assertTrue(index.contains("${swaggerPath}"), index);
17+
assertTrue(index.contains("${swaggerPath}/index.css"), index);
18+
}
19+
20+
@Test
21+
public void shouldCheckSwaggerInitializer() throws IOException {
22+
String index = asset("swagger-initializer.js");
1723
assertTrue(index.contains("${openAPIPath}"), index);
1824
}
1925

0 commit comments

Comments
 (0)