Align JSON null and empty handling with that of YAML

Closes gh-51583
This commit is contained in:
Andy Wilkinson
2026-09-04 12:35:34 +01:00
parent dc52ea8eb5
commit a8f73986f6
2 changed files with 18 additions and 20 deletions
@@ -131,14 +131,14 @@ public class SpringApplicationJsonEnvironmentPostProcessor implements Environmen
private void extract(String name, Map<String, Object> result, Object value) {
if (value instanceof Map<?, ?> map) {
if (CollectionUtils.isEmpty(map)) {
result.put(name, value);
result.put(name, "");
return;
}
flatten(name, result, (Map<String, Object>) value);
}
else if (value instanceof Collection<?> collection) {
if (CollectionUtils.isEmpty(collection)) {
result.put(name, value);
result.put(name, "");
return;
}
int index = 0;
@@ -148,7 +148,7 @@ public class SpringApplicationJsonEnvironmentPostProcessor implements Environmen
}
}
else {
result.put(name, value);
result.put(name, (value != null) ? value : "");
}
}
@@ -17,7 +17,6 @@
package org.springframework.boot.support;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
@@ -28,7 +27,6 @@ import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.mock.env.MockPropertySource;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.web.context.support.StandardServletEnvironment;
@@ -175,36 +173,36 @@ class SpringApplicationJsonEnvironmentPostProcessorTests {
}
@Test
void nullValuesShouldBeAddedToPropertySource() {
void nullValueIsMappedToAnEmptyString() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":null}");
this.processor.postProcessEnvironment(this.environment, getApplication());
assertThat(this.environment.containsProperty("foo")).isTrue();
assertThat(this.environment.getProperty("foo")).isEmpty();
}
@Test
void emptyValuesForCollectionShouldNotBeIgnored() {
void nullValueInArrayIsMappedToAnEmptyString() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":[\"bar\", null]}");
this.processor.postProcessEnvironment(this.environment, getApplication());
assertThat(this.environment.getProperty("foo[0]")).isEqualTo("bar");
assertThat(this.environment.getProperty("foo[1]")).isEmpty();
}
@Test
void emptyArrayIsMappedToAnEmptyString() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":[]}");
MockPropertySource source = new MockPropertySource();
source.setProperty("foo", "bar");
this.environment.getPropertySources().addLast(source);
assertThat(this.environment.resolvePlaceholders("${foo}")).isEqualTo("bar");
this.environment.getPropertySources().addLast(source);
this.processor.postProcessEnvironment(this.environment, getApplication());
assertThat(this.environment.resolvePlaceholders("${foo}")).isEmpty();
assertThat(this.environment.getProperty("foo")).isEmpty();
}
@Test
@SuppressWarnings("unchecked")
void emptyMapValuesShouldNotBeIgnored() {
void emptyMapIsMappedToAnEmptyString() {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"SPRING_APPLICATION_JSON={\"foo\":{}}");
MockPropertySource source = new MockPropertySource();
source.setProperty("foo.baz", "bar");
this.environment.getPropertySources().addLast(source);
this.processor.postProcessEnvironment(this.environment, getApplication());
assertThat(this.environment.getProperty("foo", Map.class)).isEmpty();
assertThat(this.environment.getProperty("foo")).isEmpty();
}
private SpringApplication getApplication() {