You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Radovan Netuka (Jira)" <ji...@apache.org> on 2022/10/17 12:40:00 UTC

[jira] [Comment Edited] (CAMEL-18612) Inconsistency in JsonPath component causes problems with databinding

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

Radovan Netuka edited comment on CAMEL-18612 at 10/17/22 12:39 PM:
-------------------------------------------------------------------

[~davsclaus] "Why do you convert the result to a String when its an array type?" Because that's the use case that's failing.

 

The suggested code doesn't help. The value *json* variable still contains a value from LinkedHashMap#toString(). 

 

When combined with camel-databind, there is a _JacksonTypeConverters_ that kicks in and returns the json string. Unfortunately, it seems it can't be used in the unit test, since it's an independant component.

 

 


was (Author: rnetuka):
[~davsclaus] "Why do you convert the result to a String when its an array type?" Because that's the use case that's failing.

 

The suggested code doesn't help. The value *json* variable ** still contains a value from LinkedHashMap#toString(). 

 

When combined with camel-databind, there is a _JacksonTypeConverters_ that kicks in and returns the json string. Unfortunately, it seems it can't be used in the unit test, since it's an independant component.

 

 

> 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)