You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2024/10/12 08:14:38 UTC

(camel) branch camel-4.8.x updated: CAMEL-21339: rest-dsl - Fix clientRequestValidation to deal with empty body when content-type header has been sent for GET requests.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-4.8.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.8.x by this push:
     new 4971e9897eb CAMEL-21339: rest-dsl - Fix clientRequestValidation to deal with empty body when content-type header has been sent for GET requests.
4971e9897eb is described below

commit 4971e9897ebd673b88758487562b83335b80a98c
Author: Claus Ibsen <cl...@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) {