From 7771d7bb844fdc7a3e6132a3d5b141c379a811e4 Mon Sep 17 00:00:00 2001 From: Noble Paul Date: Tue, 5 Mar 2019 06:11:50 +1100 Subject: [PATCH] SOLR-13285: Updates with enum fields and javabin cause ClassCastException --- solr/CHANGES.txt | 2 ++ .../apache/solr/common/util/JavaBinCodec.java | 4 +++- .../solr/common/util/TestJavaBinCodec.java | 22 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index e67a9d15201..29a41fdaed8 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -92,6 +92,8 @@ Bug Fixes * SOLR-9882: 500 error code on breaching timeAllowed by core and distributed (fsv) search, old and json facets (Mikhail Khludnev) +* SOLR-13285: Updates with enum fields and javabin cause ClassCastException (noble) + Improvements ---------------------- * SOLR-12999: Index replication could delete segments before downloading segments from master if there is not enough diff --git a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java index 4295d25148b..434e2f90d9f 100644 --- a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java +++ b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java @@ -53,6 +53,8 @@ import org.noggit.CharArr; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.apache.solr.common.util.ByteArrayUtf8CharSequence.convertCharSeq; + /** * Defines a space-efficient serialization/deserialization format for transferring data. *

@@ -818,7 +820,7 @@ public class JavaBinCodec implements PushWriter { */ public EnumFieldValue readEnumFieldValue(DataInputInputStream dis) throws IOException { Integer intValue = (Integer) readVal(dis); - String stringValue = (String) readVal(dis); + String stringValue = (String) convertCharSeq (readVal(dis)); return new EnumFieldValue(intValue, stringValue); } diff --git a/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java b/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java index 671737528ed..0e0ad88453e 100644 --- a/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java +++ b/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java @@ -65,6 +65,20 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 { } } + public void testReadAsCharSeq() throws Exception { + List types = new ArrayList<>(); + SolrInputDocument idoc = new SolrInputDocument(); + idoc.addField("foo", "bar"); + idoc.addField("foos", Arrays.asList("bar1","bar2")); + idoc.addField("enumf", new EnumFieldValue(1, "foo")); + types.add(idoc); + compareObjects( + (List) getObject(getBytes(types, true)), + (List) types + ); + + } + public static SolrDocument generateSolrDocumentWithChildDocs() { SolrDocument parentDocument = new SolrDocument(); parentDocument.addField("id", "1"); @@ -284,6 +298,14 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 { } } + private static byte[] getBytes(Object o, boolean readAsCharSeq) throws IOException { + try (JavaBinCodec javabin = new JavaBinCodec(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + javabin.readStringAsCharSeq = readAsCharSeq; + javabin.marshal(o, baos); + return baos.toByteArray(); + } + } + private static Object getObject(byte[] bytes) throws IOException { try (JavaBinCodec jbc = new JavaBinCodec()) { return jbc.unmarshal(new ByteArrayInputStream(bytes));