Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid locking JARs on Windows when opening input streams #2049

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 24 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,34 @@ 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
Expand Up @@ -287,32 +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, locator.openStream(), 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 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