Skip to content

Commit 3fbea11

Browse files
AVRO-4169 fix bug with custom encoder (#3440)
1 parent 0606c33 commit 3fbea11

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,14 @@ protected Schema createSchema(Type type, Map<String, Schema> names) {
683683
setElement(result, component);
684684
return result;
685685
}
686+
AvroEncode enc = ReflectionUtil.getAvroEncode(c);
687+
if (enc != null) {
688+
try {
689+
return enc.using().getDeclaredConstructor().newInstance().getSchema();
690+
} catch (Exception e) {
691+
throw new AvroRuntimeException("Could not create schema from custom serializer for " + c.getName());
692+
}
693+
}
686694
AvroSchema explicit = c.getAnnotation(AvroSchema.class);
687695
if (explicit != null) // explicit schema
688696
return new Schema.Parser().parse(explicit.value());

lang/java/avro/src/test/java/org/apache/avro/reflect/TestAvroEncode.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,23 @@ public class TestAvroEncode {
3737
@Test
3838
void testWithinClass() throws IOException {
3939

40-
var wrapper = new Wrapper(new R1("test"));
40+
var wrapper = new Wrapper(new R1("321"));
4141

4242
var read = readWrite(wrapper);
4343

44-
assertEquals("test", wrapper.getR1().getValue());
45-
assertEquals("test used this", read.getR1().getValue());
44+
assertEquals("321", wrapper.getR1().getValue());
45+
assertEquals("321 used this", read.getR1().getValue());
4646
}
4747

4848
@Test
4949
void testDirect() throws IOException {
5050

51-
var r1 = new R1("test");
51+
var r1 = new R1("123");
5252

5353
var read = readWrite(r1);
5454

55-
assertEquals("test", r1.getValue());
56-
assertEquals("test used this", read.getValue());
55+
assertEquals("123", r1.getValue());
56+
assertEquals("123 used this", read.getValue());
5757
}
5858

5959
@Test
@@ -127,14 +127,15 @@ public String getValue() {
127127
public static class R1Encoding extends CustomEncoding<R1> {
128128

129129
{
130-
schema = Schema.createRecord("R1", null, null, false,
131-
Arrays.asList(new Schema.Field("value", Schema.create(Schema.Type.STRING), null, null)));
130+
schema = Schema.createRecord("R1", null, "org.apache.avro.reflect.TestAvroEncode", false,
131+
Arrays.asList(new Schema.Field("value", Schema.create(Schema.Type.INT), null, null)));
132132
}
133133

134134
@Override
135135
protected void write(Object datum, Encoder out) throws IOException {
136136
if (datum instanceof R1) {
137-
out.writeString(((R1) datum).getValue());
137+
var value = ((R1) datum).getValue();
138+
out.writeInt(Integer.parseInt(value));
138139
} else {
139140
throw new AvroTypeException("Expected R1, got " + datum.getClass());
140141
}
@@ -143,7 +144,7 @@ protected void write(Object datum, Encoder out) throws IOException {
143144

144145
@Override
145146
protected R1 read(Object reuse, Decoder in) throws IOException {
146-
return new R1(in.readString() + " used this");
147+
return new R1(in.readInt() + " used this");
147148
}
148149
}
149150

0 commit comments

Comments
 (0)