Skip to content

Commit b1030eb

Browse files
committed
Fix JsonObjectDecoder chunks handling
Issue: SPR-14859
1 parent d59caaa commit b1030eb

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

spring-web/src/main/java/org/springframework/http/codec/json/JsonObjectDecoder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public Publisher<? extends DataBuffer> apply(DataBuffer buffer) {
118118
this.writerIndex = this.input.writerIndex();
119119
}
120120
else {
121+
this.index = this.index - this.input.readerIndex();
121122
this.input = Unpooled.copiedBuffer(this.input,
122123
Unpooled.copiedBuffer(buffer.asByteBuffer()));
123124
DataBufferUtils.release(buffer);

spring-web/src/test/java/org/springframework/http/codec/json/JsonObjectDecoderTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public void decodeMultipleChunksToJsonObject() throws InterruptedException {
6060
@Test
6161
public void decodeSingleChunkToArray() throws InterruptedException {
6262
JsonObjectDecoder decoder = new JsonObjectDecoder();
63+
6364
Flux<DataBuffer> source = Flux.just(stringBuffer(
6465
"[{\"foo\": \"foofoo\", \"bar\": \"barbar\"},{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}]"));
6566
Flux<String> output =
@@ -69,11 +70,20 @@ public void decodeSingleChunkToArray() throws InterruptedException {
6970
.expectNext("{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}")
7071
.expectComplete()
7172
.verify(output);
73+
74+
source = Flux.just(stringBuffer("[{\"foo\": \"bar\"},{\"foo\": \"baz\"}]"));
75+
output = decoder.decode(source, null, null, Collections.emptyMap()).map(JsonObjectDecoderTests::toString);
76+
ScriptedSubscriber.<String>create()
77+
.expectNext("{\"foo\": \"bar\"}")
78+
.expectNext("{\"foo\": \"baz\"}")
79+
.expectComplete()
80+
.verify(output);
7281
}
7382

7483
@Test
7584
public void decodeMultipleChunksToArray() throws InterruptedException {
7685
JsonObjectDecoder decoder = new JsonObjectDecoder();
86+
7787
Flux<DataBuffer> source =
7888
Flux.just(stringBuffer("[{\"foo\": \"foofoo\", \"bar\""), stringBuffer(
7989
": \"barbar\"},{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}]"));
@@ -84,6 +94,18 @@ public void decodeMultipleChunksToArray() throws InterruptedException {
8494
.expectNext("{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}")
8595
.expectComplete()
8696
.verify(output);
97+
98+
source = Flux.just(
99+
stringBuffer("[{\"foo\": \""),
100+
stringBuffer("bar\"},{\"fo"),
101+
stringBuffer("o\": \"baz\"}"),
102+
stringBuffer("]"));
103+
output = decoder.decode(source, null, null, Collections.emptyMap()).map(JsonObjectDecoderTests::toString);
104+
ScriptedSubscriber.<String>create()
105+
.expectNext("{\"foo\": \"bar\"}")
106+
.expectNext("{\"foo\": \"baz\"}")
107+
.expectComplete()
108+
.verify(output);
87109
}
88110

89111

0 commit comments

Comments
 (0)