Commit 11f0f668 authored by artsiom's avatar artsiom Committed by Stephane Nicoll

Throw an exception on invalid syntax in SPRING_APPLICATION_JSON

Closes gh-14251
parent 5a1aefc7
...@@ -50,6 +50,7 @@ import org.springframework.web.context.support.StandardServletEnvironment; ...@@ -50,6 +50,7 @@ import org.springframework.web.context.support.StandardServletEnvironment;
* @author Dave Syer * @author Dave Syer
* @author Phillip Webb * @author Phillip Webb
* @author Madhura Bhave * @author Madhura Bhave
* @author Artsiom Yudovin
* @since 1.3.0 * @since 1.3.0
*/ */
public class SpringApplicationJsonEnvironmentPostProcessor public class SpringApplicationJsonEnvironmentPostProcessor
...@@ -97,17 +98,11 @@ public class SpringApplicationJsonEnvironmentPostProcessor ...@@ -97,17 +98,11 @@ public class SpringApplicationJsonEnvironmentPostProcessor
private void processJson(ConfigurableEnvironment environment, private void processJson(ConfigurableEnvironment environment,
JsonPropertyValue propertyValue) { JsonPropertyValue propertyValue) {
try { JsonParser parser = JsonParserFactory.getJsonParser();
JsonParser parser = JsonParserFactory.getJsonParser(); Map<String, Object> map = parser.parseMap(propertyValue.getJson());
Map<String, Object> map = parser.parseMap(propertyValue.getJson()); if (!map.isEmpty()) {
if (!map.isEmpty()) { addJsonPropertySource(environment,
addJsonPropertySource(environment, new JsonPropertySource(propertyValue, flatten(map)));
new JsonPropertySource(propertyValue, flatten(map)));
}
}
catch (Exception ex) {
logger.warn("Cannot parse JSON for spring.application.json: "
+ propertyValue.getJson(), ex);
} }
} }
......
...@@ -16,8 +16,11 @@ ...@@ -16,8 +16,11 @@
package org.springframework.boot.env; package org.springframework.boot.env;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.json.JsonParseException;
import org.springframework.boot.origin.PropertySourceOrigin; import org.springframework.boot.origin.PropertySourceOrigin;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
...@@ -32,15 +35,21 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -32,15 +35,21 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer * @author Dave Syer
* @author Madhura Bhave * @author Madhura Bhave
* @author Phillip Webb * @author Phillip Webb
* @author Artsiom Yudovin
*/ */
public class SpringApplicationJsonEnvironmentPostProcessorTests { public class SpringApplicationJsonEnvironmentPostProcessorTests {
@Rule
public ExpectedException expected = ExpectedException.none();
private SpringApplicationJsonEnvironmentPostProcessor processor = new SpringApplicationJsonEnvironmentPostProcessor(); private SpringApplicationJsonEnvironmentPostProcessor processor = new SpringApplicationJsonEnvironmentPostProcessor();
private ConfigurableEnvironment environment = new StandardEnvironment(); private ConfigurableEnvironment environment = new StandardEnvironment();
@Test @Test
public void error() { public void error() {
this.expected.expect(JsonParseException.class);
this.expected.expectMessage("Cannot parse JSON");
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty(); assertThat(this.environment.resolvePlaceholders("${foo:}")).isEmpty();
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.application.json=foo:bar"); "spring.application.json=foo:bar");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment