[
https://issues.apache.org/jira/browse/CAMEL-10349?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16376876#comment-16376876
]
ASF GitHub Bot commented on CAMEL-10349:
----------------------------------------
davsclaus closed pull request #1190: CAMEL-10349, javaScript setHeader
conversion fault
URL: https://github.com/apache/camel/pull/1190
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
b/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
index af2ba63f96a..96be8c68aca 100644
---
a/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
+++
b/components/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
@@ -32,6 +32,9 @@
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
+import jdk.nashorn.api.scripting.NashornScriptEngine;
+import jdk.nashorn.api.scripting.ScriptObjectMirror;
+
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
@@ -481,6 +484,13 @@ protected Object runScript(ScriptEngine engine, Exchange
exchange, ScriptContext
if (result == null) {
result = engine.get("result");
}
+
+ // into jdk8 a list will be mapped to ScriptObjectMirror
+ if (engine instanceof NashornScriptEngine && result != null && result
instanceof ScriptObjectMirror) {
+ if (((ScriptObjectMirror)result).isArray()) {
+ result = ((ScriptObjectMirror)result).values();
+ }
+ }
return result;
}
diff --git
a/components/camel-script/src/test/java/org/apache/camel/builder/script/JavaScriptArrayExpressionTest.java
b/components/camel-script/src/test/java/org/apache/camel/builder/script/JavaScriptArrayExpressionTest.java
new file mode 100644
index 00000000000..2ee8e93c2e7
--- /dev/null
+++
b/components/camel-script/src/test/java/org/apache/camel/builder/script/JavaScriptArrayExpressionTest.java
@@ -0,0 +1,58 @@
+/**
+ * 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.builder.script;
+
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * Tests cast routing expression using JavaScript
+ */
+public class JavaScriptArrayExpressionTest extends CamelTestSupport {
+
+ @EndpointInject(uri = "mock:result")
+ MockEndpoint resultEndpoint;
+
+ private static final String HEADER_NAME = "myHeader";
+
+ @Test
+ public void testSendMatchingMessage() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ public void configure() throws Exception {
+ from("direct:start").setHeader(HEADER_NAME).javaScript("new
Array()").to(resultEndpoint);
+ }
+ });
+
+ template.sendBody("direct:start", "BODY");
+
+ assertNotEquals(resultEndpoint.getExchanges().size(), 0);
+
+ Object header =
resultEndpoint.getExchanges().get(0).getIn().getHeader(HEADER_NAME);
+
+ log.info("Check header instance: {}",
header.getClass().getCanonicalName());
+
+ assertTrue(header instanceof List);
+
+ assertMockEndpointsSatisfied(10, TimeUnit.SECONDS);
+ }
+}
diff --git
a/components/camel-script/src/test/java/org/apache/camel/builder/script/JavaScriptListWithMapExpressionTest.java
b/components/camel-script/src/test/java/org/apache/camel/builder/script/JavaScriptListWithMapExpressionTest.java
new file mode 100644
index 00000000000..fba56e9904f
--- /dev/null
+++
b/components/camel-script/src/test/java/org/apache/camel/builder/script/JavaScriptListWithMapExpressionTest.java
@@ -0,0 +1,61 @@
+/**
+ * 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.builder.script;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * Tests cast routing expression using JavaScript
+ */
+public class JavaScriptListWithMapExpressionTest extends CamelTestSupport {
+
+ @EndpointInject(uri = "mock:result")
+ MockEndpoint resultEndpoint;
+
+ private static final String HEADER_NAME = "myHeader";
+
+ @Test
+ public void testSendMatchingMessage() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ public void configure() throws Exception {
+ from("direct:start").setHeader(HEADER_NAME).javaScript("[
{\"firstField\": \"firstValue\", \"secondField\":\"secondValue\"}
]").to(resultEndpoint);
+ }
+ });
+
+ template.sendBody("direct:start", "BODY");
+
+ assertNotEquals(resultEndpoint.getExchanges().size(), 0);
+
+ Object header =
resultEndpoint.getExchanges().get(0).getIn().getHeader(HEADER_NAME);
+
+ log.info("Check header instance: {}",
header.getClass().getCanonicalName());
+
+ assertTrue(header instanceof List);
+ assertNotEquals(((List)header).size(), 0);
+ assertTrue(((List)header).get(0) instanceof Map);
+
+ assertMockEndpointsSatisfied(10, TimeUnit.SECONDS);
+ }
+}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> javaScript setHeader conversion fault
> -------------------------------------
>
> Key: CAMEL-10349
> URL: https://issues.apache.org/jira/browse/CAMEL-10349
> Project: Camel
> Issue Type: Bug
> Components: camel-script, karaf
> Affects Versions: 2.18.0
> Reporter: Fabrizio Spataro
> Assignee: Fabrizio Spataro
> Priority: Minor
>
> I may have found a bug derived from 2.18
> Before 2.18, i can specified an header like:
> {code}
> <setHeader headerName="myHeader">
> <javaScript>[ {"firstField": "firstValue", "secondField":"secondValue"}
> ]</javaScript>
> </setHeader>
> {code}
> I pass it into a method inside my bean. It was cast to
> ArrayList<HashMap<String,Object>>
> After 2.18 upgrade i had an error
> 'java.lang.ClassCastException: jdk.nashorn.api.scripting.ScriptObjectMirror
> cannot be cast to java.util.List'
> Can you help me? Is it a camel bug or an jdk8 issue?
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)