From e31c8e54b4cf7c459dba26f06be069ef07cd1d33 Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Fri, 6 Oct 2023 14:49:35 +0200 Subject: [PATCH] Remove quotes enclosing fields when using TAB separator (#1903) --- app/transformation/CsvExport.java | 4 ++-- test/transformation/CsvExportTest.java | 26 ++++++++++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/app/transformation/CsvExport.java b/app/transformation/CsvExport.java index b1b4acc..aaa9f10 100644 --- a/app/transformation/CsvExport.java +++ b/app/transformation/CsvExport.java @@ -54,8 +54,8 @@ public String of(final String fields, final String separator) { try { Object value = JsonPath.read(Configuration.defaultConfiguration() .jsonProvider().parse(org.toString()), "$." + field); - return String.format("\"%s\"", - value.toString().replaceAll("\"", "\"\"")); + return separator==DEFAULT_SEPARATOR ? String.format("\"%s\"", + value.toString().replaceAll("\"", "\"\"")) : value.toString(); } catch (PathNotFoundException x) { Logger.trace(x.getMessage()); diff --git a/test/transformation/CsvExportTest.java b/test/transformation/CsvExportTest.java index b31892d..bbbd312 100644 --- a/test/transformation/CsvExportTest.java +++ b/test/transformation/CsvExportTest.java @@ -25,6 +25,16 @@ public void testFlatFieldsTabulatorSeparator() { testFlatFields(CsvExport.TAB_SEPARATOR); } + @Test + public void testNestedFieldsDefaultSeparator() { + testNestedFields(CsvExport.DEFAULT_SEPARATOR); + } + + @Test + public void testNestedFieldsTabulatorSeparator() { + testNestedFields(CsvExport.TAB_SEPARATOR); + } + private void testFlatFields(final String sep) { ObjectNode node1 = Json.newObject(); node1.put("field1", "org1-value1"); @@ -40,11 +50,13 @@ private void testFlatFields(final String sep) { "field1", "field3", // "\"org1-value1\"", "\"org1-value3\"", // "\"org2-value1\"", "\"org2-value3\""); + if (sep.equals(CsvExport.TAB_SEPARATOR)) { + expected=expected.replaceAll("\"",""); + } assertThat(export.of("field1" + sep + "field3", sep)).isEqualTo(expected); } - @Test - public void testNestedFields() { + private void testNestedFields(final String sep) { ObjectNode org1 = Json.newObject(); ObjectNode sub1 = Json.newObject(); org1.put("field1", "org1-value1"); @@ -63,15 +75,17 @@ public void testNestedFields() { sub2.put("field3", "org2-sub3"); List orgs = Arrays.asList(org1, org2); CsvExport export = new CsvExport(Json.stringify(Json.toJson(orgs))); - String expected = String.format("%s,%s\n%s,%s\n%s,%s\n", // + String expected = String.format("%s" + sep + "%s\n%s" + sep + "%s\n%s" + sep + "%s\n", // "field1", "field3.field2", // "\"org1-value1\"", "\"org1-sub2\"", // "\"org2-value1\"", "\"org2-sub2\""); - assertThat(export.of("field1,field3.field2")).isEqualTo(expected); + if (sep.equals(CsvExport.TAB_SEPARATOR)) { + expected=expected.replaceAll("\"",""); + } + assertThat(export.of("field1" + sep + "field3.field2", sep)).isEqualTo(expected); } - @Test - public void testMissingField() { + private void testMissingField() { ObjectNode org = Json.newObject(); org.put("field1", "org1-value1"); org.put("field2", "org1-value2");