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 8b3d8eb105b CAMEL-22297: fixed jsonata component to support json array
inputs (#18777)
8b3d8eb105b is described below
commit 8b3d8eb105b60ca1e2addb373e70c9a58d06db37
Author: Luis Sergio Faria Carneiro <[email protected]>
AuthorDate: Wed Jul 30 15:18:58 2025 -0300
CAMEL-22297: fixed jsonata component to support json array inputs (#18777)
---
.../camel/component/jsonata/JsonataEndpoint.java | 10 +--
.../component/jsonata/JsonataArrayInputTest.java | 91 ++++++++++++++++++++++
.../component/jsonata/arrayinput/expressions.spec | 3 +
.../camel/component/jsonata/arrayinput/input.json | 1 +
.../camel/component/jsonata/arrayinput/output.json | 1 +
5 files changed, 99 insertions(+), 7 deletions(-)
diff --git
a/components/camel-jsonata/src/main/java/org/apache/camel/component/jsonata/JsonataEndpoint.java
b/components/camel-jsonata/src/main/java/org/apache/camel/component/jsonata/JsonataEndpoint.java
index 1631ea8c4d3..03e05b49a69 100644
---
a/components/camel-jsonata/src/main/java/org/apache/camel/component/jsonata/JsonataEndpoint.java
+++
b/components/camel-jsonata/src/main/java/org/apache/camel/component/jsonata/JsonataEndpoint.java
@@ -21,11 +21,9 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
-import java.util.Map;
import java.util.stream.Collectors;
import com.dashjoin.jsonata.Jsonata;
-import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.camel.Category;
import org.apache.camel.Exchange;
@@ -146,14 +144,12 @@ public class JsonataEndpoint extends ResourceEndpoint {
String path = getResourceUri();
ObjectHelper.notNull(path, "resourceUri");
- Map<String, Object> input;
+ Object input;
if (getInputType() == JsonataInputOutputType.JsonString) {
InputStream inputStream =
exchange.getIn().getBody(InputStream.class);
- input = mapper.readValue(inputStream, new TypeReference<>() {
- });
+ input = mapper.readValue(inputStream, Object.class);
} else {
- input = mapper.convertValue(exchange.getIn().getBody(), new
TypeReference<>() {
- });
+ input = mapper.convertValue(exchange.getIn().getBody(),
Object.class);
}
InputStream is = getResourceAsInputStream();
diff --git
a/components/camel-jsonata/src/test/java/org/apache/camel/component/jsonata/JsonataArrayInputTest.java
b/components/camel-jsonata/src/test/java/org/apache/camel/component/jsonata/JsonataArrayInputTest.java
new file mode 100644
index 00000000000..54388a4310c
--- /dev/null
+++
b/components/camel-jsonata/src/test/java/org/apache/camel/component/jsonata/JsonataArrayInputTest.java
@@ -0,0 +1,91 @@
+/*
+ * 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.jsonata;
+
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.support.ResourceHelper;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.util.IOHelper;
+import org.junit.jupiter.api.Test;
+
+class JsonataArrayInputTest extends CamelTestSupport {
+
+ @Test
+ void testJsonArrayInputAsString() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived(
+ IOHelper.loadText(
+ ResourceHelper.resolveMandatoryResourceAsInputStream(
+ context,
"org/apache/camel/component/jsonata/arrayinput/output.json"))
+ .trim() // Remove the last newline added by
IOHelper.loadText()
+ );
+
+ sendBody("direct://start-string",
+ ResourceHelper.resolveMandatoryResourceAsInputStream(
+ context,
"org/apache/camel/component/jsonata/arrayinput/input.json"));
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ @Test
+ void testJsonArrayInputAsJacksonNode() throws Exception {
+ InputStream inputJson =
ResourceHelper.resolveMandatoryResourceAsInputStream(
+ context,
"org/apache/camel/component/jsonata/arrayinput/input.json");
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode body = mapper.readTree(inputJson);
+ inputJson.close();
+
+ getMockEndpoint("mock:result").expectedBodiesReceived(
+ IOHelper.loadText(
+ ResourceHelper.resolveMandatoryResourceAsInputStream(
+ context,
"org/apache/camel/component/jsonata/arrayinput/output.json"))
+ .trim() // Remove the last newline added by
IOHelper.loadText()
+ );
+
+ sendBody("direct://start-jackson", body);
+
+ MockEndpoint.assertIsSatisfied(context);
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ final Processor processor = exchange -> {
+ Map<String, String> contextMap = new HashMap<>();
+ exchange.getIn().setHeader(JsonataConstants.JSONATA_CONTEXT,
contextMap);
+ };
+
+ return new RouteBuilder() {
+ public void configure() {
+ from("direct://start-string")
+ .process(processor)
+
.to("jsonata:org/apache/camel/component/jsonata/arrayinput/expressions.spec?inputType=JsonString&outputType=JsonString")
+ .to("mock:result");
+ from("direct://start-jackson")
+ .process(processor)
+
.to("jsonata:org/apache/camel/component/jsonata/arrayinput/expressions.spec?inputType=Jackson&outputType=JsonString")
+ .to("mock:result");
+ }
+ };
+ }
+}
diff --git
a/components/camel-jsonata/src/test/resources/org/apache/camel/component/jsonata/arrayinput/expressions.spec
b/components/camel-jsonata/src/test/resources/org/apache/camel/component/jsonata/arrayinput/expressions.spec
new file mode 100644
index 00000000000..e62f9e6137b
--- /dev/null
+++
b/components/camel-jsonata/src/test/resources/org/apache/camel/component/jsonata/arrayinput/expressions.spec
@@ -0,0 +1,3 @@
+$.{
+ "double": 2 * $
+}
\ No newline at end of file
diff --git
a/components/camel-jsonata/src/test/resources/org/apache/camel/component/jsonata/arrayinput/input.json
b/components/camel-jsonata/src/test/resources/org/apache/camel/component/jsonata/arrayinput/input.json
new file mode 100644
index 00000000000..3a26a2e5e94
--- /dev/null
+++
b/components/camel-jsonata/src/test/resources/org/apache/camel/component/jsonata/arrayinput/input.json
@@ -0,0 +1 @@
+[1,2,3]
\ No newline at end of file
diff --git
a/components/camel-jsonata/src/test/resources/org/apache/camel/component/jsonata/arrayinput/output.json
b/components/camel-jsonata/src/test/resources/org/apache/camel/component/jsonata/arrayinput/output.json
new file mode 100644
index 00000000000..dfa406e3e49
--- /dev/null
+++
b/components/camel-jsonata/src/test/resources/org/apache/camel/component/jsonata/arrayinput/output.json
@@ -0,0 +1 @@
+[{"double":2},{"double":4},{"double":6}]
\ No newline at end of file