|
| 1 | +package io.smallrye.openapi.runtime.scanner; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import jakarta.ws.rs.GET; |
| 10 | +import jakarta.ws.rs.Path; |
| 11 | + |
| 12 | +import org.eclipse.microprofile.openapi.OASConfig; |
| 13 | +import org.eclipse.microprofile.openapi.OASFactory; |
| 14 | +import org.eclipse.microprofile.openapi.OASModelReader; |
| 15 | +import org.eclipse.microprofile.openapi.annotations.extensions.Extension; |
| 16 | +import org.eclipse.microprofile.openapi.models.OpenAPI; |
| 17 | +import org.eclipse.microprofile.openapi.models.media.Schema.SchemaType; |
| 18 | +import org.jboss.jandex.Index; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import io.smallrye.openapi.api.SmallRyeOASConfig; |
| 22 | +import io.smallrye.openapi.api.SmallRyeOpenAPI; |
| 23 | + |
| 24 | +class ProfileSelectionWithStaticModelTest { |
| 25 | + |
| 26 | + public static class MyReader implements OASModelReader { |
| 27 | + @Override |
| 28 | + public OpenAPI buildModel() { |
| 29 | + return OASFactory.createOpenAPI() |
| 30 | + .components(OASFactory.createComponents() |
| 31 | + .addPathItem("Orders", OASFactory.createPathItem() |
| 32 | + .GET(OASFactory.createOperation() |
| 33 | + .responses(OASFactory.createAPIResponses() |
| 34 | + .addAPIResponse("default", OASFactory.createAPIResponse() |
| 35 | + .content(OASFactory.createContent() |
| 36 | + .addMediaType("text/plain", OASFactory.createMediaType() |
| 37 | + .schema(OASFactory.createSchema() |
| 38 | + .type(List.of(SchemaType.STRING)))))))))); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void testStaticModelOperationExcluded() throws Exception { |
| 44 | + @Path("/api") |
| 45 | + class MyResource { |
| 46 | + @Path("/users") |
| 47 | + @GET |
| 48 | + @Extension(name = "x-smallrye-profile-public", value = "") |
| 49 | + public List<String> listUsers() { |
| 50 | + return Collections.emptyList(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + SmallRyeOpenAPI result; |
| 55 | + |
| 56 | + try { |
| 57 | + System.setProperty(SmallRyeOASConfig.SCAN_PROFILES, "public"); |
| 58 | + System.setProperty(OASConfig.MODEL_READER, MyReader.class.getName()); |
| 59 | + |
| 60 | + result = SmallRyeOpenAPI.builder() |
| 61 | + .enableStandardFilter(false) |
| 62 | + .enableStandardStaticFiles(false) |
| 63 | + .withCustomStaticFile(() -> { |
| 64 | + return getClass() |
| 65 | + .getClassLoader() |
| 66 | + .getResourceAsStream( |
| 67 | + "io/smallrye/openapi/runtime/scanner/static/profile-selection-static-model.yaml"); |
| 68 | + }) |
| 69 | + .enableAnnotationScan(true) |
| 70 | + .withIndex(Index.of(MyResource.class)) |
| 71 | + .build(); |
| 72 | + } finally { |
| 73 | + System.clearProperty(SmallRyeOASConfig.SCAN_PROFILES); |
| 74 | + System.clearProperty(OASConfig.MODEL_READER); |
| 75 | + } |
| 76 | + |
| 77 | + OpenAPI model = result.model(); |
| 78 | + |
| 79 | + var paths = model.getPaths().getPathItems(); |
| 80 | + assertEquals(2, paths.size(), () -> paths.keySet().toString()); |
| 81 | + assertTrue(paths.containsKey("/api/users"), () -> paths.keySet().toString()); |
| 82 | + assertTrue(paths.containsKey("/api/public/echo"), () -> paths.keySet().toString()); |
| 83 | + |
| 84 | + var componentPaths = model.getComponents().getPathItems(); |
| 85 | + assertEquals(1, componentPaths.size()); |
| 86 | + var orderOperations = componentPaths.get("Orders").getOperations(); |
| 87 | + // removed by filter |
| 88 | + assertTrue(orderOperations.isEmpty()); |
| 89 | + } |
| 90 | +} |
0 commit comments