From 11f0f668ff3ec0fdcb4f0586a31181597dd25393 Mon Sep 17 00:00:00 2001 From: artsiom Date: Thu, 30 Aug 2018 13:43:02 +0300 Subject: [PATCH] Throw an exception on invalid syntax in SPRING_APPLICATION_JSON Closes gh-14251 --- ...ApplicationJsonEnvironmentPostProcessor.java | 17 ++++++----------- ...cationJsonEnvironmentPostProcessorTests.java | 9 +++++++++ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/SpringApplicationJsonEnvironmentPostProcessor.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/SpringApplicationJsonEnvironmentPostProcessor.java index 3570a42026..6508b9cafa 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/SpringApplicationJsonEnvironmentPostProcessor.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/SpringApplicationJsonEnvironmentPostProcessor.java @@ -50,6 +50,7 @@ import org.springframework.web.context.support.StandardServletEnvironment; * @author Dave Syer * @author Phillip Webb * @author Madhura Bhave + * @author Artsiom Yudovin * @since 1.3.0 */ public class SpringApplicationJsonEnvironmentPostProcessor @@ -97,17 +98,11 @@ public class SpringApplicationJsonEnvironmentPostProcessor private void processJson(ConfigurableEnvironment environment, JsonPropertyValue propertyValue) { - try { - JsonParser parser = JsonParserFactory.getJsonParser(); - Map map = parser.parseMap(propertyValue.getJson()); - if (!map.isEmpty()) { - addJsonPropertySource(environment, - new JsonPropertySource(propertyValue, flatten(map))); - } - } - catch (Exception ex) { - logger.warn("Cannot parse JSON for spring.application.json: " - + propertyValue.getJson(), ex); + JsonParser parser = JsonParserFactory.getJsonParser(); + Map map = parser.parseMap(propertyValue.getJson()); + if (!map.isEmpty()) { + addJsonPropertySource(environment, + new JsonPropertySource(propertyValue, flatten(map))); } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/SpringApplicationJsonEnvironmentPostProcessorTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/SpringApplicationJsonEnvironmentPostProcessorTests.java index b485853eaa..574ffe8ba1 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/SpringApplicationJsonEnvironmentPostProcessorTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/SpringApplicationJsonEnvironmentPostProcessorTests.java @@ -16,8 +16,11 @@ package org.springframework.boot.env; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.springframework.boot.json.JsonParseException; import org.springframework.boot.origin.PropertySourceOrigin; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.PropertySource; @@ -32,15 +35,21 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Dave Syer * @author Madhura Bhave * @author Phillip Webb + * @author Artsiom Yudovin */ public class SpringApplicationJsonEnvironmentPostProcessorTests { + @Rule + public ExpectedException expected = ExpectedException.none(); + private SpringApplicationJsonEnvironmentPostProcessor processor = new SpringApplicationJsonEnvironmentPostProcessor(); private ConfigurableEnvironment environment = new StandardEnvironment(); @Test public void error() { + this.expected.expect(JsonParseException.class); + this.expected.expectMessage("Cannot parse JSON"); assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty(); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "spring.application.json=foo:bar");