You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by rs...@apache.org on 2022/09/01 19:14:26 UTC
[avro] branch master updated: AVRO-3597: Allow custom readers to override string creation (#1847)
This is an automated email from the ASF dual-hosted git repository.
rskraba pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new 0066daff4 AVRO-3597: Allow custom readers to override string creation (#1847)
0066daff4 is described below
commit 0066daff46b66c0bd28b5525ba3d2f32f26046e3
Author: clesaec <51...@users.noreply.github.com>
AuthorDate: Thu Sep 1 21:14:17 2022 +0200
AVRO-3597: Allow custom readers to override string creation (#1847)
* AVRO-3597: recall method newInstanceFromString for backward compatibility
* AVRO-3597: add licence
---
.../apache/avro/generic/GenericDatumReader.java | 2 +-
.../avro/specific/TestSpecificDatumReader.java | 81 ++++++++++++++++++++++
2 files changed, 82 insertions(+), 1 deletion(-)
diff --git a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericDatumReader.java b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericDatumReader.java
index b816ef365..3c5d1316c 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericDatumReader.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericDatumReader.java
@@ -459,7 +459,7 @@ public class GenericDatumReader<D> implements DatumReader<D> {
if (stringClass == CharSequence.class) {
return readString(old, in);
}
- return this.getReaderCache().newInstanceFromString(stringClass, in.readString());
+ return this.newInstanceFromString(stringClass, in.readString());
}
/**
diff --git a/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificDatumReader.java b/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificDatumReader.java
new file mode 100644
index 000000000..3c10b74cd
--- /dev/null
+++ b/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificDatumReader.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro.specific;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.avro.Schema;
+import org.apache.avro.io.BinaryDecoder;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.avro.util.Utf8;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestSpecificDatumReader {
+
+ @Test
+ void readMyData() throws IOException {
+ // Check that method newInstanceFromString from SpecificDatumReader extension is
+ // called.
+ final EncoderFactory e_factory = new EncoderFactory().configureBufferSize(30);
+ final DecoderFactory factory = new DecoderFactory().configureDecoderBufferSize(30);
+
+ final MyReader reader = new MyReader();
+ reader.setExpected(Schema.create(Schema.Type.STRING));
+ reader.setSchema(Schema.create(Schema.Type.STRING));
+
+ final ByteArrayOutputStream out = new ByteArrayOutputStream(30);
+ final BinaryEncoder encoder = e_factory.binaryEncoder(out, null);
+ encoder.writeString(new Utf8("Hello"));
+ encoder.flush();
+
+ final BinaryDecoder decoder = factory.binaryDecoder(out.toByteArray(), null);
+ reader.getData().setFastReaderEnabled(false);
+ final MyData read = reader.read(null, decoder);
+ Assertions.assertNotNull(read, "MyReader.newInstanceFromString was not called");
+ Assertions.assertEquals("Hello", read.getContent());
+ }
+
+ public static class MyData {
+ private final String content;
+
+ public MyData(String content) {
+ this.content = content;
+ }
+
+ public String getContent() {
+ return content;
+ }
+ }
+
+ public static class MyReader extends SpecificDatumReader<MyData> {
+
+ @Override
+ protected Class findStringClass(Schema schema) {
+ return MyData.class;
+ }
+
+ @Override
+ protected Object newInstanceFromString(Class c, String s) {
+ return new MyData(s);
+ }
+ }
+}