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
The following commit(s) were added to refs/heads/main by this push: new 1d9cfb6acd8 CAMEL-22116: Fix camel-openapi-validator to better support base-path such as SB war apps that are seperated by context-path 1d9cfb6acd8 is described below commit 1d9cfb6acd851b4c119f6034b7ddc22c171c1651 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Jun 3 15:15:57 2025 +0200 CAMEL-22116: Fix camel-openapi-validator to better support base-path such as SB war apps that are seperated by context-path --- .../client/OpenApiRestClientRequestValidator.java | 5 ++- .../DefaultRestOpenapiProcessorStrategy.java | 2 +- .../camel/component/rest/openapi/OpenApiUtils.java | 6 ++-- .../rest/openapi/RestOpenApiEndpoint.java | 1 + .../component/rest/openapi/RestOpenApiHelper.java | 39 ++++++++++++++++++++++ .../rest/openapi/RestOpenapiProcessorStrategy.java | 6 ++-- 6 files changed, 51 insertions(+), 8 deletions(-) diff --git a/components/camel-openapi-validator/src/main/java/org/apache/camel/component/rest/openapi/validator/client/OpenApiRestClientRequestValidator.java b/components/camel-openapi-validator/src/main/java/org/apache/camel/component/rest/openapi/validator/client/OpenApiRestClientRequestValidator.java index 9a97f9838cc..b6e857fc268 100644 --- a/components/camel-openapi-validator/src/main/java/org/apache/camel/component/rest/openapi/validator/client/OpenApiRestClientRequestValidator.java +++ b/components/camel-openapi-validator/src/main/java/org/apache/camel/component/rest/openapi/validator/client/OpenApiRestClientRequestValidator.java @@ -23,6 +23,7 @@ import com.atlassian.oai.validator.report.SimpleValidationReportFormat; import com.atlassian.oai.validator.report.ValidationReport; import io.swagger.v3.oas.models.OpenAPI; import org.apache.camel.Exchange; +import org.apache.camel.component.rest.openapi.RestOpenApiComponent; import org.apache.camel.component.rest.openapi.RestOpenApiHelper; import org.apache.camel.http.base.HttpHeaderFilterStrategy; import org.apache.camel.spi.RestClientRequestValidator; @@ -55,8 +56,10 @@ public class OpenApiRestClientRequestValidator implements RestClientRequestValid String method = exchange.getMessage().getHeader(Exchange.HTTP_METHOD, String.class); String path = exchange.getMessage().getHeader(Exchange.HTTP_PATH, String.class); + // find the base-path which can be configured in various places + RestOpenApiComponent comp = (RestOpenApiComponent) exchange.getContext().hasComponent("rest-openapi"); + String basePath = RestOpenApiHelper.determineBasePath(exchange.getContext(), comp, null, openAPI); // need to clip base-path - String basePath = RestOpenApiHelper.getBasePathFromOpenApi(openAPI); if (path != null && path.startsWith(basePath)) { path = path.substring(basePath.length()); } 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 00de0c570b1..b107edc8de6 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 @@ -116,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 = basePath != null ? basePath : RestOpenApiHelper.getBasePathFromOpenApi(openAPI); + String path = basePath; if (path == null || path.isEmpty() || path.equals("/")) { path = ""; } diff --git a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/OpenApiUtils.java b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/OpenApiUtils.java index ff7bd82268d..4a5ed22ceae 100644 --- a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/OpenApiUtils.java +++ b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/OpenApiUtils.java @@ -64,9 +64,9 @@ public class OpenApiUtils { private final AtomicBoolean packageScanInit = new AtomicBoolean(); private final Set<Class<?>> scannedClasses = new HashSet<>(); - private CamelContext camelContext; - private String bindingPackage; - private Components components; + private final CamelContext camelContext; + private final String bindingPackage; + private final Components components; public OpenApiUtils(CamelContext camelContext, String bindingPackage, Components components) { this.camelContext = camelContext; diff --git a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiEndpoint.java b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiEndpoint.java index 1a63caa79a5..ca65bb57979 100644 --- a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiEndpoint.java +++ b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiEndpoint.java @@ -505,6 +505,7 @@ public final class RestOpenApiEndpoint extends DefaultEndpoint { return new RestOpenApiProducer(endpoint.createProducer(), hasHost, requestValidator); } + @Deprecated String determineBasePath(final OpenAPI openapi) { if (isNotEmpty(basePath)) { return basePath; diff --git a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiHelper.java b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiHelper.java index 26114dcc3b3..709c829a548 100644 --- a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiHelper.java +++ b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiHelper.java @@ -27,11 +27,15 @@ import java.util.stream.Stream; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.servers.Server; +import org.apache.camel.CamelContext; import org.apache.camel.spi.ContentTypeAware; import org.apache.camel.spi.Resource; +import org.apache.camel.spi.RestConfiguration; +import org.apache.camel.support.CamelContextHelper; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.StringHelper; +import static org.apache.camel.util.ObjectHelper.isNotEmpty; import static org.apache.camel.util.StringHelper.notEmpty; public final class RestOpenApiHelper { @@ -94,6 +98,41 @@ public final class RestOpenApiHelper { return location.toLowerCase().endsWith(".yml") || location.toLowerCase().endsWith(".yaml"); } + /** + * Determines the base-path according to various configuration on component/endpoint and in the spec + */ + public static String determineBasePath( + CamelContext camelContext, RestOpenApiComponent component, RestOpenApiEndpoint endpoint, OpenAPI openAPI) { + if (endpoint != null && isNotEmpty(endpoint.getBasePath())) { + return endpoint.getBasePath(); + } + + if (component != null) { + String componentBasePath = component.getBasePath(); + if (isNotEmpty(componentBasePath)) { + return componentBasePath; + } + } + + if (openAPI != null) { + String specificationBasePath = RestOpenApiHelper.getBasePathFromOpenApi(openAPI); + if (isNotEmpty(specificationBasePath)) { + return specificationBasePath; + } + } + + String cn = endpoint != null ? endpoint.determineComponentName() : null; + RestConfiguration restConfiguration + = CamelContextHelper.getRestConfiguration(camelContext, null, cn); + String restConfigurationBasePath = restConfiguration.getContextPath(); + + if (isNotEmpty(restConfigurationBasePath)) { + return restConfigurationBasePath; + } + + return RestOpenApiComponent.DEFAULT_BASE_PATH; + } + public static String getBasePathFromOpenApi(final OpenAPI openApi) { String basePath = null; if (openApi.getServers() != null) { 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 96443f0cc2d..969157e2d0d 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,7 +56,7 @@ public interface RestOpenapiProcessorStrategy { * Validates the OpenAPI specification on startup * * @param openAPI the openapi specification - * @param basePath optional base path + * @param basePath base path * @param platformHttpConsumer the platform http consumer * @throws Exception is thrown if validation error on startup */ @@ -77,7 +77,7 @@ public interface RestOpenapiProcessorStrategy { * @param callback the AsyncCallback will be invoked when the processing of the exchange is completed. If the * exchange is completed synchronously, then the callback is also invoked synchronously. The * callback should therefore be careful of starting recursive loop. - * @return (doneSync) true to continue execute synchronously, false to continue being executed + * @return (doneSync) true to continue to execute synchronously, false to continue being executed * asynchronously */ boolean process( @@ -93,7 +93,7 @@ public interface RestOpenapiProcessorStrategy { * @param callback the AsyncCallback will be invoked when the processing of the exchange is completed. If the * exchange is completed synchronously, then the callback is also invoked synchronously. The * callback should therefore be careful of starting recursive loop. - * @return (doneSync) true to continue execute synchronously, false to continue being executed + * @return (doneSync) true to continue to execute synchronously, false to continue being executed * asynchronously */ boolean processApiSpecification(String specificationUri, Exchange exchange, AsyncCallback callback);