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 c2a3341606e CAMEL-21339: rest-dsl - Fix clientRequestValidation to 
deal with empty body when content-type header has been sent for GET requests.
c2a3341606e is described below

commit c2a3341606e6adc4c0f7a19eb46c97a019148628
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Sat Oct 12 10:12:19 2024 +0200

    CAMEL-21339: rest-dsl - Fix clientRequestValidation to deal with empty body 
when content-type header has been sent for GET requests.
---
 .../component/jetty/rest/RestJettyAcceptTest.java    | 20 ++++++++++++++++++++
 .../processor/DefaultRestClientRequestValidator.java |  2 +-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git 
a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyAcceptTest.java
 
b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyAcceptTest.java
index 64b0280fe49..398180b5030 100644
--- 
a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyAcceptTest.java
+++ 
b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyAcceptTest.java
@@ -16,6 +16,11 @@
  */
 package org.apache.camel.component.jetty.rest;
 
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+
 import org.apache.camel.CamelExecutionException;
 import org.apache.camel.Exchange;
 import org.apache.camel.FluentProducerTemplate;
@@ -62,7 +67,18 @@ public class RestJettyAcceptTest extends BaseJettyTest {
         HttpOperationFailedException cause = 
assertIsInstanceOf(HttpOperationFailedException.class, ex.getCause());
         assertEquals(406, cause.getStatusCode());
         assertEquals("", cause.getResponseBody());
+    }
 
+    @Test
+    public void testGetContentTypeHeaderOk() throws Exception {
+        // use JDK client as camel-http will drop "Content-Type" header for GET
+        HttpRequest request = HttpRequest.newBuilder().header("Content-Type", 
"application/json")
+                .uri(URI.create("http://localhost:"; + getPort() + 
"/hello/scott"))
+                .GET().build();
+
+        HttpClient client = HttpClient.newBuilder().build();
+        HttpResponse<String> response = client.send(request, 
HttpResponse.BodyHandlers.ofString());
+        assertEquals(200, response.statusCode());
     }
 
     @Override
@@ -79,6 +95,10 @@ public class RestJettyAcceptTest extends BaseJettyTest {
                 
rest("/users/").post("{id}/update").consumes("application/json").produces("application/json").to("direct:update");
                 from("direct:update")
                         .setBody(constant("{ \"status\": \"ok\" }"));
+
+                
rest("/hello/").get("{id}").consumes("application/json").produces("application/json").to("direct:hello");
+                from("direct:hello")
+                        .setBody(simple("{ \"hello\": \"${header.id}\" }"));
             }
         };
     }
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultRestClientRequestValidator.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultRestClientRequestValidator.java
index 7494c8fc505..a9add96dfea 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultRestClientRequestValidator.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/DefaultRestClientRequestValidator.java
@@ -87,7 +87,7 @@ public class DefaultRestClientRequestValidator implements 
RestClientRequestValid
         // if content-type is json then lets validate the message body can be 
parsed to json
         if (body != null && contentType != null && 
isValidOrAcceptedContentType("application/json", contentType)) {
             String json = MessageHelper.extractBodyAsString(exchange.getIn());
-            if (json != null) {
+            if (!ObjectHelper.isEmpty(json)) {
                 try {
                     Jsoner.deserialize(json);
                 } catch (DeserializationException e) {

Reply via email to