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 8a83785f3..2a05d2f5a 100644 --- a/core/src/main/java/io/smallrye/openapi/api/SmallRyeOpenAPI.java +++ b/core/src/main/java/io/smallrye/openapi/api/SmallRyeOpenAPI.java @@ -541,7 +541,13 @@ protected void buildReaderModel(BuildContext ctx) { protected void buildStaticModel(BuildContext ctx) { if (enableStandardStaticFiles) { Function loadFn = Optional.ofNullable(resourceLocator) - .orElse(ctx.appClassLoader::getResource); + .orElse(path -> { + StringBuilder loadPath = new StringBuilder(path); + if (loadPath.charAt(0) == '/') { + loadPath.delete(0, 1); + } + return ctx.appClassLoader.getResource(loadPath.toString()); + }); ctx.staticModel = OpenApiProcessor.loadOpenApiStaticFiles(loadFn) .stream() diff --git a/core/src/test/java/io/smallrye/openapi/api/SmallRyeOpenAPIBuilderTest.java b/core/src/test/java/io/smallrye/openapi/api/SmallRyeOpenAPIBuilderTest.java new file mode 100644 index 000000000..03c199cf1 --- /dev/null +++ b/core/src/test/java/io/smallrye/openapi/api/SmallRyeOpenAPIBuilderTest.java @@ -0,0 +1,38 @@ +package io.smallrye.openapi.api; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.net.URL; +import java.net.URLClassLoader; + +import org.eclipse.microprofile.openapi.models.OpenAPI; +import org.junit.jupiter.api.Test; + +class SmallRyeOpenAPIBuilderTest { + + @Test + void testStaticFileLoadedFromClasspath() throws Exception { + URL loaderRoot = getClass() + .getClassLoader() + .getResource("classloader/META-INF/openapi.yaml") + .toURI() + .resolve("..") + .toURL(); + + ClassLoader custom = new URLClassLoader( + new URL[] { loaderRoot }, + Thread.currentThread().getContextClassLoader()); + + SmallRyeOpenAPI result = SmallRyeOpenAPI.builder() + .withApplicationClassLoader(custom) + .enableModelReader(false) + .enableStandardFilter(false) + .enableAnnotationScan(false) + .enableStandardStaticFiles(true) + .build(); + + OpenAPI model = result.model(); + assertEquals("Loaded from the class path", model.getInfo().getTitle()); + } + +} diff --git a/core/src/test/resources/classloader/META-INF/openapi.yaml b/core/src/test/resources/classloader/META-INF/openapi.yaml new file mode 100644 index 000000000..b20b80715 --- /dev/null +++ b/core/src/test/resources/classloader/META-INF/openapi.yaml @@ -0,0 +1,6 @@ +--- +openapi: 3.1.0 +info: + title: Loaded from the class path + version: "1.0.0" +paths: {} diff --git a/tools/maven-plugin/src/test/java/io/smallrye/openapi/mavenplugin/BasicIT.java b/tools/maven-plugin/src/test/java/io/smallrye/openapi/mavenplugin/BasicIT.java index 0717d0e36..b08bb7848 100644 --- a/tools/maven-plugin/src/test/java/io/smallrye/openapi/mavenplugin/BasicIT.java +++ b/tools/maven-plugin/src/test/java/io/smallrye/openapi/mavenplugin/BasicIT.java @@ -44,6 +44,7 @@ void basic_info(MavenExecutionResult result) throws IOException { assertEquals(properties.get("infoContactUrl"), schema.getInfo().getContact().getUrl()); assertEquals(properties.get("infoContactEmail"), schema.getInfo().getContact().getEmail()); assertEquals(properties.get("infoLicenseName"), schema.getInfo().getLicense().getName()); + assertEquals("My Custom Extension", schema.getInfo().getExtensions().get("x-custom-extension")); assertEquals(properties.get("infoLicenseIdentifier"), schema.getInfo().getLicense().getIdentifier()); //The URL is not tested here due to being exclusive with Identifier diff --git a/tools/maven-plugin/src/test/resources-its/io/smallrye/openapi/mavenplugin/BasicIT/basic_info/src/main/resources/META-INF/openapi.yaml b/tools/maven-plugin/src/test/resources-its/io/smallrye/openapi/mavenplugin/BasicIT/basic_info/src/main/resources/META-INF/openapi.yaml new file mode 100644 index 000000000..af86d05c3 --- /dev/null +++ b/tools/maven-plugin/src/test/resources-its/io/smallrye/openapi/mavenplugin/BasicIT/basic_info/src/main/resources/META-INF/openapi.yaml @@ -0,0 +1,3 @@ +openapi: "3.0.3" +info: + x-custom-extension: My Custom Extension