Skip to content

Commit 5b886f7

Browse files
authored
Merge pull request #51287 from MikeEdgar/suppress-op-method-ref-ext
OpenAPI: skip adding method references to model without OperationFilter
2 parents 91b5b12 + 25432f0 commit 5b886f7

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
import io.quarkus.vertx.http.runtime.security.SecurityHandlerPriorities;
122122
import io.smallrye.openapi.api.OpenApiConfig;
123123
import io.smallrye.openapi.api.OpenApiDocument;
124+
import io.smallrye.openapi.api.OperationHandler;
124125
import io.smallrye.openapi.api.SmallRyeOpenAPI;
125126
import io.smallrye.openapi.api.constants.SecurityConstants;
126127
import io.smallrye.openapi.api.util.MergeUtil;
@@ -881,7 +882,13 @@ private void registerReflectionForApiResponseSchemaSerialization(BuildProducer<R
881882
}
882883
}
883884

884-
private void handleOperation(Operation operation, ClassInfo classInfo, MethodInfo method) {
885+
/**
886+
* Callback invoked by the smallrye-open-api annotation scanner for each discovered API
887+
* operation. We use this to set a (private) extension in the OpenAPI model which is then
888+
* used by the {@link OperationFilter} to match operations with the security and
889+
* tag information discovered earlier in the build by this class.
890+
*/
891+
private void addMethodReferenceExtension(Operation operation, ClassInfo classInfo, MethodInfo method) {
885892
String methodRef = createUniqueMethodReference(classInfo, method);
886893
operation.addExtension(OperationFilter.EXT_METHOD_REF, methodRef);
887894
}
@@ -906,6 +913,16 @@ public void build(BuildProducer<GeneratedResourceBuildItem> resourceBuildItemBui
906913
.map(IgnoreStaticDocumentBuildItem::getUrlIgnorePattern)
907914
.toList();
908915

916+
/*
917+
* Only add method references if the OperationFilter is enabled. Otherwise,
918+
* they are not needed.
919+
*/
920+
OperationHandler operationHandler = openAPIBuildItems.stream()
921+
.map(AddToOpenAPIDefinitionBuildItem::getOASFilter)
922+
.anyMatch(OperationFilter.class::isInstance)
923+
? this::addMethodReferenceExtension
924+
: OperationHandler.DEFAULT;
925+
909926
SmallRyeOpenAPI.Builder builder = SmallRyeOpenAPI.builder()
910927
.withConfig(config)
911928
.withIndex(index)
@@ -926,7 +943,7 @@ public void build(BuildProducer<GeneratedResourceBuildItem> resourceBuildItemBui
926943
.withScannerFilter(getScannerFilter(capabilities, index))
927944
.withContextRootResolver(getContextRootResolver(config, capabilities, httpRootPathBuildItem))
928945
.withTypeConverter(getTypeConverter(index, capabilities))
929-
.withOperationHandler(this::handleOperation)
946+
.withOperationHandler(operationHandler)
930947
.enableUnannotatedPathParameters(capabilities.isPresent(Capability.RESTEASY_REACTIVE))
931948
.enableStandardFilter(false)
932949
.withFilters(openAPIBuildItems.stream()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.quarkus.smallrye.openapi.test.jaxrs;
2+
3+
import static org.hamcrest.Matchers.hasKey;
4+
import static org.hamcrest.Matchers.not;
5+
6+
import jakarta.ws.rs.GET;
7+
import jakarta.ws.rs.Path;
8+
9+
import org.jboss.shrinkwrap.api.asset.StringAsset;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.extension.RegisterExtension;
12+
13+
import io.quarkus.smallrye.openapi.deployment.filter.OperationFilter;
14+
import io.quarkus.test.QuarkusUnitTest;
15+
import io.restassured.RestAssured;
16+
17+
/**
18+
* Verify that method reference extensions are not added to the OpenAPI model when the
19+
* {@link OperationFilter} is not in use (disabled via several configuration properties).
20+
*/
21+
class DisabledOperationFilterTestCase {
22+
@RegisterExtension
23+
static QuarkusUnitTest runner = new QuarkusUnitTest()
24+
.withApplicationRoot(jar -> jar
25+
.addClasses(Endpoint.class)
26+
.add(new StringAsset("""
27+
quarkus.smallrye-openapi.auto-add-operation-summary=false
28+
quarkus.smallrye-openapi.auto-add-tags=false
29+
quarkus.smallrye-openapi.auto-add-security-requirement=false
30+
"""), "application.properties"));
31+
32+
@Path("/api")
33+
public static class Endpoint {
34+
@Path("/op1")
35+
@GET
36+
public String op1() {
37+
return null;
38+
}
39+
40+
@Path("/op2")
41+
@GET
42+
public String op2() {
43+
return null;
44+
}
45+
}
46+
47+
@Test
48+
void testMethodRefExtensionsAbsent() {
49+
RestAssured.given().header("Accept", "application/json")
50+
.when().get("/q/openapi")
51+
.then()
52+
.log().ifValidationFails()
53+
.body("paths.\"/api/op1\".get", not(hasKey(OperationFilter.EXT_METHOD_REF)))
54+
.body("paths.\"/api/op2\".get", not(hasKey(OperationFilter.EXT_METHOD_REF)));
55+
}
56+
}

0 commit comments

Comments
 (0)