[
https://issues.apache.org/jira/browse/CAMEL-18612?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17618830#comment-17618830
]
Claus Ibsen commented on CAMEL-18612:
-------------------------------------
I think the conversion to String is not correct as its not json string, but a
JDK toString from a List. So instead you can change test as follows
{code}
Object books =
getMockEndpoint("mock:books").getReceivedExchanges().get(0).getIn().getBody();
ObjectMapper objectMapper = new ObjectMapper();
// convert the result to valid Json string
String json = objectMapper.writeValueAsString(books);
// and convert json to pojo via jackson
Book[] result = objectMapper.readValue(json, Book[].class);
Book[] expected = new Book[] { new Book("programming", "Claus
Ibsen,Jonathan Anstey", "Camel in Action", "39.99", "978-193-518-236-8") };
assertArrayEquals(expected, result);
{code}
And in the route you do
{code}
<transform>
<jsonpath>$.store.book</jsonpath>
</transform>
{code}
Then its more correct
> Inconsistency in JsonPath component causes problems with databinding
> --------------------------------------------------------------------
>
> Key: CAMEL-18612
> URL: https://issues.apache.org/jira/browse/CAMEL-18612
> Project: Camel
> Issue Type: Bug
> Components: camel-jsonpath
> Reporter: Radovan Netuka
> Assignee: Radovan Netuka
> Priority: Major
> Fix For: 3.20.0
>
> Attachments: multiple-item-array.json, single-item-array.json
>
>
> Inconsistent treatment of jsonpath expression result causes problems with
> data binding. When jsonpath evaluates to an array element, this piece of code
> threats it as a single object, making it impossible to bind to an array of
> objects.
>
> {code:java}
> (singleElement && !resultIsCollection) {
> result = ((List) result).get(0);
> ...
> }{code}
>
>
> Steps to reproduce:
> # Create this Camel route
> {code:java}
> from("file:work/")
> .routeId("file-route")
> .to("direct:transform");
> from("direct:transform")
> .routeId("direct-transform")
> .streamCaching()
> .log("Before jsonpath transformation >>> ${body}")
> .setBody().jsonpath("$.d.results", String.class)
> .log("After jsonpath transformation >>> ${body}")
> .process(exchange -> {
> log.info("Before Jackson deserialization");
> String testResponse = exchange.getMessage().getBody(String.class);
> objectMapper.readValue(testResponse, TestResultsInfo[].class);
> log.info("After Jackson deserialization");
> })
> .to("mock:test");{code}
> # Use the _single-item-array.json_ file from the attachment
> # Try to bind the message body to these classes
> {code:java}
> @Data
> @NoArgsConstructor
> @JsonIgnoreProperties(ignoreUnknown = true)
> public static class TestResultsInfo {
> String resultText;
> @JsonProperty(value = "AddressInfo")
> TestAddressInfo addressInfo;
> }
> @Data
> @NoArgsConstructor
> @JsonIgnoreProperties(ignoreUnknown = true)
> public static class TestAddressInfo {
> @JsonProperty(value = "City")
> String city;
> @JsonProperty(value = "State")
> String street;
> }{code}
> It will fail because it's not possible to bind it to _TestResultsInfo[]._ If
> you add a second element to the array (or use _multiple-item-array.json_ file
> instead), it will work fine.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)