kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | a22272b1286157f7705ae72a065588d528303102750ff6d86008b81b7479685f |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-17 11:47 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-17 11:47 +02:00 |
| Parents | c79a6f84a0b1, 77043725d2d6 |
3 files changed
modified changelog/README.md +2 −0
| @@ -5,6 +5,8 @@ | ||
| 5 | 5 | - [bug] Propagate CodecRegistry to nested UDTs (JAVA-847) |
| 6 | 6 | - [improvement] Ability to store a default, shareable CodecRegistry |
| 7 | 7 | instance (JAVA-848) |
| 8 | +- [bug] Treat empty ByteBuffers as empty values in TupleCodec and | |
| 9 | + UDTCodec (JAVA-880) | |
| 8 | 10 | |
| 9 | 11 | |
| 10 | 12 | ### 2.2.0-rc2 |
modified driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java +4 −2
| @@ -2009,8 +2009,9 @@ | ||
| 2009 | 2009 | |
| 2010 | 2010 | @Override |
| 2011 | 2011 | public UDTValue deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { |
| 2012 | - if (bytes == null || bytes.remaining() == 0) | |
| 2012 | + if (bytes == null) | |
| 2013 | 2013 | return null; |
| 2014 | + // empty byte buffers will result in empty UDTValues | |
| 2014 | 2015 | ByteBuffer input = bytes.duplicate(); |
| 2015 | 2016 | UDTValue value = definition.newValue(); |
| 2016 | 2017 | |
| @@ -2121,8 +2122,9 @@ | ||
| 2121 | 2122 | |
| 2122 | 2123 | @Override |
| 2123 | 2124 | public TupleValue deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { |
| 2124 | - if (bytes == null || bytes.remaining() == 0) | |
| 2125 | + if (bytes == null) | |
| 2125 | 2126 | return null; |
| 2127 | + // empty byte buffers will result in empty TupleValues | |
| 2126 | 2128 | ByteBuffer input = bytes.duplicate(); |
| 2127 | 2129 | TupleValue value = definition.newValue(); |
| 2128 | 2130 |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecTest.java +32 −1
| @@ -31,6 +31,7 @@ | ||
| 31 | 31 | import static com.google.common.collect.Lists.newArrayList; |
| 32 | 32 | import static org.testng.Assert.fail; |
| 33 | 33 | |
| 34 | +import com.datastax.driver.core.TypeCodec.UDTCodec; | |
| 34 | 35 | import com.datastax.driver.core.UserType.Field; |
| 35 | 36 | import com.datastax.driver.core.exceptions.CodecNotFoundException; |
| 36 | 37 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| @@ -169,7 +170,7 @@ | ||
| 169 | 170 | UserType u2 = new UserType("ks", "table", newArrayList(new Field("f1", text()), new Field("f2", varchar())), V3, new CodecRegistry()); |
| 170 | 171 | UserType u3 = new UserType("ks", "table", newArrayList(new Field("f1", varchar()), new Field("f2", text())), V3, new CodecRegistry()); |
| 171 | 172 | 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)) | |
| 173 | 174 | .accepts(u2) |
| 174 | 175 | .accepts(u3) |
| 175 | 176 | .accepts(u4); |
| @@ -218,6 +219,36 @@ | ||
| 218 | 219 | } |
| 219 | 220 | |
| 220 | 221 | |
| 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 | + | |
| 221 | 252 | private class ListVarcharToListListInteger extends TypeCodec<List<List<Integer>>> { |
| 222 | 253 | |
| 223 | 254 | private final ListCodec<String> codec = new ListCodec<String>(VarcharCodec.instance); |