-
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.
Merge pull request #36152 from phillip-kruger/openapi-build-filter
Allow Build time OpenAPI Filters
- Loading branch information
Showing
9 changed files
with
244 additions
and
15 deletions.
There are no files selected for viewing
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
34 changes: 34 additions & 0 deletions
34
...pi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/MyBuildTimeFilter.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,34 @@ | ||
package io.quarkus.smallrye.openapi.test.jaxrs; | ||
|
||
import java.util.Collection; | ||
|
||
import org.eclipse.microprofile.openapi.OASFactory; | ||
import org.eclipse.microprofile.openapi.OASFilter; | ||
import org.eclipse.microprofile.openapi.models.OpenAPI; | ||
import org.eclipse.microprofile.openapi.models.info.Info; | ||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.IndexView; | ||
|
||
import io.quarkus.smallrye.openapi.OpenApiFilter; | ||
|
||
/** | ||
* Filter to add custom elements | ||
*/ | ||
@OpenApiFilter(OpenApiFilter.RunStage.BUILD) | ||
public class MyBuildTimeFilter implements OASFilter { | ||
|
||
private IndexView view; | ||
|
||
public MyBuildTimeFilter(IndexView view) { | ||
this.view = view; | ||
} | ||
|
||
@Override | ||
public void filterOpenAPI(OpenAPI openAPI) { | ||
Collection<ClassInfo> knownClasses = this.view.getKnownClasses(); | ||
Info info = OASFactory.createInfo(); | ||
info.setDescription("Created from Annotated Buildtime filter with " + knownClasses.size() + " known indexed classes"); | ||
openAPI.setInfo(info); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...napi/deployment/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/MyRunTimeFilter.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,23 @@ | ||
package io.quarkus.smallrye.openapi.test.jaxrs; | ||
|
||
import org.eclipse.microprofile.openapi.OASFactory; | ||
import org.eclipse.microprofile.openapi.OASFilter; | ||
import org.eclipse.microprofile.openapi.models.OpenAPI; | ||
import org.eclipse.microprofile.openapi.models.info.Info; | ||
|
||
import io.quarkus.smallrye.openapi.OpenApiFilter; | ||
|
||
/** | ||
* Filter to add custom elements | ||
*/ | ||
@OpenApiFilter(OpenApiFilter.RunStage.RUN) | ||
public class MyRunTimeFilter implements OASFilter { | ||
|
||
@Override | ||
public void filterOpenAPI(OpenAPI openAPI) { | ||
Info info = OASFactory.createInfo(); | ||
info.setDescription("Created from Annotated Runtime filter"); | ||
openAPI.setInfo(info); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
.../src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiBuiltTimeFilterTestCase.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,28 @@ | ||
package io.quarkus.smallrye.openapi.test.jaxrs; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class OpenApiBuiltTimeFilterTestCase { | ||
private static final String OPEN_API_PATH = "/q/openapi"; | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(OpenApiResource.class, ResourceBean.class, MyBuildTimeFilter.class)); | ||
|
||
@Test | ||
public void testOpenApiFilterResource() { | ||
RestAssured.given().header("Accept", "application/json") | ||
.when().get(OPEN_API_PATH) | ||
.then() | ||
.header("Content-Type", "application/json;charset=UTF-8") | ||
.body("info.description", Matchers.startsWith("Created from Annotated Buildtime filter with")); | ||
|
||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...nt/src/test/java/io/quarkus/smallrye/openapi/test/jaxrs/OpenApiRunTimeFilterTestCase.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,28 @@ | ||
package io.quarkus.smallrye.openapi.test.jaxrs; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class OpenApiRunTimeFilterTestCase { | ||
private static final String OPEN_API_PATH = "/q/openapi"; | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(OpenApiResource.class, ResourceBean.class, MyRunTimeFilter.class)); | ||
|
||
@Test | ||
public void testOpenApiFilterResource() { | ||
RestAssured.given().header("Accept", "application/json") | ||
.when().get(OPEN_API_PATH) | ||
.then() | ||
.header("Content-Type", "application/json;charset=UTF-8") | ||
.body("info.description", Matchers.startsWith("Created from Annotated Runtime filter")); | ||
|
||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...ons/smallrye-openapi/runtime/src/main/java/io/quarkus/smallrye/openapi/OpenApiFilter.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,27 @@ | ||
package io.quarkus.smallrye.openapi; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* This extends the MP way to define an `org.eclipse.microprofile.openapi.OASFilter`. | ||
* Currently in MP, this needs to be added to a config `mp.openapi.filter` and only allows one filter (class) per application. | ||
* | ||
* This Annotation, that is Quarkus specific, will allow users to annotate one or more classes and that will be | ||
* all that is needed to include the filter. (No config needed). Filters still need to extend. | ||
* | ||
* @see https://download.eclipse.org/microprofile/microprofile-open-api-3.1.1/microprofile-openapi-spec-3.1.1.html#_oasfilter | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface OpenApiFilter { | ||
RunStage value() default RunStage.RUN; // When this filter should run, default Runtime | ||
|
||
static enum RunStage { | ||
BUILD, | ||
RUN, | ||
BOTH | ||
} | ||
} |
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