Skip to content

Commit

Permalink
Fix netty#6969: Do not reset the states while streaming Json array
Browse files Browse the repository at this point in the history
Motivation:

Calling JsonObjectDecoder#reset while streaming Json array over multiple
writes causes CorruptedFrameException to be thrown.

Modifications:

While streaming Json array and if the current readerIndex has been reset,
ensure that the states will not be reset.

Result:

Fixes netty#6969
  • Loading branch information
violetagg authored and normanmaurer committed Jul 17, 2017
1 parent 6152990 commit 96f52e0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t

if (this.idx > in.readerIndex() && lastReaderIndex != in.readerIndex()) {
this.idx = in.readerIndex();
reset();
}

// index of next byte to process.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,40 @@ public void testJsonArrayOverMultipleWrites() {
assertFalse(ch.finish());
}

@Test
public void testStreamJsonArrayOverMultipleWrites() {
EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder(true));

String arrayPart1 = "[{\"test";
String arrayPart2 = "case\" : \"\\\"}]Escaped dou\\\"ble quotes \\\" in JSON str\\\"ing\"";
String arrayPart3 = " }\n\n , ";
String arrayPart4 = "{\"testcase\" : \"Streaming string me";
String arrayPart5 = "ssage\"} ]";

// Test array
boolean dataAvailable = ch.writeInbound(Unpooled.copiedBuffer(" " + arrayPart1, CharsetUtil.UTF_8));
assertFalse(dataAvailable);
dataAvailable = ch.writeInbound(Unpooled.copiedBuffer(arrayPart2, CharsetUtil.UTF_8));
assertFalse(dataAvailable);
dataAvailable = ch.writeInbound(Unpooled.copiedBuffer(arrayPart3, CharsetUtil.UTF_8));
assertTrue(dataAvailable);
dataAvailable = ch.writeInbound(Unpooled.copiedBuffer(arrayPart4, CharsetUtil.UTF_8));
assertTrue(dataAvailable);
dataAvailable = ch.writeInbound(Unpooled.copiedBuffer(arrayPart5 + " ", CharsetUtil.UTF_8));
assertTrue(dataAvailable);

ByteBuf res = ch.readInbound();
assertEquals("{\"testcase\" : \"\\\"}]Escaped dou\\\"ble quotes \\\" in JSON str\\\"ing\" }",
res.toString(CharsetUtil.UTF_8));
res.release();

res = ch.readInbound();
assertEquals("{\"testcase\" : \"Streaming string message\"}", res.toString(CharsetUtil.UTF_8));
res.release();

assertFalse(ch.finish());
}

@Test
public void testSingleByteStream() {
EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder());
Expand Down

0 comments on commit 96f52e0

Please sign in to comment.