This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-3.7.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.7.x by this push: new 59e6848 CAMEL-16114: Fixed jackson dataformat unmarshal type for arrays. Thanks to Bob Paulin for unit test. 59e6848 is described below commit 59e684818b70fd87cee1104e3ee22dcb90b8b6bf Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Jan 30 12:05:03 2021 +0100 CAMEL-16114: Fixed jackson dataformat unmarshal type for arrays. Thanks to Bob Paulin for unit test. --- .../jackson/JacksonMarshalUnmarshalArrayTest.java | 61 ++++++++++++++++++++++ .../reifier/dataformat/DataFormatReifier.java | 16 ++++++ .../reifier/dataformat/JsonDataFormatReifier.java | 2 +- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonMarshalUnmarshalArrayTest.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonMarshalUnmarshalArrayTest.java new file mode 100644 index 0000000..0055d1a --- /dev/null +++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonMarshalUnmarshalArrayTest.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.component.jackson; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.dataformat.JsonLibrary; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class JacksonMarshalUnmarshalArrayTest extends CamelTestSupport { + + @Test + public void testUnmarshalArray() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:endArray"); + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(String[].class); + + String json = "[\"Camel\", \"World\"]"; + template.sendBody("direct:beginArray", json); + + assertMockEndpointsSatisfied(); + + String[] array = mock.getReceivedExchanges().get(0).getIn().getBody(String[].class); + assertNotNull(array); + assertEquals(2, array.length); + + String string = array[0]; + assertEquals("Camel", string); + string = array[1]; + assertEquals("World", string); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:beginArray").unmarshal().json(JsonLibrary.Jackson, String[].class).to("mock:endArray"); + } + }; + } + +} diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/DataFormatReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/DataFormatReifier.java index cb995f9..e10f28e 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/DataFormatReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/DataFormatReifier.java @@ -77,6 +77,7 @@ import org.apache.camel.spi.ReifierStrategy; import org.apache.camel.support.CamelContextHelper; import org.apache.camel.support.PropertyBindingSupport; import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.StringHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -346,4 +347,19 @@ public abstract class DataFormatReifier<T extends DataFormatDefinition> extends protected abstract void prepareDataFormatConfig(Map<String, Object> properties); + protected String asTypeName(Class<?> classType) { + String type; + if (!classType.isPrimitive()) { + if (classType.isArray()) { + type = StringHelper.between(classType.getName(), "[L", ";") + "[]"; + } else { + type = classType.getName(); + } + } else { + type = classType.getCanonicalName(); + } + + return type; + } + } diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/JsonDataFormatReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/JsonDataFormatReifier.java index 5796fb8..ab94db3 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/JsonDataFormatReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/dataformat/JsonDataFormatReifier.java @@ -43,7 +43,7 @@ public class JsonDataFormatReifier extends DataFormatReifier<JsonDataFormat> { } if (definition.getLibrary() != JsonLibrary.XStream) { if (definition.getUnmarshalType() != null) { - properties.put("unmarshalTypeName", definition.getUnmarshalType().getName()); + properties.put("unmarshalTypeName", asTypeName(definition.getUnmarshalType())); } else { properties.put("unmarshalTypeName", definition.getUnmarshalTypeName()); }