Uploaded image for project: 'Camel'
  1. Camel
  2. CAMEL-18612

Inconsistency in JsonPath component causes problems with databinding

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • 3.18.3, 3.20.0
    • camel-jsonpath
    • 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:

      1. 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");
      1. Use the single-item-array.json file from the attachment
      2. 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

        1. multiple-item-array.json
          0.3 kB
          Radovan Netuka
        2. single-item-array.json
          0.1 kB
          Radovan Netuka

        Issue Links

          Activity

            People

              rnetuka Radovan Netuka
              rnetuka Radovan Netuka
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: