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 280bb72047d CAMEL-22116: Fix camel-openapi-validator to better support base-path such as SB war apps that are seperated by context-path 280bb72047d is described below commit 280bb72047d4f036ecba24f40941c919d28faa88 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Jun 3 13:06:59 2025 +0200 CAMEL-22116: Fix camel-openapi-validator to better support base-path such as SB war apps that are seperated by context-path --- components/camel-openapi-validator/pom.xml | 2 +- .../client/OpenApiRestClientRequestValidator.java | 9 ++++++++ .../component/rest/openapi/RestOpenApiHelper.java | 2 +- .../rest/openapi/RestOpenApiProcessor.java | 25 +++++++++++----------- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/components/camel-openapi-validator/pom.xml b/components/camel-openapi-validator/pom.xml index 46226572df8..2d4254e4f12 100644 --- a/components/camel-openapi-validator/pom.xml +++ b/components/camel-openapi-validator/pom.xml @@ -40,7 +40,7 @@ <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-support</artifactId> + <artifactId>camel-rest-openapi</artifactId> </dependency> <dependency> 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 76382cbe283..e0dcb1d4bf8 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.RestOpenApiHelper; import org.apache.camel.spi.RestClientRequestValidator; import org.apache.camel.spi.annotations.JdkService; import org.apache.camel.support.ExchangeHelper; @@ -40,6 +41,13 @@ 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); + + // need to clip base-path + String basePath = RestOpenApiHelper.getBasePathFromOpenApi(openAPI); + if (path != null && path.startsWith(basePath)) { + path = path.substring(basePath.length()); + } + String accept = exchange.getMessage().getHeader("Accept", String.class); String contentType = ExchangeHelper.getContentType(exchange); String body = MessageHelper.extractBodyAsString(exchange.getIn()); @@ -56,6 +64,7 @@ public class OpenApiRestClientRequestValidator implements RestClientRequestValid } // Use all non-Camel headers for (String header : exchange.getMessage().getHeaders().keySet()) { + // TODO: should skip standard HTTP headers like: Host, User-Agent if (!startsWithIgnoreCase(header, "Camel")) { builder.withHeader(header, exchange.getMessage().getHeader(header, String.class)); } 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 088db205b6e..26114dcc3b3 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 @@ -34,7 +34,7 @@ import org.apache.camel.util.StringHelper; import static org.apache.camel.util.StringHelper.notEmpty; -final class RestOpenApiHelper { +public final class RestOpenApiHelper { private static final Pattern HOST_PATTERN = Pattern.compile("https?://[^:]+(:\\d+)?", Pattern.CASE_INSENSITIVE); private static final List<String> YAML_CONTENT_TYPES = Arrays.asList("application/yaml", "application/yml", 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 aa67c225306..626372536f7 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 @@ -33,7 +33,6 @@ import org.apache.camel.support.processor.RestBindingAdvice; import org.apache.camel.support.processor.RestBindingAdviceFactory; import org.apache.camel.support.processor.RestBindingConfiguration; import org.apache.camel.support.service.ServiceHelper; -import org.apache.camel.util.URISupport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -95,42 +94,42 @@ public class RestOpenApiProcessor extends DelegateAsyncProcessor implements Came @Override public boolean process(Exchange exchange, AsyncCallback callback) { // use HTTP_URI as this works for all runtimes - String uri = exchange.getMessage().getHeader(Exchange.HTTP_URI, String.class); - if (uri != null) { - uri = URISupport.stripQuery(uri); - } - if (uri != null && uri.startsWith(basePath)) { - uri = uri.substring(basePath.length()); + String path = exchange.getMessage().getHeader(Exchange.HTTP_PATH, String.class); + // if (path != null) { + // path = URISupport.stripQuery(path); + // } + if (path != null && path.startsWith(basePath)) { + path = path.substring(basePath.length()); } String verb = exchange.getMessage().getHeader(Exchange.HTTP_METHOD, String.class); RestConsumerContextPathMatcher.ConsumerPath<Operation> m - = RestConsumerContextPathMatcher.matchBestPath(verb, uri, paths); + = RestConsumerContextPathMatcher.matchBestPath(verb, path, paths); if (m instanceof RestOpenApiConsumerPath rcp) { Operation o = rcp.getConsumer(); String consumerPath = rcp.getConsumerPath(); //if uri is not starting with slash then remove the slash in the consumerPath from the openApi spec - if (consumerPath.startsWith("/") && uri != null && !uri.startsWith("/")) { + if (consumerPath.startsWith("/") && path != null && !path.startsWith("/")) { consumerPath = consumerPath.substring(1); } // map path-parameters from operation to camel headers - HttpHelper.evalPlaceholders(exchange.getMessage().getHeaders(), uri, consumerPath); + HttpHelper.evalPlaceholders(exchange.getMessage().getHeaders(), path, consumerPath); // process the incoming request - return restOpenapiProcessorStrategy.process(openAPI, o, verb, uri, rcp.getBinding(), exchange, callback); + return restOpenapiProcessorStrategy.process(openAPI, o, verb, path, rcp.getBinding(), exchange, callback); } // is it the api-context path - if (uri != null && uri.equals(apiContextPath)) { + if (path != null && path.equals(apiContextPath)) { return restOpenapiProcessorStrategy.processApiSpecification(endpoint.getSpecificationUri(), exchange, callback); } // okay we cannot process this requires so return either 404 or 405. // to know if its 405 then we need to check if any other HTTP method would have a consumer for the "same" request - final String contextPath = uri; + final String contextPath = path; List<String> allow = METHODS.stream() .filter(v -> RestConsumerContextPathMatcher.matchBestPath(v, contextPath, paths) != null).toList(); if (allow.isEmpty()) {