-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filter out disabled REST methods from the OpenAPI document
- Loading branch information
Showing
10 changed files
with
215 additions
and
19 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
core/runtime/src/main/java/io/quarkus/runtime/rest/DisabledRestEndpoints.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.quarkus.runtime.rest; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* This class serves for passing a list of disabled REST paths (via the `@EndpointDisabled` annotation) | ||
* so that an OpenAPI filter can omit them from the generated OpenAPI document. | ||
*/ | ||
public class DisabledRestEndpoints { | ||
|
||
// keys are REST paths, values are HTTP methods disabled on the given path | ||
private static Map<String, List<String>> endpoints; | ||
|
||
public static void set(Map<String, List<String>> endpoints) { | ||
DisabledRestEndpoints.endpoints = endpoints; | ||
} | ||
|
||
public static Map<String, List<String>> get() { | ||
return endpoints; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...oyment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/DisabledEndpointTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package io.quarkus.smallrye.openapi.test.jaxrs; | ||
|
||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.PUT; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.QueryParam; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.resteasy.reactive.server.EndpointDisabled; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
/** | ||
* Verify that REST endpoints that are disabled via the {@link EndpointDisabled} annotation are not included in the OpenAPI | ||
* document. | ||
*/ | ||
public class DisabledEndpointTestCase { | ||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(DisabledEndpoint.class, | ||
EnabledEndpoint.class) | ||
.add(new StringAsset("quarkus.http.root-path=/root\n"), "application.properties")); | ||
|
||
@EndpointDisabled(name = "xxx", disableIfMissing = true, stringValue = "xxx") | ||
@Path("/disabled") | ||
public static class DisabledEndpoint { | ||
|
||
@Path("/hello") | ||
@GET | ||
public String hello() { | ||
return null; | ||
} | ||
|
||
@Path("/hello2/{param1}") | ||
@GET | ||
public String hello2(@QueryParam("param1") String param1) { | ||
return null; | ||
} | ||
|
||
@Path("/hello5") | ||
@PUT | ||
public String hello5() { | ||
return null; | ||
} | ||
|
||
} | ||
|
||
@EndpointDisabled(name = "xxx", disableIfMissing = false, stringValue = "xxx") | ||
@Path("/enabled") | ||
public static class EnabledEndpoint { | ||
|
||
@Path("/hello3") | ||
@GET | ||
public String hello() { | ||
return null; | ||
} | ||
|
||
@Path("/hello4/{param1}") | ||
@GET | ||
public String hello4(@QueryParam("param1") String param1) { | ||
return null; | ||
} | ||
|
||
@Path("/hello5") | ||
@POST | ||
public String hello5() { | ||
return null; | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void testDisabledEndpoint() { | ||
RestAssured.given().header("Accept", "application/json") | ||
.when().get("/q/openapi") | ||
.prettyPeek().then() | ||
.body("paths.\"/root/disabled/hello\".get", nullValue()) | ||
.body("paths.\"/root/disabled/hello2/{param1}\".get", nullValue()) | ||
.body("paths.\"/root/enabled/hello3\".get", notNullValue()) | ||
.body("paths.\"/root/enabled/hello4/{param1}\".get", notNullValue()) | ||
.body("paths.\"/root/enabled/hello5\".post", notNullValue()) | ||
.body("paths.\"/root/enabled/hello5\".put", nullValue()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...src/main/java/io/quarkus/smallrye/openapi/runtime/filter/DisabledRestEndpointsFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.quarkus.smallrye.openapi.runtime.filter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.eclipse.microprofile.openapi.OASFilter; | ||
import org.eclipse.microprofile.openapi.models.OpenAPI; | ||
import org.eclipse.microprofile.openapi.models.PathItem; | ||
|
||
import io.quarkus.runtime.rest.DisabledRestEndpoints; | ||
|
||
/** | ||
* If the RESTEasy Reactive extension passed us a list of REST paths that are disabled via the @DisabledRestEndpoint | ||
* annotation, remove them from the OpenAPI document. This has to be done at runtime because | ||
* the annotation is controlled by a runtime config property. | ||
*/ | ||
public class DisabledRestEndpointsFilter implements OASFilter { | ||
|
||
public void filterOpenAPI(OpenAPI openAPI) { | ||
Map<String, List<String>> disabledEndpointsMap = DisabledRestEndpoints.get(); | ||
if (disabledEndpointsMap != null) { | ||
Map<String, PathItem> pathItems = openAPI.getPaths().getPathItems(); | ||
List<String> emptyPathItems = new ArrayList<>(); | ||
if (pathItems != null) { | ||
for (Map.Entry<String, PathItem> entry : pathItems.entrySet()) { | ||
String path = entry.getKey(); | ||
PathItem pathItem = entry.getValue(); | ||
List<String> disabledMethodsForThisPath = disabledEndpointsMap.get(path); | ||
if (disabledMethodsForThisPath != null) { | ||
disabledMethodsForThisPath.forEach(method -> { | ||
pathItem.setOperation(PathItem.HttpMethod.valueOf(method), null); | ||
}); | ||
// if the pathItem is now empty, remove it | ||
if (pathItem.getOperations().isEmpty()) { | ||
emptyPathItems.add(path); | ||
} | ||
} | ||
} | ||
emptyPathItems.forEach(openAPI.getPaths()::removePathItem); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters