提交 0a4d3c14 编写于 作者: A Arjen Poutsma

Jackson2JsonDecoder should support empty JSON array

Before this commit, the Jackson2JsonDecoder was not able to decode an
empty JSON array (`[]`). After this commit, it is.

Issue: SPR-15685
上级 1e04cdfa
...@@ -111,20 +111,23 @@ public class Jackson2JsonDecoder extends Jackson2CodecSupport implements HttpMes ...@@ -111,20 +111,23 @@ public class Jackson2JsonDecoder extends Jackson2CodecSupport implements HttpMes
this.objectMapper.readerFor(javaType)); this.objectMapper.readerFor(javaType));
return objectDecoder.decode(inputStream, elementType, mimeType, hints) return objectDecoder.decode(inputStream, elementType, mimeType, hints)
.map(dataBuffer -> { .flatMap(dataBuffer -> {
if (dataBuffer.readableByteCount() == 0) {
return Mono.empty();
}
try { try {
Object value = reader.readValue(dataBuffer.asInputStream()); Object value = reader.readValue(dataBuffer.asInputStream());
DataBufferUtils.release(dataBuffer); DataBufferUtils.release(dataBuffer);
return value; return Mono.just(value);
} }
catch (InvalidDefinitionException ex) { catch (InvalidDefinitionException ex) {
throw new CodecException("Type definition error: " + ex.getType(), ex); return Mono.error(new CodecException("Type definition error: " + ex.getType(), ex));
} }
catch (JsonProcessingException ex) { catch (JsonProcessingException ex) {
throw new DecodingException("JSON decoding error: " + ex.getOriginalMessage(), ex); return Mono.error(new DecodingException("JSON decoding error: " + ex.getOriginalMessage(), ex));
} }
catch (IOException ex) { catch (IOException ex) {
throw new DecodingException("I/O error while parsing input stream", ex); return Mono.error(new DecodingException("I/O error while parsing input stream", ex));
} }
}); });
} }
......
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -35,9 +35,7 @@ import org.springframework.http.codec.Pojo; ...@@ -35,9 +35,7 @@ import org.springframework.http.codec.Pojo;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.Collections.emptyMap; import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap; import static java.util.Collections.singletonMap;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.*;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.core.ResolvableType.forClass; import static org.springframework.core.ResolvableType.forClass;
import static org.springframework.http.MediaType.APPLICATION_JSON; import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_XML; import static org.springframework.http.MediaType.APPLICATION_XML;
...@@ -116,6 +114,19 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa ...@@ -116,6 +114,19 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
.verifyComplete(); .verifyComplete();
} }
@Test
public void decodeEmptyArrayToFlux() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer("[]"));
ResolvableType elementType = forClass(Pojo.class);
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null,
emptyMap());
StepVerifier.create(flux)
.expectNextCount(0)
.verifyComplete();
}
@Test @Test
public void fieldLevelJsonView() throws Exception { public void fieldLevelJsonView() throws Exception {
Flux<DataBuffer> source = Flux.just( Flux<DataBuffer> source = Flux.just(
...@@ -164,6 +175,18 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa ...@@ -164,6 +175,18 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
.verifyComplete(); .verifyComplete();
} }
@Test
public void decodeEmptyArrayToMono() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer("[]"));
ResolvableType elementType = forClass(Pojo.class);
Mono<Object> mono = new Jackson2JsonDecoder().decodeToMono(source, elementType,
null, emptyMap());
StepVerifier.create(mono)
.expectNextCount(0)
.verifyComplete();
}
@Test @Test
public void invalidData() throws Exception { public void invalidData() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer( "{\"foofoo\": \"foofoo\", \"barbar\": \"barbar\"}")); Flux<DataBuffer> source = Flux.just(stringBuffer( "{\"foofoo\": \"foofoo\", \"barbar\": \"barbar\"}"));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册