Skip to content

Throw an exception on invalid syntax in SPRING_APPLICATION_JSON #14251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* @author Dave Syer
* @author Phillip Webb
* @author Madhura Bhave
* @author Artsiom Yudovin
* @since 1.3.0
*/
public class SpringApplicationJsonEnvironmentPostProcessor
Expand Down Expand Up @@ -97,17 +98,11 @@ public void postProcessEnvironment(ConfigurableEnvironment environment,

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)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,15 +35,21 @@
* @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");
Expand Down