Jackson decoder supports array/Collection elements

Closes gh-32579
This commit is contained in:
rstoyanchev 2024-04-17 15:39:04 +01:00
parent 00c7002354
commit 0bc447c304
2 changed files with 23 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.springframework.http.codec.json;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
@ -135,9 +136,12 @@ public abstract class AbstractJackson2Decoder extends Jackson2CodecSupport imple
forceUseOfBigDecimal = true;
}
boolean tokenizeArrays = (!elementType.isArray() &&
!Collection.class.isAssignableFrom(elementType.resolve(Object.class)));
Flux<DataBuffer> processed = processInput(input, elementType, mimeType, hints);
Flux<TokenBuffer> tokens = Jackson2Tokenizer.tokenize(processed, mapper.getFactory(), mapper,
true, forceUseOfBigDecimal, getMaxInMemorySize());
tokenizeArrays, forceUseOfBigDecimal, getMaxInMemorySize());
return Flux.deferContextual(contextView -> {

View File

@ -46,6 +46,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.codec.json.JacksonViewBean.MyJacksonView1;
import org.springframework.http.codec.json.JacksonViewBean.MyJacksonView3;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.testfixture.xml.Pojo;
import static org.assertj.core.api.Assertions.assertThat;
@ -172,6 +173,22 @@ class Jackson2JsonDecoderTests extends AbstractDecoderTests<Jackson2JsonDecoder>
.verify(), null, null);
}
@Test
protected void decodeToFluxWithListElements() {
Flux<DataBuffer> input = Flux.concat(
stringBuffer("[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"),
stringBuffer("[{\"bar\":\"b3\",\"foo\":\"f3\"},{\"bar\":\"b4\",\"foo\":\"f4\"}]"));
ResolvableType elementType = ResolvableType.forClassWithGenerics(List.class, Pojo.class);
testDecodeAll(input, elementType,
step -> step
.expectNext(List.of(pojo1, pojo2))
.expectNext(List.of(new Pojo("f3", "b3"), new Pojo("f4", "b4")))
.verifyComplete(),
MimeTypeUtils.APPLICATION_JSON,
Collections.emptyMap());
}
@Test
void decodeEmptyArrayToFlux() {