[ 
https://issues.apache.org/jira/browse/CAMEL-18612?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17618927#comment-17618927
 ] 

Radovan Netuka commented on CAMEL-18612:
----------------------------------------

[~davsclaus] Also, I think the following code might also fix the issue:
{code:java}
boolean resultIsCollection = Collection.class.isAssignableFrom(this.resultType);
boolean singleElement = result instanceof List && ((List)result).size() == 1;
if (singleElement && !resultIsCollection && !(resultType==String.class)) {
    result = ((List)result).get(0);
    LOG.trace("Unwrapping result: {} from single element List before converting 
to: {}", result, this.resultType);
} {code}
Note the added {*}&& !(resultType==String.class){*}. Just tell me, if you 
prefer this solution.

 

I still think the code above smells. A single-element list is still a list and 
shouldn't be returned as a member of itself.

> 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.18.3, 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)

Reply via email to