This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-4.14.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.14.x by this push:
new 9ea09e6aca77 CAMEL-22874: camel-openapi-rest - Fix issue with error
handler invoked twice when using handled(false) for rest-dsl with openapi
contract-first CAMEL-22874: Added unit test (#21296) CAMEL-22874: Added unit
test
9ea09e6aca77 is described below
commit 9ea09e6aca77edfe8a14e438a9814eac615f254c
Author: Claus Ibsen <[email protected]>
AuthorDate: Sat Feb 7 12:01:15 2026 +0100
CAMEL-22874: camel-openapi-rest - Fix issue with error handler invoked
twice when using handled(false) for rest-dsl with openapi contract-first
CAMEL-22874: Added unit test (#21296)
CAMEL-22874: Added unit test
---
.../RestOpenApiCodeFirstOnExceptionIssueTest.java | 124 +++++++++++++++++++++
...stOpenApiContractFirstOnExceptionIssueTest.java | 116 +++++++++++++++++++
.../apache/camel/model/rest/RestDefinition.java | 5 +
3 files changed, 245 insertions(+)
diff --git
a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/RestOpenApiCodeFirstOnExceptionIssueTest.java
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/RestOpenApiCodeFirstOnExceptionIssueTest.java
new file mode 100644
index 000000000000..12976aa29846
--- /dev/null
+++
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/RestOpenApiCodeFirstOnExceptionIssueTest.java
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.platform.http.vertx;
+
+import io.restassured.RestAssured;
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static io.restassured.RestAssured.given;
+
+public class RestOpenApiCodeFirstOnExceptionIssueTest extends CamelTestSupport
{
+
+ @Override
+ protected boolean useJmx() {
+ return true;
+ }
+
+ @Test
+ public void testOnExceptionHandledTrue() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ onException(Exception.class)
+ .log("Error processing request: ${exception.message}")
+ .process(e -> {
+ log.info("onException: {}", e);
+ })
+ .to("mock:error")
+ .handled(true);
+
+ restConfiguration().contextPath("/api/v3");
+
+ rest()
+ .get("pet/{id}")
+ .to("direct:getPetById");
+
+ from("direct:getPetById").routeId("directRoute")
+ .process(e -> {
+ throw new RuntimeException("Simulated error get
pet");
+ });
+ }
+ });
+
+ getMockEndpoint("mock:error").expectedMessageCount(1);
+
+ given()
+ .when()
+ .get("/api/v3/pet/1")
+ .then()
+ .statusCode(204);
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ @Test
+ public void testOnExceptionHandledFalse() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ onException(Exception.class)
+ .log("Error processing request: ${exception.message}")
+ .process(e -> {
+ log.info("onException: {}", e);
+ })
+ .to("mock:error")
+ .handled(false);
+
+ restConfiguration().contextPath("/api/v3");
+
+ rest()
+ .get("pet/{id}")
+ .to("direct:getPetById");
+
+ from("direct:getPetById").routeId("directRoute")
+ .process(e -> {
+ throw new RuntimeException("Simulated error get
pet");
+ });
+ }
+ });
+
+ getMockEndpoint("mock:error").expectedMessageCount(1);
+
+ given()
+ .when()
+ .get("/api/v3/pet/1")
+ .then()
+ .statusCode(500);
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ @Override
+ public CamelContext createCamelContext() throws Exception {
+ int port = AvailablePortFinder.getNextAvailable();
+ VertxPlatformHttpServerConfiguration conf = new
VertxPlatformHttpServerConfiguration();
+ conf.setBindPort(port);
+
+ RestAssured.port = port;
+
+ CamelContext context = new DefaultCamelContext();
+ context.addService(new VertxPlatformHttpServer(conf));
+ return context;
+ }
+
+}
diff --git
a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/RestOpenApiContractFirstOnExceptionIssueTest.java
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/RestOpenApiContractFirstOnExceptionIssueTest.java
new file mode 100644
index 000000000000..97908f46dd71
--- /dev/null
+++
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/RestOpenApiContractFirstOnExceptionIssueTest.java
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.platform.http.vertx;
+
+import io.restassured.RestAssured;
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static io.restassured.RestAssured.given;
+
+public class RestOpenApiContractFirstOnExceptionIssueTest extends
CamelTestSupport {
+
+ @Override
+ protected boolean useJmx() {
+ return true;
+ }
+
+ @Test
+ public void testOnExceptionHandledTrue() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ onException(Exception.class)
+ .log("Error processing request: ${exception.message}")
+ .process(e -> {
+ log.info("onException: {}", e);
+ })
+ .to("mock:error")
+ .handled(true);
+
+
rest().openApi().specification("openapi-v3.json").missingOperation("ignore").routeId("petStore");
+
+ from("direct:getPetById").routeId("directRoute")
+ .process(e -> {
+ throw new RuntimeException("Simulated error get
pet");
+ });
+ }
+ });
+
+ getMockEndpoint("mock:error").expectedMessageCount(1);
+
+ given()
+ .when()
+ .get("/api/v3/pet/1")
+ .then()
+ .statusCode(204);
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ @Test
+ public void testOnExceptionHandledFalse() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ onException(Exception.class)
+ .log("Error processing request: ${exception.message}")
+ .process(e -> {
+ log.info("onException: {}", e);
+ })
+ .to("mock:error")
+ .handled(false);
+
+
rest().openApi().specification("openapi-v3.json").missingOperation("ignore").routeId("petStore");
+
+ from("direct:getPetById").routeId("directRoute")
+ .process(e -> {
+ throw new RuntimeException("Simulated error get
pet");
+ });
+ }
+ });
+
+ getMockEndpoint("mock:error").expectedMessageCount(1);
+
+ given()
+ .when()
+ .get("/api/v3/pet/1")
+ .then()
+ .statusCode(500);
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ @Override
+ public CamelContext createCamelContext() throws Exception {
+ int port = AvailablePortFinder.getNextAvailable();
+ VertxPlatformHttpServerConfiguration conf = new
VertxPlatformHttpServerConfiguration();
+ conf.setBindPort(port);
+
+ RestAssured.port = port;
+
+ CamelContext context = new DefaultCamelContext();
+ context.addService(new VertxPlatformHttpServer(conf));
+ return context;
+ }
+
+}
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 3dc8067608b1..3d8e78fa85e2 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
@@ -41,6 +41,7 @@ import org.apache.camel.model.OptionalIdentifiedDefinition;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.StopDefinition;
import org.apache.camel.model.ToDefinition;
+import org.apache.camel.model.errorhandler.NoErrorHandlerDefinition;
import org.apache.camel.spi.AsEndpointUri;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.NodeIdFactory;
@@ -1104,6 +1105,10 @@ public class RestDefinition extends
OptionalIdentifiedDefinition<RestDefinition>
// the route should be from this rest endpoint
route.fromRest(from);
+ // rest-dsl open-api contract-first is only a facade and each route
handles error handling
+ route.setErrorHandlerFactory(new NoErrorHandlerDefinition());
+ route.getInput().setLocation(getLocation());
+ route.getInput().setLineNumber(getLineNumber());
route.setRestDefinition(this);
answer.add(route);
}