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

Bindy - Support regex expression as separator

    XMLWordPrintableJSON

    Details

    • Type: New Feature
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.20.2
    • Fix Version/s: 2.21.0
    • Component/s: camel-bindy
    • Labels:
      None
    • Estimated Complexity:
      Unknown

      Description

      I try to unmarshal CSV file into the class below:

      @CsvRecord(separator = "[,;]", skipFirstLine = true)
      public class Example {
          @DataField(pos = 1)
          private String field1;
      
          @DataField(pos = 2)
          private String field2;
      }

      CSV file:

      header1,header2
      "value_1","value,2"
      

      After unmarshalling I get the following value for field2: "value[,;]2".

      However, the correct value for field2 should be "value,2".

      Complete test:

      import org.apache.camel.builder.RouteBuilder;
      import org.apache.camel.component.mock.MockEndpoint;
      import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
      import org.apache.camel.dataformat.bindy.annotation.DataField;
      import org.apache.camel.model.dataformat.BindyType;
      import org.apache.camel.test.junit4.CamelTestSupport;
      import org.junit.Test;
      
      public class BindyTest extends CamelTestSupport {
          @CsvRecord(separator = "[,;]", skipFirstLine = true)
          public static class Example {
              @DataField(pos = 1)
              private String field1;
      
              @DataField(pos = 2)
              private String field2;
          }
      
          @Test
          public void testUnmarshal() throws Exception {
              MockEndpoint mock = getMockEndpoint("mock:result");
              mock.expectedMessageCount(1);
      
              template.sendBody("direct:unmarshal", "header1,header2\n\"value1\",\"value,2\"");
      
              assertMockEndpointsSatisfied();
              Example body = mock.getReceivedExchanges().get(0).getIn().getBody(Example.class);
              assertEquals("value,2", body.field2);
          }
      
          @Override
          protected RouteBuilder createRouteBuilder() throws Exception {
              return new RouteBuilder() {
                  @Override
                  public void configure() throws Exception {
                      from("direct:unmarshal")
                              .unmarshal().bindy(BindyType.Csv, Example.class)
                              .to("mock:result");
                  }
              };
          }
      }
      

      Possible workaround:
      Use separator

      [,;](?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)

      (regex explained in https://stackoverflow.com/a/18893443)

        Attachments

          Activity

            People

            • Assignee:
              dmvolod Dmitry Volodin
              Reporter:
              bgd Bohdan
            • Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: