Skip to content

Commit a1b71ef

Browse files
hdeadmansnicoll
authored andcommitted
Avoid NPE when replacement property does not exist
See spring-projectsgh-15394
1 parent 6906859 commit a1b71ef

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

spring-boot-project/spring-boot-properties-migrator/src/main/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReporter.java

+3
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ private String detectMapValueReplacementType(String fullId) {
137137
if (lastDot != -1) {
138138
ConfigurationMetadataProperty property = this.allProperties
139139
.get(fullId.substring(0, lastDot));
140+
if (property == null) {
141+
return null;
142+
}
140143
String type = property.getType();
141144
if (type != null && type.startsWith(Map.class.getName())) {
142145
int lastComma = type.lastIndexOf(',');

spring-boot-project/spring-boot-properties-migrator/src/test/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReporterTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,19 @@ public void reasonIsProvidedIfPropertyCouldNotBeRenamed() throws IOException {
152152
+ "'test.inconvertible' uses an incompatible target type");
153153
}
154154

155+
@Test
156+
public void invalidReplacementHandled() throws IOException {
157+
this.environment.getPropertySources().addFirst(loadPropertySource("first",
158+
"config/config-error-invalid-replacement.properties"));
159+
String report = createErrorReport(
160+
loadRepository("metadata/sample-metadata-invalid-replacement.json"));
161+
assertThat(report).isNotNull();
162+
assertThat(report).containsSubsequence("Property source 'first'",
163+
"deprecated.six.test", "Line: 1", "Reason",
164+
"Replacement key 'does.not.exist' uses an incompatible target type");
165+
assertThat(report).doesNotContain("null");
166+
}
167+
155168
private List<String> mapToNames(PropertySources sources) {
156169
List<String> names = new ArrayList<>();
157170
for (PropertySource<?> source : sources) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
deprecated.six.test=abc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"properties": [
3+
{
4+
"name": "deprecated.six.test",
5+
"type": "java.lang.String",
6+
"deprecation": {
7+
"replacement": "does.not.exist",
8+
"level": "error"
9+
}
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)