Skip to content

Commit

Permalink
AVRO-1564. Java: Fix handling of optional byte field in Thrift. Contr…
Browse files Browse the repository at this point in the history
…ibuted by Michael Pershyn.

git-svn-id: https://svn.apache.org/repos/asf/avro/trunk@1641892 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
cutting committed Nov 26, 2014
1 parent f66a787 commit ad27ed0
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 35 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ Trunk (not yet released)
AVRO-1596. Java: Cannot read past corrupted block in Avro data file.
(tomwhite)

AVRO-1564. Java: Fix handling of optional byte field in Thrift.
(Michael Pershyn via cutting)

Avro 1.7.7 (23 July 2014)

NEW FEATURES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ protected String getSchemaName(Object datum) {
// to avro INT for thrift's optional fields
if (datum instanceof Short)
return Schema.Type.INT.getName();
// support implicit conversion from thrift's byte
// to avro INT for thrift's optional fields
if (datum instanceof Byte)
return Schema.Type.INT.getName();

return super.getSchemaName(datum);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public class TestThrift {
Test test = new Test();
test.setBoolField(true);
test.setByteField((byte)2);
test.setByteOptionalField((byte)4);
test.setI16Field((short)3);
test.setI16OptionalField((short)15);
test.setI64Field(5L);
test.setDoubleField(2.0);

Expand All @@ -75,15 +77,14 @@ public class TestThrift {
check(test);
}


private void check(Test test) throws Exception {

ByteArrayOutputStream bao = new ByteArrayOutputStream();
ThriftDatumWriter<Test> w = new ThriftDatumWriter<Test>(Test.class);
Encoder e = EncoderFactory.get().binaryEncoder(bao, null);
w.write(test, e);
e.flush();

Object o = new ThriftDatumReader<Test>(Test.class).read
(null,
DecoderFactory.get().createBinaryDecoder
Expand Down
Loading

0 comments on commit ad27ed0

Please sign in to comment.