Skip to content

Commit

Permalink
[Java] Fix bug in DTO decodeFrom signature.
Browse files Browse the repository at this point in the history
It was using `short` for `actingVersion` and `blockLength`, but the
inner call to `Decoder#wrap` expects an `int` (in the `uint16` range)
for both arguments.  There were a bunch of down/narrowing casts in
tests, which I should have spotted when doing the port and have now been
removed.
  • Loading branch information
ZachBray committed Sep 26, 2024
1 parent ae8a211 commit f83f986
Showing 3 changed files with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -470,7 +470,7 @@ private static void generateDecodeFrom(
.append("\n")
.append(indent).append("public static ").append(dtoClassName).append(" decodeFrom(")
.append("DirectBuffer buffer, int offset, ")
.append("short actingBlockLength, short actingVersion)\n")
.append("int actingBlockLength, int actingVersion)\n")
.append(indent).append("{\n")
.append(indent).append(INDENT).append(decoderClassName).append(" decoder = new ")
.append(decoderClassName).append("();\n")
Original file line number Diff line number Diff line change
@@ -101,16 +101,16 @@ void javaDtoEncodeShouldBeTheInverseOfDtoDecode(
generatedClassLoader.loadClass(packageName + ".TestMessageDto");

final Method decodeFrom =
dtoClass.getMethod("decodeFrom", DirectBuffer.class, int.class, short.class, short.class);
dtoClass.getMethod("decodeFrom", DirectBuffer.class, int.class, int.class, int.class);

final Method encodeWith =
dtoClass.getMethod("encodeWithHeaderWith", dtoClass, MutableDirectBuffer.class, int.class);

final int inputLength = encodedMessage.length();
final ExpandableArrayBuffer inputBuffer = encodedMessage.buffer();
final MessageHeaderDecoder header = new MessageHeaderDecoder().wrap(inputBuffer, 0);
final short blockLength = (short)header.blockLength();
final short actingVersion = (short)header.version();
final int blockLength = header.blockLength();
final int actingVersion = header.version();
final Object dto = decodeFrom.invoke(null,
encodedMessage.buffer(), MessageHeaderDecoder.ENCODED_LENGTH, blockLength, actingVersion);
outputBuffer.setMemory(0, outputBuffer.capacity(), (byte)0);
Original file line number Diff line number Diff line change
@@ -56,8 +56,7 @@ void shouldRoundTripCar2()
final ExpandableArrayBuffer inputBuffer = new ExpandableArrayBuffer();
final int inputLength = encodeCar(inputBuffer, 0);

final CarDto dto =
CarDto.decodeFrom(inputBuffer, 0, (short)CarDecoder.BLOCK_LENGTH, (short)CarDecoder.SCHEMA_VERSION);
final CarDto dto = CarDto.decodeFrom(inputBuffer, 0, CarDecoder.BLOCK_LENGTH, CarDecoder.SCHEMA_VERSION);

final ExpandableArrayBuffer outputBuffer = new ExpandableArrayBuffer();
final int outputLength = CarDto.encodeWith(dto, outputBuffer, 0);

0 comments on commit f83f986

Please sign in to comment.