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

Fix loading of static resources from the class path #2017

Merged
merged 2 commits into from
Oct 16, 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
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