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 bf27aa0549f CAMEL-22121: rest-dsl: Add client response validator
bf27aa0549f is described below
commit bf27aa0549ff535d7e43187916a0c62a44b80142
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Jun 18 10:15:37 2025 +0200
CAMEL-22121: rest-dsl: Add client response validator
---
.../src/main/docs/openapi-validator.adoc | 2 --
.../component/rest/openapi/RestOpenApiHelper.java | 15 ++++----
.../apache/camel/model/rest/RestDefinition.java | 33 +++++++++++++-----
.../apache/camel/support/RestComponentHelper.java | 40 +++++++++-------------
4 files changed, 50 insertions(+), 40 deletions(-)
diff --git
a/components/camel-openapi-validator/src/main/docs/openapi-validator.adoc
b/components/camel-openapi-validator/src/main/docs/openapi-validator.adoc
index 7506c61ae07..7fa06027a79 100644
--- a/components/camel-openapi-validator/src/main/docs/openapi-validator.adoc
+++ b/components/camel-openapi-validator/src/main/docs/openapi-validator.adoc
@@ -15,8 +15,6 @@ The `camel-openapi-validator` uses the third party
https://bitbucket.org/atlassi
library instead for client request/response validator. This library is a more
extensive validator than
the default validator from `camel-core`.
-NOTE: This library does not work with running in `camel-jbang`.
-
== Auto-detection from classpath
To use this implementation all you need to do is to add the
`camel-openapi-validator` dependency to the classpath.
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 709c829a548..4e2fcd559aa 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
@@ -114,13 +114,6 @@ public final class RestOpenApiHelper {
}
}
- 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);
@@ -130,6 +123,14 @@ public final class RestOpenApiHelper {
return restConfigurationBasePath;
}
+ // openapi spec should be last, as all the above can override the
configuration
+ if (openAPI != null) {
+ String specificationBasePath =
RestOpenApiHelper.getBasePathFromOpenApi(openAPI);
+ if (isNotEmpty(specificationBasePath)) {
+ return specificationBasePath;
+ }
+ }
+
return RestOpenApiComponent.DEFAULT_BASE_PATH;
}
diff --git
a/core/camel-core-model/src/main/java/org/apache/camel/model/rest/RestDefinition.java
b/core/camel-core-model/src/main/java/org/apache/camel/model/rest/RestDefinition.java
index dcf9912bc08..eb519dbca59 100644
---
a/core/camel-core-model/src/main/java/org/apache/camel/model/rest/RestDefinition.java
+++
b/core/camel-core-model/src/main/java/org/apache/camel/model/rest/RestDefinition.java
@@ -940,7 +940,7 @@ public class RestDefinition extends
OptionalIdentifiedDefinition<RestDefinition>
}
if (openApi != null) {
addRouteDefinition(camelContext, openApi, answer,
config.getComponent(), config.getProducerComponent(),
- config.getApiContextPath(),
config.isClientRequestValidation());
+ config.getApiContextPath(),
config.isClientRequestValidation(), config.isClientResponseValidation());
}
return answer;
@@ -1035,7 +1035,7 @@ public class RestDefinition extends
OptionalIdentifiedDefinition<RestDefinition>
private void addRouteDefinition(
CamelContext camelContext, OpenApiDefinition openApi,
List<RouteDefinition> answer,
String component, String producerComponent, String apiContextPath,
- boolean clientValidation) {
+ boolean clientRequestValidation, boolean clientResponseValidation)
{
RouteDefinition route = new RouteDefinition();
if (openApi.getRouteId() != null) {
@@ -1044,7 +1044,16 @@ public class RestDefinition extends
OptionalIdentifiedDefinition<RestDefinition>
// add dummy empty stop
route.getOutputs().add(new StopDefinition());
- final RestBindingDefinition binding =
getRestBindingDefinition(camelContext, component);
+ // local configuration can override global
+ if (getClientRequestValidation() != null) {
+ clientRequestValidation = parseBoolean(camelContext,
getClientRequestValidation());
+ }
+ if (getClientResponseValidation() != null) {
+ clientResponseValidation = parseBoolean(camelContext,
getClientResponseValidation());
+ }
+
+ final RestBindingDefinition binding
+ = getRestBindingDefinition(camelContext, component,
clientRequestValidation, clientResponseValidation);
route.setRestBindingDefinition(binding);
// append options
@@ -1055,11 +1064,12 @@ public class RestDefinition extends
OptionalIdentifiedDefinition<RestDefinition>
if (binding.getProduces() != null) {
options.put("produces", parseText(camelContext,
binding.getProduces()));
}
- if (getClientRequestValidation() != null) {
- options.put("clientRequestValidation", parseBoolean(camelContext,
getClientRequestValidation()));
- } else if (clientValidation) {
+ if (clientRequestValidation) {
options.put("clientRequestValidation", "true");
}
+ if (clientResponseValidation) {
+ options.put("clientResponseValidation", "true");
+ }
if (openApi.getMissingOperation() != null) {
options.put("missingOperation", parseText(camelContext,
openApi.getMissingOperation()));
}
@@ -1097,7 +1107,9 @@ public class RestDefinition extends
OptionalIdentifiedDefinition<RestDefinition>
answer.add(route);
}
- private RestBindingDefinition getRestBindingDefinition(CamelContext
camelContext, String component) {
+ private RestBindingDefinition getRestBindingDefinition(
+ CamelContext camelContext, String component,
+ boolean clientRequestValidation, boolean clientResponseValidation)
{
String mode = getBindingMode();
if (mode == null) {
mode = camelContext.getRestConfiguration().getBindingMode().name();
@@ -1117,7 +1129,12 @@ public class RestDefinition extends
OptionalIdentifiedDefinition<RestDefinition>
}
binding.setBindingMode(mode);
binding.setSkipBindingOnErrorCode(getSkipBindingOnErrorCode());
- binding.setClientRequestValidation(getClientRequestValidation());
+ if (clientRequestValidation) {
+ binding.setClientRequestValidation("true");
+ }
+ if (clientResponseValidation) {
+ binding.setClientResponseValidation("true");
+ }
binding.setEnableCORS(getEnableCORS());
binding.setEnableNoContentResponse(getEnableNoContentResponse());
return binding;
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/RestComponentHelper.java
b/core/camel-support/src/main/java/org/apache/camel/support/RestComponentHelper.java
index 48be3701291..fc5c7ea3601 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/RestComponentHelper.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/RestComponentHelper.java
@@ -16,7 +16,6 @@
*/
package org.apache.camel.support;
-import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Locale;
@@ -94,12 +93,11 @@ public final class RestComponentHelper {
*
* Creates the Rest consumers url based on component and url options.
*
- * @param componentName the name of the rest component
- * @param verb the HTTP verb
- * @param path the HTTP path of the route
- * @param queryMap the endpoint query options
- * @return a string of the component route url
- * @throws URISyntaxException - is thrown if uri has invalid syntax.
+ * @param componentName the name of the rest component
+ * @param verb the HTTP verb
+ * @param path the HTTP path of the route
+ * @param queryMap the endpoint query options
+ * @return a string of the component route url
*/
public static String createRestConsumerUrl(String componentName, String
verb, String path, Map<String, Object> queryMap) {
String query = URISupport.createQueryString(queryMap);
@@ -110,11 +108,10 @@ public final class RestComponentHelper {
*
* Creates the Rest consumers url based on component and url options.
*
- * @param componentName the name of the rest component
- * @param path the HTTP path of the route
- * @param queryMap the endpoint query options
- * @return a string of the component route url
- * @throws URISyntaxException - is thrown if uri has invalid syntax.
+ * @param componentName the name of the rest component
+ * @param path the HTTP path of the route
+ * @param queryMap the endpoint query options
+ * @return a string of the component route url
*/
public static String createRestConsumerUrl(String componentName, String
path, Map<String, Object> queryMap) {
String query = URISupport.createQueryString(queryMap);
@@ -125,20 +122,17 @@ public final class RestComponentHelper {
*
* Creates the Rest consumers url based on component and url options.
*
- * @param componentName the name of the rest component
- * @param scheme the scheme of the HTTP route http/https
- * @param host the host of the HTTP route
- * @param port the port the route will be exposed through
- * @param path the HTTP path of the route
- * @param queryMap the endpoint query options
- * @return a string of the component route url
- * @throws URISyntaxException - is thrown if uri has invalid syntax.
+ * @param componentName the name of the rest component
+ * @param scheme the scheme of the HTTP route http/https
+ * @param host the host of the HTTP route
+ * @param port the port the route will be exposed through
+ * @param path the HTTP path of the route
+ * @param queryMap the endpoint query options
+ * @return a string of the component route url
*/
public static String createRestConsumerUrl(
String componentName, String scheme, String host, int port, String
path, Map<String, Object> queryMap) {
-
String query = URISupport.createQueryString(queryMap);
-
return applyFormatAndQuery("%s:%s://%s:%s/%s", query, componentName,
scheme, host, port, path);
}
@@ -146,7 +140,6 @@ public final class RestComponentHelper {
final String initial = String.format(format, formatOptions);
// get the endpoint
StringBuilder urlBuilder = new StringBuilder(initial.length() +
query.length() + 1);
-
urlBuilder.append(initial);
if (!query.isEmpty()) {
urlBuilder.append("?");
@@ -154,4 +147,5 @@ public final class RestComponentHelper {
}
return urlBuilder.toString();
}
+
}