Skip to content

Commit a3cd4b1

Browse files
committed
JAVA-3057 Allow decoding a UDT that has more fields than expected
1 parent ea2e475 commit a3cd4b1

File tree

2 files changed

+18
-16
lines changed
  • core/src

2 files changed

+18
-16
lines changed

core/src/main/java/com/datastax/oss/driver/internal/core/type/codec/UdtCodec.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@
3030
import java.nio.BufferUnderflowException;
3131
import java.nio.ByteBuffer;
3232
import net.jcip.annotations.ThreadSafe;
33+
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
3335

3436
@ThreadSafe
3537
public class UdtCodec implements TypeCodec<UdtValue> {
3638

39+
private static final Logger LOG = LoggerFactory.getLogger(UdtCodec.class);
40+
3741
private final UserDefinedType cqlType;
3842

3943
public UdtCodec(@NonNull UserDefinedType cqlType) {
@@ -107,10 +111,8 @@ public UdtValue decode(@Nullable ByteBuffer bytes, @NonNull ProtocolVersion prot
107111
int i = 0;
108112
while (input.hasRemaining()) {
109113
if (i == cqlType.getFieldTypes().size()) {
110-
throw new IllegalArgumentException(
111-
String.format(
112-
"Too many fields in encoded UDT value, expected %d",
113-
cqlType.getFieldTypes().size()));
114+
LOG.debug("Encountered unexpected fields when parsing codec {}", cqlType);
115+
break;
114116
}
115117
int elementSize = input.getInt();
116118
ByteBuffer element;

core/src/test/java/com/datastax/oss/driver/internal/core/type/codec/UdtCodecTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,18 @@ public void should_decode_udt() {
136136
}
137137

138138
@Test
139-
public void should_fail_to_decode_udt_when_too_many_fields() {
140-
assertThatThrownBy(
141-
() ->
142-
decode(
143-
"0x"
144-
+ ("00000004" + "00000001")
145-
+ "ffffffff"
146-
+ ("00000001" + "61")
147-
// extra contents
148-
+ "ffffffff"))
149-
.isInstanceOf(IllegalArgumentException.class)
150-
.hasMessage("Too many fields in encoded UDT value, expected 3");
139+
public void should_decode_udt_when_too_many_fields() {
140+
UdtValue udt =
141+
decode(
142+
"0x"
143+
+ ("00000004" + "00000001")
144+
+ "ffffffff"
145+
+ ("00000001" + "61")
146+
// extra contents
147+
+ "ffffffff");
148+
assertThat(udt.getInt(0)).isEqualTo(1);
149+
assertThat(udt.isNull(1)).isTrue();
150+
assertThat(udt.getString(2)).isEqualTo("a");
151151
}
152152

153153
/** Test for JAVA-2557. Ensures that the codec can decode null fields with any negative length. */

0 commit comments

Comments
 (0)