Throw an exception on invalid syntax in SPRING_APPLICATION_JSON
Closes gh-14251
This commit is contained in:
@@ -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<String, Object> 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<String, Object> map = parser.parseMap(propertyValue.getJson());
|
||||
if (!map.isEmpty()) {
|
||||
addJsonPropertySource(environment,
|
||||
new JsonPropertySource(propertyValue, flatten(map)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user