diff --git a/spring-web/src/main/java/org/springframework/http/codec/json/JsonObjectDecoder.java b/spring-web/src/main/java/org/springframework/http/codec/json/JsonObjectDecoder.java index 4e0fe48f17..93f6f47599 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/json/JsonObjectDecoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/json/JsonObjectDecoder.java @@ -118,6 +118,7 @@ class JsonObjectDecoder extends AbstractDecoder { this.writerIndex = this.input.writerIndex(); } else { + this.index = this.index - this.input.readerIndex(); this.input = Unpooled.copiedBuffer(this.input, Unpooled.copiedBuffer(buffer.asByteBuffer())); DataBufferUtils.release(buffer); diff --git a/spring-web/src/test/java/org/springframework/http/codec/json/JsonObjectDecoderTests.java b/spring-web/src/test/java/org/springframework/http/codec/json/JsonObjectDecoderTests.java index d9c145e5b0..9ebd961f04 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/json/JsonObjectDecoderTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/json/JsonObjectDecoderTests.java @@ -60,6 +60,7 @@ public class JsonObjectDecoderTests extends AbstractDataBufferAllocatingTestCase @Test public void decodeSingleChunkToArray() throws InterruptedException { JsonObjectDecoder decoder = new JsonObjectDecoder(); + Flux source = Flux.just(stringBuffer( "[{\"foo\": \"foofoo\", \"bar\": \"barbar\"},{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}]")); Flux output = @@ -69,11 +70,20 @@ public class JsonObjectDecoderTests extends AbstractDataBufferAllocatingTestCase .expectNext("{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}") .expectComplete() .verify(output); + + source = Flux.just(stringBuffer("[{\"foo\": \"bar\"},{\"foo\": \"baz\"}]")); + output = decoder.decode(source, null, null, Collections.emptyMap()).map(JsonObjectDecoderTests::toString); + ScriptedSubscriber.create() + .expectNext("{\"foo\": \"bar\"}") + .expectNext("{\"foo\": \"baz\"}") + .expectComplete() + .verify(output); } @Test public void decodeMultipleChunksToArray() throws InterruptedException { JsonObjectDecoder decoder = new JsonObjectDecoder(); + Flux source = Flux.just(stringBuffer("[{\"foo\": \"foofoo\", \"bar\""), stringBuffer( ": \"barbar\"},{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}]")); @@ -84,6 +94,18 @@ public class JsonObjectDecoderTests extends AbstractDataBufferAllocatingTestCase .expectNext("{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}") .expectComplete() .verify(output); + + source = Flux.just( + stringBuffer("[{\"foo\": \""), + stringBuffer("bar\"},{\"fo"), + stringBuffer("o\": \"baz\"}"), + stringBuffer("]")); + output = decoder.decode(source, null, null, Collections.emptyMap()).map(JsonObjectDecoderTests::toString); + ScriptedSubscriber.create() + .expectNext("{\"foo\": \"bar\"}") + .expectNext("{\"foo\": \"baz\"}") + .expectComplete() + .verify(output); }