Skip to content

Commit

Permalink
Fix loading of static resources from the class path
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Oct 15, 2024
1 parent 5d9817a commit 7d582fb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,13 @@ protected void buildReaderModel(BuildContext<?, ?, ?, ?, ?> ctx) {
protected <V, A extends V, O extends V, AB, OB> void buildStaticModel(BuildContext<V, A, O, AB, OB> ctx) {
if (enableStandardStaticFiles) {
Function<String, URL> 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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
6 changes: 6 additions & 0 deletions core/src/test/resources/classloader/META-INF/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
openapi: 3.1.0
info:
title: Loaded from the class path
version: "1.0.0"
paths: {}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
openapi: "3.0.3"
info:
x-custom-extension: My Custom Extension

0 comments on commit 7d582fb

Please sign in to comment.