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/14 12:53:00 UTC

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

Radovan Netuka created CAMEL-18612:
--------------------------------------

             Summary: 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


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)