Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Fixed
-
None
-
None
-
Unknown
Description
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.
(singleElement && !resultIsCollection) { result = ((List) result).get(0); ... }
Steps to reproduce:
- Create this Camel route
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");
- Use the single-item-array.json file from the attachment
- Try to bind the message body to these classes
@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; }
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.
Attachments
Attachments
Issue Links
- relates to
-
CAMEL-11796 Add headerName option to jsonpath expression
- Resolved
- links to
Why do you convert the result to a String when its an array type? When you convert to String then the JDK will convert the List to a string by calling toString, which does a string dump that happens to look like json but it is not.
In the PR unit test the result is:
[
{category=programming, author=Claus Ibsen,Jonathan Anstey, title=Camel in Action, price=39.99, isbn=978-193-518-236-8}]