diff --git a/core/spring-boot/src/main/java/org/springframework/boot/support/SpringApplicationJsonEnvironmentPostProcessor.java b/core/spring-boot/src/main/java/org/springframework/boot/support/SpringApplicationJsonEnvironmentPostProcessor.java index 16747285d2b..f24083579f4 100644 --- a/core/spring-boot/src/main/java/org/springframework/boot/support/SpringApplicationJsonEnvironmentPostProcessor.java +++ b/core/spring-boot/src/main/java/org/springframework/boot/support/SpringApplicationJsonEnvironmentPostProcessor.java @@ -131,14 +131,14 @@ public class SpringApplicationJsonEnvironmentPostProcessor implements Environmen private void extract(String name, Map result, Object value) { if (value instanceof Map map) { if (CollectionUtils.isEmpty(map)) { - result.put(name, value); + result.put(name, ""); return; } flatten(name, result, (Map) 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 : ""); } } diff --git a/core/spring-boot/src/test/java/org/springframework/boot/support/SpringApplicationJsonEnvironmentPostProcessorTests.java b/core/spring-boot/src/test/java/org/springframework/boot/support/SpringApplicationJsonEnvironmentPostProcessorTests.java index bf31369ab37..8f2c41d68b4 100644 --- a/core/spring-boot/src/test/java/org/springframework/boot/support/SpringApplicationJsonEnvironmentPostProcessorTests.java +++ b/core/spring-boot/src/test/java/org/springframework/boot/support/SpringApplicationJsonEnvironmentPostProcessorTests.java @@ -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() {