This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-4.10.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.10.x by this push: new 192d17e0a3f CAMEL-22062: camel-jsonata: Using JsonNode for output when outputType is Jackson (#18043) 192d17e0a3f is described below commit 192d17e0a3faa635622b2eba6d4d99959d824b7b Author: Luis Sergio Faria Carneiro <luis.carne...@sensedia.com> AuthorDate: Wed May 14 02:34:44 2025 -0300 CAMEL-22062: camel-jsonata: Using JsonNode for output when outputType is Jackson (#18043) --- .../camel/component/jsonata/JsonataEndpoint.java | 16 +++-- .../jsonata/JsonataJacksonOutputTest.java | 70 ++++++++++++++++++++++ 2 files changed, 80 insertions(+), 6 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 a641906eced..77f90354a51 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 @@ -127,7 +127,6 @@ public class JsonataEndpoint extends ResourceEndpoint { }); } - Object output = null; Jsonata expression = null; try (InputStreamReader inputStreamReader = new InputStreamReader(getResourceAsInputStream(), StandardCharsets.UTF_8); @@ -139,15 +138,20 @@ public class JsonataEndpoint extends ResourceEndpoint { } Jsonata.Frame frame = expression.createFrame(); - if (frameBinding != null) + if (frameBinding != null) { frameBinding.bindToFrame(frame); - output = expression.evaluate(input, frame); + } + Object outputLib = expression.evaluate(input, frame); + String bodyAsString = mapper.writeValueAsString(outputLib); // now lets output the results to the exchange - Object body = output; + final Object output; if (getOutputType() == JsonataInputOutputType.JsonString) { - body = mapper.writeValueAsString(output); + output = bodyAsString; + } else { + output = mapper.readTree(bodyAsString); } - ExchangeHelper.setInOutBodyPatternAware(exchange, body); + ExchangeHelper.setInOutBodyPatternAware(exchange, output); } + } diff --git a/components/camel-jsonata/src/test/java/org/apache/camel/component/jsonata/JsonataJacksonOutputTest.java b/components/camel-jsonata/src/test/java/org/apache/camel/component/jsonata/JsonataJacksonOutputTest.java new file mode 100644 index 00000000000..038248a7408 --- /dev/null +++ b/components/camel-jsonata/src/test/java/org/apache/camel/component/jsonata/JsonataJacksonOutputTest.java @@ -0,0 +1,70 @@ +/* + * 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.util.HashMap; +import java.util.Map; + +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; + +/** + * Unit test based on the first sample test from the JSONata project. + */ +class JsonataJacksonOutputTest extends CamelTestSupport { + + @Test + void testFirstSampleJsonata() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived( + IOHelper.loadText( + ResourceHelper.resolveMandatoryResourceAsInputStream( + context, "org/apache/camel/component/jsonata/firstSample/output.json")) + .trim() // Remove the last newline added by IOHelper.loadText() + ); + + sendBody("direct://start", + ResourceHelper.resolveMandatoryResourceAsInputStream( + context, "org/apache/camel/component/jsonata/firstSample/input.json")); + + MockEndpoint.assertIsSatisfied(context); + } + + @Override + protected RouteBuilder createRouteBuilder() { + final Processor processor = exchange -> { + Map<String, String> contextMap = new HashMap<>(); + contextMap.put("contextB", "bb"); + + exchange.getIn().setHeader(JsonataConstants.JSONATA_CONTEXT, contextMap); + }; + + return new RouteBuilder() { + @Override + public void configure() { + from("direct://start") + .process(processor) + .to("jsonata:org/apache/camel/component/jsonata/firstSample/expressions.spec?inputType=JsonString&outputType=Jackson") + .to("mock:result"); + } + }; + } +}