You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2025/05/08 07:59:00 UTC

(camel) 02/02: CAMEL-21965: camel-rest-openapi: Fix when configured explicit base-path then this path are used in the http endpoint registry. Added docs to rest-dsl how to configure this.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit ce4a3f44c5552e7955c7814a876d8156c887f6f1
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu May 8 09:55:56 2025 +0200

    CAMEL-21965: camel-rest-openapi: Fix when configured explicit base-path then this path are used in the http endpoint registry. Added docs to rest-dsl how to configure this.
---
 .../openapi/DefaultRestOpenapiProcessorStrategy.java   |  5 +++--
 .../component/rest/openapi/RestOpenApiProcessor.java   |  2 +-
 .../rest/openapi/RestOpenapiProcessorStrategy.java     |  4 +++-
 .../rest/openapi/RestOpenapiProcessorStrategyTest.java |  2 +-
 .../modules/ROOT/pages/rest-dsl-openapi.adoc           | 18 ++++++++++++++++++
 5 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java
index c931dc20102..00de0c570b1 100644
--- a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java
+++ b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java
@@ -75,7 +75,8 @@ public class DefaultRestOpenapiProcessorStrategy extends ServiceSupport
     private final List<String> uris = new ArrayList<>();
 
     @Override
-    public void validateOpenApi(OpenAPI openAPI, PlatformHttpConsumerAware platformHttpConsumer) throws Exception {
+    public void validateOpenApi(OpenAPI openAPI, String basePath, PlatformHttpConsumerAware platformHttpConsumer)
+            throws Exception {
         List<String> ids = new ArrayList<>();
         for (var e : openAPI.getPaths().entrySet()) {
             for (var o : e.getValue().readOperationsMap().entrySet()) {
@@ -115,7 +116,7 @@ public class DefaultRestOpenapiProcessorStrategy extends ServiceSupport
         // enlist open-api rest services
         PlatformHttpComponent phc = camelContext.getComponent("platform-http", PlatformHttpComponent.class);
         if (phc != null) {
-            String path = RestOpenApiHelper.getBasePathFromOpenApi(openAPI);
+            String path = basePath != null ? basePath : RestOpenApiHelper.getBasePathFromOpenApi(openAPI);
             if (path == null || path.isEmpty() || path.equals("/")) {
                 path = "";
             }
diff --git a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiProcessor.java b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiProcessor.java
index d52dfdd1fc4..aa67c225306 100644
--- a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiProcessor.java
+++ b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiProcessor.java
@@ -189,7 +189,7 @@ public class RestOpenApiProcessor extends DelegateAsyncProcessor implements Came
         ServiceHelper.initService(restOpenapiProcessorStrategy);
 
         // validate openapi contract
-        restOpenapiProcessorStrategy.validateOpenApi(openAPI, platformHttpConsumer);
+        restOpenapiProcessorStrategy.validateOpenApi(openAPI, basePath, platformHttpConsumer);
     }
 
     private RestBindingConfiguration createRestBindingConfiguration(Operation o) throws Exception {
diff --git a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenapiProcessorStrategy.java b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenapiProcessorStrategy.java
index 7c639071633..96443f0cc2d 100644
--- a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenapiProcessorStrategy.java
+++ b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenapiProcessorStrategy.java
@@ -56,10 +56,12 @@ public interface RestOpenapiProcessorStrategy {
      * Validates the OpenAPI specification on startup
      *
      * @param  openAPI              the openapi specification
+     * @param  basePath             optional base path
      * @param  platformHttpConsumer the platform http consumer
      * @throws Exception            is thrown if validation error on startup
      */
-    default void validateOpenApi(OpenAPI openAPI, PlatformHttpConsumerAware platformHttpConsumer) throws Exception {
+    default void validateOpenApi(OpenAPI openAPI, String basePath, PlatformHttpConsumerAware platformHttpConsumer)
+            throws Exception {
         // noop
     }
 
diff --git a/components/camel-rest-openapi/src/test/java/org/apache/camel/component/rest/openapi/RestOpenapiProcessorStrategyTest.java b/components/camel-rest-openapi/src/test/java/org/apache/camel/component/rest/openapi/RestOpenapiProcessorStrategyTest.java
index 7c906b41106..567c4835262 100644
--- a/components/camel-rest-openapi/src/test/java/org/apache/camel/component/rest/openapi/RestOpenapiProcessorStrategyTest.java
+++ b/components/camel-rest-openapi/src/test/java/org/apache/camel/component/rest/openapi/RestOpenapiProcessorStrategyTest.java
@@ -44,7 +44,7 @@ class RestOpenapiProcessorStrategyTest extends ManagedCamelTestSupport {
         ((DefaultRestOpenapiProcessorStrategy) restOpenapiProcessorStrategy).setCamelContext(camelContext);
         restOpenapiProcessorStrategy.setMissingOperation("fail");
         Exception ex = assertThrows(IllegalArgumentException.class,
-                () -> restOpenapiProcessorStrategy.validateOpenApi(getOpenApi(), mock(PlatformHttpConsumerAware.class)));
+                () -> restOpenapiProcessorStrategy.validateOpenApi(getOpenApi(), null, mock(PlatformHttpConsumerAware.class)));
         assertTrue(ex.getMessage().contains("direct:GENOPID_GET.users"));
         assertTrue(ex.getMessage().contains("direct:GENOPID_GET.user._id_"));
 
diff --git a/docs/user-manual/modules/ROOT/pages/rest-dsl-openapi.adoc b/docs/user-manual/modules/ROOT/pages/rest-dsl-openapi.adoc
index d5300db69d1..43baf0d18d6 100644
--- a/docs/user-manual/modules/ROOT/pages/rest-dsl-openapi.adoc
+++ b/docs/user-manual/modules/ROOT/pages/rest-dsl-openapi.adoc
@@ -127,6 +127,24 @@ YAML::
 ----
 ====
 
+=== Configuring Base Path
+
+By default, Camel uses the base path that are specified in the OpenAPI contract such as:
+
+[source,json]
+----
+    "basePath": {
+            "default": "/api/v3"
+    }
+----
+
+You can configure Camel to use a different base path by setting in `application.properties`:
+
+[source,properties]
+----
+camel.component.rest-openapi.base-path = /myapi
+----
+
 === Ignoring missing API operations
 
 When using OpenAPI with _contract-first_, then Camel will on startup check if there is a corresponding `direct:operationId` route