Elffuss BraidHub

kikocis/java-driver public

JAVA-880: TupleCodec and UDTCodec should treat empty ByteBuffers as empty values, not null values.
Changeset77043725d2d6fc2c0c03ff0252be49237c57cabecb505a7b9e3df179d96ce381
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-08-14 15:49 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-08-17 11:40 +02:00
Parentsc79a6f84a0b1

Browse the files at this changeset

3 files changed

modified changelog/README.md +2 −0
@@ -5,6 +5,8 @@
55 - [bug] Propagate CodecRegistry to nested UDTs (JAVA-847)
66 - [improvement] Ability to store a default, shareable CodecRegistry
77 instance (JAVA-848)
8+- [bug] Treat empty ByteBuffers as empty values in TupleCodec and
9+ UDTCodec (JAVA-880)
810
911
1012 ### 2.2.0-rc2
modified driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java +4 −2
@@ -2009,8 +2009,9 @@
20092009
20102010 @Override
20112011 public UDTValue deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) {
2012- if (bytes == null || bytes.remaining() == 0)
2012+ if (bytes == null)
20132013 return null;
2014+ // empty byte buffers will result in empty UDTValues
20142015 ByteBuffer input = bytes.duplicate();
20152016 UDTValue value = definition.newValue();
20162017
@@ -2121,8 +2122,9 @@
21212122
21222123 @Override
21232124 public TupleValue deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) {
2124- if (bytes == null || bytes.remaining() == 0)
2125+ if (bytes == null)
21252126 return null;
2127+ // empty byte buffers will result in empty TupleValues
21262128 ByteBuffer input = bytes.duplicate();
21272129 TupleValue value = definition.newValue();
21282130
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecTest.java +32 −1
@@ -31,6 +31,7 @@
3131 import static com.google.common.collect.Lists.newArrayList;
3232 import static org.testng.Assert.fail;
3333
34+import com.datastax.driver.core.TypeCodec.UDTCodec;
3435 import com.datastax.driver.core.UserType.Field;
3536 import com.datastax.driver.core.exceptions.CodecNotFoundException;
3637 import com.datastax.driver.core.exceptions.InvalidTypeException;
@@ -169,7 +170,7 @@
169170 UserType u2 = new UserType("ks", "table", newArrayList(new Field("f1", text()), new Field("f2", varchar())), V3, new CodecRegistry());
170171 UserType u3 = new UserType("ks", "table", newArrayList(new Field("f1", varchar()), new Field("f2", text())), V3, new CodecRegistry());
171172 UserType u4 = new UserType("ks", "table", newArrayList(new Field("f1", text()), new Field("f2", text())), V3, new CodecRegistry());
172- assertThat(new TypeCodec.UDTCodec(u1))
173+ assertThat(new UDTCodec(u1))
173174 .accepts(u2)
174175 .accepts(u3)
175176 .accepts(u4);
@@ -218,6 +219,36 @@
218219 }
219220
220221
222+ @Test(groups = "unit")
223+ public void should_deserialize_empty_buffer_as_tuple_with_null_values() {
224+ CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE;
225+ TupleType tupleType = new TupleType(newArrayList(DataType.cint(), DataType.varchar(), DataType.cfloat()), ProtocolVersion.NEWEST_SUPPORTED, codecRegistry);
226+ TupleValue expected = tupleType.newValue(null, null, null);
227+
228+ TupleValue actual = codecRegistry.codecFor(tupleType, TupleValue.class).deserialize(ByteBuffer.allocate(0), ProtocolVersion.NEWEST_SUPPORTED);
229+ assertThat(actual).isNotNull();
230+ assertThat(actual).isEqualTo(expected);
231+ }
232+
233+ @Test(groups = "unit")
234+ public void should_deserialize_empty_buffer_as_udt_with_null_values() {
235+ CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE;
236+ UserType udt = new UserType("ks", "t", Arrays.asList(
237+ new UserType.Field("t", DataType.text()),
238+ new UserType.Field("i", DataType.cint()),
239+ new UserType.Field("l", DataType.list(DataType.text()))
240+ ), ProtocolVersion.NEWEST_SUPPORTED, codecRegistry);
241+ UDTValue expected = udt.newValue();
242+ expected.setString("t", null);
243+ expected.setObject("i", null);
244+ expected.setList("l", null);
245+
246+ UDTValue actual = codecRegistry.codecFor(udt, UDTValue.class).deserialize(ByteBuffer.allocate(0), ProtocolVersion.NEWEST_SUPPORTED);
247+ assertThat(actual).isNotNull();
248+ assertThat(actual).isEqualTo(expected);
249+ }
250+
251+
221252 private class ListVarcharToListListInteger extends TypeCodec<List<List<Integer>>> {
222253
223254 private final ListCodec<String> codec = new ListCodec<String>(VarcharCodec.instance);