From e373b46660380ca37bfd17845ea5eac6741c9888 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 18 Jan 2019 18:27:40 -0500 Subject: [PATCH] Jackson2JsonDecoder tolerates null literal Fixes #22042 --- .../http/codec/json/AbstractJackson2Decoder.java | 12 ++++++------ .../http/codec/json/Jackson2JsonDecoderTests.java | 11 ++++++++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Decoder.java b/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Decoder.java index f3c2433f2e6..3afadd4fb68 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Decoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Decoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -113,7 +113,7 @@ public abstract class AbstractJackson2Decoder extends Jackson2CodecSupport imple getObjectMapper().readerWithView(jsonView).forType(javaType) : getObjectMapper().readerFor(javaType)); - return tokens.map(tokenBuffer -> { + return tokens.flatMap(tokenBuffer -> { try { Object value = reader.readValue(tokenBuffer.asParser(getObjectMapper())); if (!Hints.isLoggingSuppressed(hints)) { @@ -122,16 +122,16 @@ public abstract class AbstractJackson2Decoder extends Jackson2CodecSupport imple return Hints.getLogPrefix(hints) + "Decoded [" + formatted + "]"; }); } - return value; + return Mono.justOrEmpty(value); } 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) { - throw new DecodingException("JSON decoding error: " + ex.getOriginalMessage(), ex); + return Mono.error(new DecodingException("JSON decoding error: " + ex.getOriginalMessage(), 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)); } }); } diff --git a/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java b/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java index 2b8b62be565..f8dea0c25cc 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -38,6 +38,7 @@ import org.springframework.core.codec.AbstractDecoderTestCase; import org.springframework.core.codec.CodecException; import org.springframework.core.codec.DecodingException; import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.http.MediaType; import org.springframework.http.codec.Pojo; import org.springframework.util.MimeType; @@ -174,6 +175,14 @@ public class Jackson2JsonDecoderTests extends AbstractDecoderTestCase result = this.decoder.decode(Flux.concat(stringBuffer("null")), + ResolvableType.forType(Pojo.class), MediaType.APPLICATION_JSON, Collections.emptyMap()); + + StepVerifier.create(result).expectComplete().verify(); + } + @Test public void noDefaultConstructor() { Flux input =