From d71fd08b71481ec6a3669e79f639f43051b88c13 Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Wed, 2 Dec 2009 14:28:38 +0000 Subject: [PATCH] SOLR-1615: fix StrParser quoted string backslash escaping, add java-style unicode escaping git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@886155 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 4 +++ .../org/apache/solr/search/QueryParsing.java | 24 +++++++++++++-- .../apache/solr/search/TestQueryTypes.java | 29 +++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 97bd7f73e70..143e07019d8 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -104,6 +104,10 @@ Bug Fixes * SOLR-1601: Schema browser does not indicate presence of charFilter. (koji) +* SOLR-1615: Backslash escaping did not work in quoted strings + for local param arguments. (Wojtek Piaseczny, yonik) + + Other Changes ---------------------- diff --git a/src/java/org/apache/solr/search/QueryParsing.java b/src/java/org/apache/solr/search/QueryParsing.java index a3afeb11556..2266e126f82 100644 --- a/src/java/org/apache/solr/search/QueryParsing.java +++ b/src/java/org/apache/solr/search/QueryParsing.java @@ -625,14 +625,32 @@ public class QueryParsing { } char ch = val.charAt(pos); if (ch=='\\') { - ch = pos=end) break; + ch = val.charAt(pos); + switch(ch) { + case 'n' : ch='\n'; break; + case 't' : ch='\t'; break; + case 'r' : ch='\r'; break; + case 'b' : ch='\b'; break; + case 'f' : ch='\f'; break; + case 'u' : + if (pos+4 >= end) { + throw new ParseException("bad unicode escape \\uxxxx at pos" + (val_start-1) + " str='"+val+"'"); + } + ch = (char)Integer.parseInt(val.substring(pos+1, pos+5), 16); + pos += 4; + break; + } + } else if (ch==delim) { + pos++; // skip over the quote + break; } sb.append(ch); pos++; } + + return sb.toString(); } // next non-whitespace char diff --git a/src/test/org/apache/solr/search/TestQueryTypes.java b/src/test/org/apache/solr/search/TestQueryTypes.java index 155cc47ee0d..b88aa556a60 100755 --- a/src/test/org/apache/solr/search/TestQueryTypes.java +++ b/src/test/org/apache/solr/search/TestQueryTypes.java @@ -46,6 +46,7 @@ public class TestQueryTypes extends AbstractSolrTestCase { assertU(adoc("id","6", "v_f","8983")); assertU(adoc("id","7", "v_f","1.5")); assertU(adoc("id","8", "v_ti","5")); + assertU(adoc("id","9", "v_s","internal\"quote")); Object[] arr = new Object[] { "id",999.0 @@ -125,6 +126,34 @@ public class TestQueryTypes extends AbstractSolrTestCase { ,"//result[@numFound='0']" ); + // + // test escapes in quoted strings + // + + // the control... unescaped queries looking for internal"quote + assertQ(req("q","{!raw f=v_s}internal\"quote") + ,"//result[@numFound='1']" + ); + + // test that single quoted string needs no escape + assertQ(req("q","{!raw f=v_s v='internal\"quote'}") + ,"//result[@numFound='1']" + ); + + // but it's OK if the escape is done + assertQ(req("q","{!raw f=v_s v='internal\\\"quote'}") + ,"//result[@numFound='1']" + ); + + // test unicode escape + assertQ(req("q","{!raw f=v_s v=\"internal\\u0022quote\"}") + ,"//result[@numFound='1']" + ); + + // inside a quoted string, internal"quote needs to be escaped + assertQ(req("q","{!raw f=v_s v=\"internal\\\"quote\"}") + ,"//result[@numFound='1']" + ); assertQ("test custom plugin query", req("q","{!foo f=v_t}hello")