Skip to content

Commit 6723cfd

Browse files
committed
Improve null-safety of core/spring-boot
See gh-46926
1 parent 12bf450 commit 6723cfd

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

core/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import org.springframework.boot.ansi.AnsiPropertySource;
3434
import org.springframework.core.env.ConfigurableEnvironment;
35+
import org.springframework.core.env.EnumerablePropertySource;
3536
import org.springframework.core.env.Environment;
3637
import org.springframework.core.env.MapPropertySource;
3738
import org.springframework.core.env.MutablePropertySources;
@@ -41,6 +42,7 @@
4142
import org.springframework.core.log.LogMessage;
4243
import org.springframework.util.Assert;
4344
import org.springframework.util.StreamUtils;
45+
import org.springframework.util.StringUtils;
4446

4547
/**
4648
* Banner implementation that prints from a source text {@link Resource}.
@@ -112,11 +114,11 @@ private MutablePropertySources createEmptyDefaultSources(Environment environment
112114
return emptyDefaultSources;
113115
}
114116

115-
private MapPropertySource getTitleSource(@Nullable Class<?> sourceClass, @Nullable String defaultValue) {
117+
private MapWithNullsPropertySource getTitleSource(@Nullable Class<?> sourceClass, @Nullable String defaultValue) {
116118
String applicationTitle = getApplicationTitle(sourceClass);
117-
Map<String, Object> titleMap = Collections.singletonMap("application.title",
119+
Map<String, @Nullable Object> titleMap = Collections.singletonMap("application.title",
118120
(applicationTitle != null) ? applicationTitle : defaultValue);
119-
return new MapPropertySource("title", titleMap);
121+
return new MapWithNullsPropertySource("title", titleMap);
120122
}
121123

122124
/**
@@ -134,14 +136,14 @@ private AnsiPropertySource getAnsiSource() {
134136
return new AnsiPropertySource("ansi", true);
135137
}
136138

137-
private MapPropertySource getVersionSource(Environment environment, @Nullable String defaultValue) {
138-
return new MapPropertySource("version", getVersionsMap(environment, defaultValue));
139+
private MapWithNullsPropertySource getVersionSource(Environment environment, @Nullable String defaultValue) {
140+
return new MapWithNullsPropertySource("version", getVersionsMap(environment, defaultValue));
139141
}
140142

141-
private Map<String, Object> getVersionsMap(Environment environment, @Nullable String defaultValue) {
143+
private Map<String, @Nullable Object> getVersionsMap(Environment environment, @Nullable String defaultValue) {
142144
String appVersion = getApplicationVersion(environment);
143145
String bootVersion = getBootVersion();
144-
Map<String, Object> versions = new HashMap<>();
146+
Map<String, @Nullable Object> versions = new HashMap<>();
145147
versions.put("application.version", getVersionString(appVersion, false, defaultValue));
146148
versions.put("spring-boot.version", getVersionString(bootVersion, false, defaultValue));
147149
versions.put("application.formatted-version", getVersionString(appVersion, true, defaultValue));
@@ -164,4 +166,30 @@ protected String getBootVersion() {
164166
return format ? " (v" + version + ")" : version;
165167
}
166168

169+
/**
170+
* Like {@link MapPropertySource}, but allows {@code null} as map values.
171+
*/
172+
private static class MapWithNullsPropertySource extends EnumerablePropertySource<Map<String, @Nullable Object>> {
173+
174+
MapWithNullsPropertySource(String name, Map<String, @Nullable Object> source) {
175+
super(name, source);
176+
}
177+
178+
@Override
179+
public String[] getPropertyNames() {
180+
return StringUtils.toStringArray(this.source.keySet());
181+
}
182+
183+
@Override
184+
public @Nullable Object getProperty(String name) {
185+
return this.source.get(name);
186+
}
187+
188+
@Override
189+
public boolean containsProperty(String name) {
190+
return this.source.containsKey(name);
191+
}
192+
193+
}
194+
167195
}

0 commit comments

Comments
 (0)