Skip to content

Commit

Permalink
Use ClassPathUtils from smallrye-common, consolidate static model method
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <michael@xlate.io>
  • Loading branch information
MikeEdgar committed Oct 30, 2024
1 parent 8df8ce7 commit 6b29101
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 62 deletions.
4 changes: 4 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
<groupId>io.smallrye</groupId>
<artifactId>jandex</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.common</groupId>
<artifactId>smallrye-common-classloader</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
Expand Down
46 changes: 23 additions & 23 deletions core/src/main/java/io/smallrye/openapi/api/SmallRyeOpenAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.jboss.jandex.Indexer;
import org.jboss.jandex.Type;

import io.smallrye.common.classloader.ClassPathUtils;
import io.smallrye.openapi.api.util.MergeUtil;
import io.smallrye.openapi.runtime.OpenApiProcessor;
import io.smallrye.openapi.runtime.OpenApiRuntimeException;
Expand Down Expand Up @@ -563,34 +564,33 @@ protected <V, A extends V, O extends V, AB, OB> void buildStaticModel(BuildConte
return ctx.appClassLoader.getResource(loadPath.toString());
});

ctx.staticModel = OpenApiProcessor.loadOpenApiStaticFiles(loadFn)
.stream()
.map(file -> {
try (Reader reader = new InputStreamReader(file.getContent())) {
V dom = ctx.modelIO.jsonIO().fromReader(reader, file.getFormat());
OpenAPI fileModel = ctx.modelIO.readValue(dom);
debugModel("static file", fileModel);
return fileModel;
} catch (IOException e) {
throw new OpenApiRuntimeException("IOException reading " + file.getFormat() + " static file",
e);
}
})
.reduce(MergeUtil::merge)
.orElse(null);
for (URL staticFile : OpenApiProcessor.locateStaticFiles(loadFn)) {
String source = "static file " + staticFile;
try {
ClassPathUtils.consumeStream(staticFile, stream -> addStaticModel(ctx, stream, source));
} catch (IOException e) {
String msg = "IOException reading " + source;
throw new OpenApiRuntimeException(msg, e);
}
}
}

InputStream customFile = customStaticFile.get();

if (customFile != null) {
try (Reader reader = new InputStreamReader(customFile)) {
V dom = ctx.modelIO.jsonIO().fromReader(reader);
OpenAPI customStaticModel = ctx.modelIO.readValue(dom);
debugModel("static file", customStaticModel);
ctx.staticModel = MergeUtil.merge(customStaticModel, ctx.staticModel);
} catch (IOException e) {
throw new OpenApiRuntimeException("IOException reading custom static file", e);
}
addStaticModel(ctx, customFile, "custom static file");
}
}

private <V, A extends V, O extends V, AB, OB> void addStaticModel(BuildContext<V, A, O, AB, OB> ctx, InputStream stream, String source) {
try (Reader reader = new InputStreamReader(stream)) {
V dom = ctx.modelIO.jsonIO().fromReader(reader);
OpenAPI fileModel = ctx.modelIO.readValue(dom);
debugModel(source, fileModel);
ctx.staticModel = MergeUtil.merge(fileModel, ctx.staticModel);
} catch (IOException e) {
String msg = "IOException reading " + source;
throw new OpenApiRuntimeException(msg, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package io.smallrye.openapi.runtime;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.reflect.Constructor;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -292,49 +287,35 @@ private static <T> T uncheckedCall(Callable<T> callable) {
}
}

public static List<OpenApiStaticFile> loadOpenApiStaticFiles(Function<String, URL> loadFunction) {
List<OpenApiStaticFile> apiStaticFiles = new ArrayList<>();
public static List<URL> locateStaticFiles(Function<String, URL> loadFunction) {
List<URL> apiStaticFiles = new ArrayList<>();

loadOpenApiStaticFile(apiStaticFiles, loadFunction, "/META-INF/openapi.yaml", Format.YAML);
loadOpenApiStaticFile(apiStaticFiles, loadFunction, "/WEB-INF/classes/META-INF/openapi.yaml", Format.YAML);
loadOpenApiStaticFile(apiStaticFiles, loadFunction, "/META-INF/openapi.yml", Format.YAML);
loadOpenApiStaticFile(apiStaticFiles, loadFunction, "/WEB-INF/classes/META-INF/openapi.yml", Format.YAML);
loadOpenApiStaticFile(apiStaticFiles, loadFunction, "/META-INF/openapi.json", Format.JSON);
loadOpenApiStaticFile(apiStaticFiles, loadFunction, "/WEB-INF/classes/META-INF/openapi.json", Format.JSON);
locateStaticFile(apiStaticFiles, loadFunction, "/META-INF/openapi.yaml");
locateStaticFile(apiStaticFiles, loadFunction, "/WEB-INF/classes/META-INF/openapi.yaml");
locateStaticFile(apiStaticFiles, loadFunction, "/META-INF/openapi.yml");
locateStaticFile(apiStaticFiles, loadFunction, "/WEB-INF/classes/META-INF/openapi.yml");
locateStaticFile(apiStaticFiles, loadFunction, "/META-INF/openapi.json");
locateStaticFile(apiStaticFiles, loadFunction, "/WEB-INF/classes/META-INF/openapi.json");

return apiStaticFiles;
}

private static List<OpenApiStaticFile> loadOpenApiStaticFile(List<OpenApiStaticFile> apiStaticFiles,
Function<String, URL> loadFunction, String path, Format format) {
public static List<OpenApiStaticFile> loadOpenApiStaticFiles(Function<String, URL> loadFunction) {
List<OpenApiStaticFile> apiStaticFiles = new ArrayList<>();

Optional.ofNullable(loadFunction.apply(path))
.map(locator -> {
try {
return new OpenApiStaticFile(locator, openStream(locator), format);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
})
.ifPresent(apiStaticFiles::add);
locateStaticFiles(loadFunction).forEach(locator -> {
try {
Format format = locator.toString().endsWith(".json") ? Format.JSON : Format.YAML;
apiStaticFiles.add(new OpenApiStaticFile(locator, locator.openStream(), format));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});

return apiStaticFiles;
}

private static InputStream openStream(URL url) throws IOException {
if ("jar".equals(url.getProtocol())) {
URLConnection urlConnection = url.openConnection();
// prevent locking the jar after the inputstream is closed
urlConnection.setUseCaches(false);
return urlConnection.getInputStream();
}
if ("file".equals(url.getProtocol())) {
try {
return Files.newInputStream(Path.of(url.toURI()));
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Failed to translate " + url + " to local path", e);
}
}
return url.openStream();
private static void locateStaticFile(List<URL> apiStaticFiles, Function<String, URL> loadFunction, String path) {
Optional.ofNullable(loadFunction.apply(path)).ifPresent(apiStaticFiles::add);
}
}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
<artifactId>jandex</artifactId>
<version>${version.io.smallrye.jandex}</version>
</dependency>
<dependency>
<groupId>io.smallrye.common</groupId>
<artifactId>smallrye-common-classloader</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config</artifactId>
Expand Down

0 comments on commit 6b29101

Please sign in to comment.