Apache Solr open-source search software
SOLR-3959: escape the internal comma separator of poly fields
Robert Muir61851c096653
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex a2de0c7ea0f..98c9b2ed095 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -207,6 +207,8 @@ Bug Fixes(used when cache=false), spatial queries, and frange queires.(Eirik Lygre, yonik)+* SOLR-3959: Ensure the internal comma separator of poly fields is escaped+ for CSVResponseWriter. (Areek Zillur via Robert Muir)Other Changes----------------------diff --git a/solr/core/src/java/org/apache/solr/response/CSVResponseWriter.java b/solr/core/src/java/org/apache/solr/response/CSVResponseWriter.javaindex eda4f5c6aa4..38fc58e52ac 100755--- a/solr/core/src/java/org/apache/solr/response/CSVResponseWriter.java+++ b/solr/core/src/java/org/apache/solr/response/CSVResponseWriter.java@@ -202,7 +202,6 @@ class CSVWriter extends TextResponseWriter {// be escaped.strat.setUnicodeEscapeInterpretation(true);}-printer = new CSVPrinter(writer, strategy);@@ -261,7 +260,6 @@ class CSVWriter extends TextResponseWriter {if (!returnFields.wantsField(field)) {continue;}-if (field.equals("score")) {CSVField csvField = new CSVField();csvField.name = "score";@@ -284,6 +282,11 @@ class CSVWriter extends TextResponseWriter {sep = params.get("f." + field + '.' + CSV_SEPARATOR);encapsulator = params.get("f." + field + '.' + CSV_ENCAPSULATOR);escape = params.get("f." + field + '.' + CSV_ESCAPE);++ // if polyfield and no escape is provided, add "\\" escape by default+ if (sf.isPolyField()) {+ escape = (escape==null)?"\\":escape;+ }CSVSharedBufPrinter csvPrinter = csvPrinterMV;if (sep != null || encapsulator != null || escape != null) {@@ -310,7 +313,6 @@ class CSVWriter extends TextResponseWriter {csvPrinter = new CSVSharedBufPrinter(mvWriter, strat);}-CSVField csvField = new CSVField();csvField.name = field;csvField.sf = sf;@@ -407,7 +409,19 @@ class CSVWriter extends TextResponseWriter {Collection values = (Collection)val;val = values.iterator().next();}- writeVal(csvField.name, val);+ // if field is polyfield, use the multi-valued printer to apply appropriate escaping+ if (csvField.sf != null && csvField.sf.isPolyField()) {+ mvWriter.reset();+ csvField.mvPrinter.reset();+ CSVPrinter tmp = printer;+ printer = csvField.mvPrinter;+ writeVal(csvField.name, val);+ printer = tmp;+ mvWriter.freeze();+ printer.print(mvWriter.getFrozenBuf(), 0, mvWriter.getFrozenSize(), true);+ } else {+ writeVal(csvField.name, val);+ }}}diff --git a/solr/core/src/java/org/apache/solr/schema/CurrencyField.java b/solr/core/src/java/org/apache/solr/schema/CurrencyField.javaindex d0324f0c4dc..401d5317c30 100644--- a/solr/core/src/java/org/apache/solr/schema/CurrencyField.java+++ b/solr/core/src/java/org/apache/solr/schema/CurrencyField.java@@ -250,7 +250,7 @@ public class CurrencyField extends FieldType implements SchemaAware, ResourceLoa@Overridepublic void write(TextResponseWriter writer, String name, StorableField field) throws IOException {- writer.writeStr(name, field.stringValue(), false);+ writer.writeStr(name, field.stringValue(), true);}public ExchangeRateProvider getProvider() {diff --git a/solr/core/src/java/org/apache/solr/schema/LatLonType.java b/solr/core/src/java/org/apache/solr/schema/LatLonType.javaindex bf29758b08b..d9fe7393677 100644--- a/solr/core/src/java/org/apache/solr/schema/LatLonType.java+++ b/solr/core/src/java/org/apache/solr/schema/LatLonType.java@@ -241,7 +241,7 @@ public class LatLonType extends AbstractSubTypeFieldType implements SpatialQuery@Overridepublic void write(TextResponseWriter writer, String name, StorableField f) throws IOException {- writer.writeStr(name, f.stringValue(), false);+ writer.writeStr(name, f.stringValue(), true);}@Overridediff --git a/solr/core/src/java/org/apache/solr/schema/PointType.java b/solr/core/src/java/org/apache/solr/schema/PointType.javaindex d874dc7ad16..9cf87cbf636 100644--- a/solr/core/src/java/org/apache/solr/schema/PointType.java+++ b/solr/core/src/java/org/apache/solr/schema/PointType.java@@ -121,7 +121,7 @@ public class PointType extends CoordinateFieldType implements SpatialQueryable {@Overridepublic void write(TextResponseWriter writer, String name, StorableField f) throws IOException {- writer.writeStr(name, f.stringValue(), false);+ writer.writeStr(name, f.stringValue(), true);}@Overridediff --git a/solr/core/src/test-files/solr/collection1/conf/schema12.xml b/solr/core/src/test-files/solr/collection1/conf/schema12.xmlindex 85ee29a0583..57eeb311c5e 100755--- a/solr/core/src/test-files/solr/collection1/conf/schema12.xml+++ b/solr/core/src/test-files/solr/collection1/conf/schema12.xml@@ -428,6 +428,7 @@<!-- A specialized field for geospatial search. If indexed, this fieldType must not be multivalued. --><fieldType name="location" class="solr.LatLonType" subFieldSuffix="_coordinate"/>+ <fieldType name="currency" class="solr.CurrencyField" currencyConfig="currency.xml" multiValued="false" /></types>@@ -524,6 +525,8 @@<field name="point_hash" type="geohash" indexed="true" stored="true" multiValued="false"/><field name="store" type="location" indexed="true" stored="true"/>+ <!-- Test currency field -->+ <field name="amount_c" type="currency" indexed="true" stored="true" multiValued="false"/><!-- to test uniq fields --><field name="uniq" type="string" indexed="true" stored="true" multiValued="true"/><field name="uniq2" type="string" indexed="true" stored="true" multiValued="true"/>diff --git a/solr/core/src/test/org/apache/solr/response/TestCSVResponseWriter.java b/solr/core/src/test/org/apache/solr/response/TestCSVResponseWriter.javaindex 20480c1ff8e..0db44b24aa9 100644--- a/solr/core/src/test/org/apache/solr/response/TestCSVResponseWriter.java+++ b/solr/core/src/test/org/apache/solr/response/TestCSVResponseWriter.java@@ -39,6 +39,8 @@ public class TestCSVResponseWriter extends SolrTestCaseJ4 {assertU(adoc("id","1", "foo_i","-1", "foo_s","hi", "foo_l","12345678987654321", "foo_b","false", "foo_f","1.414","foo_d","-1.0E300","foo_dt","2000-01-02T03:04:05Z"));assertU(adoc("id","2", "v_ss","hi", "v_ss","there", "v2_ss","nice", "v2_ss","output", "shouldbeunstored","foo"));assertU(adoc("id","3", "shouldbeunstored","foo"));+ assertU(adoc("id","4", "amount_c", "1.50,EUR"));+ assertU(adoc("id","5", "store", "12.434,-134.1"));assertU(commit());}@@ -69,6 +71,13 @@ public class TestCSVResponseWriter extends SolrTestCaseJ4 {assertEquals("2,hi|there,nice:output\n", h.query(req("q","id:2", "wt","csv", "csv.header","false", "csv.mv.separator","|", "f.v2_ss.csv.separator",":", "fl","id,v_ss,v2_ss")));+ // test csv field for polyfield (currency) SOLR-3959+ assertEquals("4,\"1.50\\,EUR\"\n"+ , h.query(req("q","id:4", "wt","csv", "csv.header","false", "fl","id,amount_c")));++ // test csv field for polyfield (latlon) SOLR-3959+ assertEquals("5,\"12.434\\,-134.1\"\n"+ , h.query(req("q","id:5", "wt","csv", "csv.header","false", "fl","id,store")) );// test retrieving fields from indexString result = h.query(req("q","*:*", "wt","csv", "csv.header","true", "fl","*,score"));for (String field : "id,foo_s,foo_i,foo_l,foo_b,foo_f,foo_d,foo_dt,v_ss,v2_ss,score".split(",")) {@@ -100,7 +109,7 @@ public class TestCSVResponseWriter extends SolrTestCaseJ4 {, h.query(req("q","id:[1 TO 2]", "wt","csv", "csv.header","false", "fl","id,v_ss,foo_s")));// test SOLR-2970 not returning non-stored fields by default. Compare sorted list- assertEquals(sortHeader("v_ss,foo_b,v2_ss,foo_f,foo_i,foo_d,foo_s,foo_dt,id,foo_l\n")+ assertEquals(sortHeader("amount_c,store,v_ss,foo_b,v2_ss,foo_f,foo_i,foo_d,foo_s,foo_dt,id,foo_l\n"), sortHeader(h.query(req("q","id:3", "wt","csv", "csv.header","true", "fl","*", "rows","0"))));