From 6b29101157152ddf66179cba161ded48770a0d8e Mon Sep 17 00:00:00 2001 From: Michael Edgar Date: Wed, 30 Oct 2024 06:30:18 -0400 Subject: [PATCH] Use ClassPathUtils from smallrye-common, consolidate static model method Signed-off-by: Michael Edgar --- core/pom.xml | 4 ++ .../smallrye/openapi/api/SmallRyeOpenAPI.java | 46 +++++++-------- .../openapi/runtime/OpenApiProcessor.java | 59 +++++++------------ pom.xml | 5 ++ 4 files changed, 52 insertions(+), 62 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 8c0d5fdc9..4fa3f7574 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -58,6 +58,10 @@ io.smallrye jandex + + io.smallrye.common + smallrye-common-classloader + org.jboss.logging jboss-logging diff --git a/core/src/main/java/io/smallrye/openapi/api/SmallRyeOpenAPI.java b/core/src/main/java/io/smallrye/openapi/api/SmallRyeOpenAPI.java index f0d4a6b56..d4c675d04 100644 --- a/core/src/main/java/io/smallrye/openapi/api/SmallRyeOpenAPI.java +++ b/core/src/main/java/io/smallrye/openapi/api/SmallRyeOpenAPI.java @@ -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; @@ -563,34 +564,33 @@ protected 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 void addStaticModel(BuildContext 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); } } diff --git a/core/src/main/java/io/smallrye/openapi/runtime/OpenApiProcessor.java b/core/src/main/java/io/smallrye/openapi/runtime/OpenApiProcessor.java index 1e6de20c6..e029e79a4 100644 --- a/core/src/main/java/io/smallrye/openapi/runtime/OpenApiProcessor.java +++ b/core/src/main/java/io/smallrye/openapi/runtime/OpenApiProcessor.java @@ -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; @@ -292,49 +287,35 @@ private static T uncheckedCall(Callable callable) { } } - public static List loadOpenApiStaticFiles(Function loadFunction) { - List apiStaticFiles = new ArrayList<>(); + public static List locateStaticFiles(Function loadFunction) { + List 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 loadOpenApiStaticFile(List apiStaticFiles, - Function loadFunction, String path, Format format) { + public static List loadOpenApiStaticFiles(Function loadFunction) { + List 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 apiStaticFiles, Function loadFunction, String path) { + Optional.ofNullable(loadFunction.apply(path)).ifPresent(apiStaticFiles::add); } } diff --git a/pom.xml b/pom.xml index 2df608ddc..5af12d529 100644 --- a/pom.xml +++ b/pom.xml @@ -123,6 +123,11 @@ jandex ${version.io.smallrye.jandex} + + io.smallrye.common + smallrye-common-classloader + 2.8.0 + io.smallrye.config smallrye-config