Skip to content

Commit 0bc447c

Browse files
committed
Jackson decoder supports array/Collection elements
Closes gh-32579
1 parent 00c7002 commit 0bc447c

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.lang.annotation.Annotation;
2121
import java.math.BigDecimal;
22+
import java.util.Collection;
2223
import java.util.List;
2324
import java.util.Map;
2425
import java.util.concurrent.atomic.AtomicReference;
@@ -135,9 +136,12 @@ public Flux<Object> decode(Publisher<DataBuffer> input, ResolvableType elementTy
135136
forceUseOfBigDecimal = true;
136137
}
137138

139+
boolean tokenizeArrays = (!elementType.isArray() &&
140+
!Collection.class.isAssignableFrom(elementType.resolve(Object.class)));
141+
138142
Flux<DataBuffer> processed = processInput(input, elementType, mimeType, hints);
139143
Flux<TokenBuffer> tokens = Jackson2Tokenizer.tokenize(processed, mapper.getFactory(), mapper,
140-
true, forceUseOfBigDecimal, getMaxInMemorySize());
144+
tokenizeArrays, forceUseOfBigDecimal, getMaxInMemorySize());
141145

142146
return Flux.deferContextual(contextView -> {
143147

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.http.codec.json.JacksonViewBean.MyJacksonView1;
4747
import org.springframework.http.codec.json.JacksonViewBean.MyJacksonView3;
4848
import org.springframework.util.MimeType;
49+
import org.springframework.util.MimeTypeUtils;
4950
import org.springframework.web.testfixture.xml.Pojo;
5051

5152
import static org.assertj.core.api.Assertions.assertThat;
@@ -172,6 +173,22 @@ protected void decodeToMono() {
172173
.verify(), null, null);
173174
}
174175

176+
@Test
177+
protected void decodeToFluxWithListElements() {
178+
Flux<DataBuffer> input = Flux.concat(
179+
stringBuffer("[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"),
180+
stringBuffer("[{\"bar\":\"b3\",\"foo\":\"f3\"},{\"bar\":\"b4\",\"foo\":\"f4\"}]"));
181+
182+
ResolvableType elementType = ResolvableType.forClassWithGenerics(List.class, Pojo.class);
183+
184+
testDecodeAll(input, elementType,
185+
step -> step
186+
.expectNext(List.of(pojo1, pojo2))
187+
.expectNext(List.of(new Pojo("f3", "b3"), new Pojo("f4", "b4")))
188+
.verifyComplete(),
189+
MimeTypeUtils.APPLICATION_JSON,
190+
Collections.emptyMap());
191+
}
175192

176193
@Test
177194
void decodeEmptyArrayToFlux() {

0 commit comments

Comments
 (0)