You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by zh...@apache.org on 2022/03/16 01:04:45 UTC
[camel-quarkus] branch main updated: Fix #3606 to add reflection configs for Schema and its subClasses (#3624)
This is an automated email from the ASF dual-hosted git repository.
zhfeng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
The following commit(s) were added to refs/heads/main by this push:
new 26ab5de Fix #3606 to add reflection configs for Schema and its subClasses (#3624)
26ab5de is described below
commit 26ab5de9065f481dc369579ac00271365ad59b14
Author: Amos Feng <zh...@gmail.com>
AuthorDate: Wed Mar 16 09:04:30 2022 +0800
Fix #3606 to add reflection configs for Schema and its subClasses (#3624)
---
.../java/deployment/OpenApiJavaProcessor.java | 22 ++++++++++++++++++++++
.../component/openapijava/it/OpenApiRoutes.java | 5 +++--
.../component/openapijava/it/model/Fruit.java | 4 +++-
.../component/openapijava/it/v2/OpenApiV2Test.java | 16 ++++++++++++++++
.../component/openapijava/it/v3/OpenApiV3Test.java | 16 ++++++++++++++++
5 files changed, 60 insertions(+), 3 deletions(-)
diff --git a/extensions/openapi-java/deployment/src/main/java/org/apache/camel/quarkus/component/openapi/java/deployment/OpenApiJavaProcessor.java b/extensions/openapi-java/deployment/src/main/java/org/apache/camel/quarkus/component/openapi/java/deployment/OpenApiJavaProcessor.java
index 137ec91..07f65a3 100644
--- a/extensions/openapi-java/deployment/src/main/java/org/apache/camel/quarkus/component/openapi/java/deployment/OpenApiJavaProcessor.java
+++ b/extensions/openapi-java/deployment/src/main/java/org/apache/camel/quarkus/component/openapi/java/deployment/OpenApiJavaProcessor.java
@@ -36,13 +36,17 @@ import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
+import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
+import io.quarkus.deployment.builditem.IndexDependencyBuildItem;
+import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.smallrye.openapi.deployment.spi.AddToOpenAPIDefinitionBuildItem;
import io.smallrye.openapi.api.models.OpenAPIImpl;
import io.smallrye.openapi.api.util.MergeUtil;
import io.smallrye.openapi.runtime.io.definition.DefinitionReader;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
+import io.swagger.v3.oas.models.media.Schema;
import org.apache.camel.CamelContext;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
@@ -62,6 +66,9 @@ import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.openapi.OASFilter;
import org.eclipse.microprofile.openapi.models.OpenAPI;
+import org.jboss.jandex.ClassInfo;
+import org.jboss.jandex.DotName;
+import org.jboss.jandex.IndexView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -71,12 +78,27 @@ class OpenApiJavaProcessor {
private static final String FEATURE = "camel-openapi-java";
private static final Logger LOGGER = LoggerFactory.getLogger(OpenApiJavaProcessor.class);
+ private static final DotName SCHEMA = DotName.createSimple(Schema.class.getName());
@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}
+ @BuildStep
+ void addDependencies(BuildProducer<IndexDependencyBuildItem> indexDependency) {
+ indexDependency.produce(new IndexDependencyBuildItem("io.swagger.core.v3", "swagger-models"));
+ }
+
+ @BuildStep
+ void reflectiveClasses(BuildProducer<ReflectiveClassBuildItem> reflectiveClasses, CombinedIndexBuildItem combinedIndex) {
+ reflectiveClasses.produce(new ReflectiveClassBuildItem(true, true, SCHEMA.toString()));
+
+ IndexView index = combinedIndex.getIndex();
+ index.getAllKnownSubclasses(SCHEMA).stream().map(ClassInfo::toString).forEach(
+ name -> reflectiveClasses.produce(new ReflectiveClassBuildItem(false, false, name)));
+ }
+
@BuildStep(onlyIf = ExposeOpenApiEnabled.class)
void exposeOpenAPI(List<CamelRoutesBuilderClassBuildItem> routesBuilderClasses,
BuildProducer<AddToOpenAPIDefinitionBuildItem> openAPI,
diff --git a/integration-tests/openapi-java/src/main/java/org/apache/camel/quarkus/component/openapijava/it/OpenApiRoutes.java b/integration-tests/openapi-java/src/main/java/org/apache/camel/quarkus/component/openapijava/it/OpenApiRoutes.java
index 8e6a1e3..884bf4c 100644
--- a/integration-tests/openapi-java/src/main/java/org/apache/camel/quarkus/component/openapijava/it/OpenApiRoutes.java
+++ b/integration-tests/openapi-java/src/main/java/org/apache/camel/quarkus/component/openapijava/it/OpenApiRoutes.java
@@ -60,6 +60,7 @@ public class OpenApiRoutes extends RouteBuilder {
rest("/api")
.get("/fruits/list")
+ .type(Fruit.class)
.description("Gets a list of fruits")
.id("list")
.produces(MediaType.APPLICATION_JSON)
@@ -173,8 +174,8 @@ public class OpenApiRoutes extends RouteBuilder {
private Set<Fruit> getFruits() {
Set<Fruit> fruits = Collections.newSetFromMap(Collections.synchronizedMap(new LinkedHashMap<>()));
- fruits.add(new Fruit("Apple", "Winter fruit"));
- fruits.add(new Fruit("Pineapple", "Tropical fruit"));
+ fruits.add(new Fruit("Apple", "Winter fruit", 10));
+ fruits.add(new Fruit("Pineapple", "Tropical fruit", 20));
return fruits;
}
}
diff --git a/integration-tests/openapi-java/src/main/java/org/apache/camel/quarkus/component/openapijava/it/model/Fruit.java b/integration-tests/openapi-java/src/main/java/org/apache/camel/quarkus/component/openapijava/it/model/Fruit.java
index 44ee9a1..52117e7 100644
--- a/integration-tests/openapi-java/src/main/java/org/apache/camel/quarkus/component/openapijava/it/model/Fruit.java
+++ b/integration-tests/openapi-java/src/main/java/org/apache/camel/quarkus/component/openapijava/it/model/Fruit.java
@@ -23,9 +23,11 @@ public class Fruit {
public String name;
public String description;
+ public Integer num;
- public Fruit(String name, String description) {
+ public Fruit(String name, String description, Integer num) {
this.name = name;
this.description = description;
+ this.num = num;
}
}
diff --git a/integration-tests/openapi-java/src/test/java/org/apache/camel/quarkus/component/openapijava/it/v2/OpenApiV2Test.java b/integration-tests/openapi-java/src/test/java/org/apache/camel/quarkus/component/openapijava/it/v2/OpenApiV2Test.java
index 0b57703..d59ac56 100644
--- a/integration-tests/openapi-java/src/test/java/org/apache/camel/quarkus/component/openapijava/it/v2/OpenApiV2Test.java
+++ b/integration-tests/openapi-java/src/test/java/org/apache/camel/quarkus/component/openapijava/it/v2/OpenApiV2Test.java
@@ -136,4 +136,20 @@ public class OpenApiV2Test extends OpenApiTest {
"basePath", is("/api"),
"schemes", contains("http", "https"));
}
+
+ @ParameterizedTest
+ @EnumSource(OpenApiContentType.class)
+ public void openApiDefinitions(OpenApiContentType contentType) {
+ RestAssured.given()
+ .header("Accept", contentType.getMimeType())
+ .get("/openapi")
+ .then()
+ .contentType(ContentType.JSON)
+ .statusCode(200)
+ .body(
+ "definitions.Fruit.type", is("object"),
+ "definitions.Fruit.properties.name.type", is("string"),
+ "definitions.Fruit.properties.description.type", is("string"),
+ "definitions.Fruit.properties.num.type", is("integer"));
+ }
}
diff --git a/integration-tests/openapi-java/src/test/java/org/apache/camel/quarkus/component/openapijava/it/v3/OpenApiV3Test.java b/integration-tests/openapi-java/src/test/java/org/apache/camel/quarkus/component/openapijava/it/v3/OpenApiV3Test.java
index 463b943..0f7159b 100644
--- a/integration-tests/openapi-java/src/test/java/org/apache/camel/quarkus/component/openapijava/it/v3/OpenApiV3Test.java
+++ b/integration-tests/openapi-java/src/test/java/org/apache/camel/quarkus/component/openapijava/it/v3/OpenApiV3Test.java
@@ -92,4 +92,20 @@ public class OpenApiV3Test extends OpenApiTest {
.statusCode(200)
.body("servers[0].url", is("http://localhost:8080/api"));
}
+
+ @ParameterizedTest
+ @EnumSource(OpenApiContentType.class)
+ public void openApiComponents(OpenApiContentType contentType) {
+ RestAssured.given()
+ .header("Accept", contentType.getMimeType())
+ .get("/openapi")
+ .then()
+ .contentType(ContentType.JSON)
+ .statusCode(200)
+ .body(
+ "components.schemas.Fruit.type", is("object"),
+ "components.schemas.Fruit.properties.name.type", is("string"),
+ "components.schemas.Fruit.properties.description.type", is("string"),
+ "components.schemas.Fruit.properties.num.type", is("integer"));
+ }
}