Browse Source

Avoid NPE when replacement property does not exist

See gh-15394
pull/16246/head
hdeadman 7 years ago committed by Stephane Nicoll
parent
commit
a1b71ef910
  1. 3
      spring-boot-project/spring-boot-properties-migrator/src/main/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReporter.java
  2. 13
      spring-boot-project/spring-boot-properties-migrator/src/test/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReporterTests.java
  3. 1
      spring-boot-project/spring-boot-properties-migrator/src/test/resources/config/config-error-invalid-replacement.properties
  4. 12
      spring-boot-project/spring-boot-properties-migrator/src/test/resources/metadata/sample-metadata-invalid-replacement.json

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

@ -137,6 +137,9 @@ class PropertiesMigrationReporter {
if (lastDot != -1) { if (lastDot != -1) {
ConfigurationMetadataProperty property = this.allProperties ConfigurationMetadataProperty property = this.allProperties
.get(fullId.substring(0, lastDot)); .get(fullId.substring(0, lastDot));
if (property == null) {
return null;
}
String type = property.getType(); String type = property.getType();
if (type != null && type.startsWith(Map.class.getName())) { if (type != null && type.startsWith(Map.class.getName())) {
int lastComma = type.lastIndexOf(','); int lastComma = type.lastIndexOf(',');

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

@ -152,6 +152,19 @@ public class PropertiesMigrationReporterTests {
+ "'test.inconvertible' uses an incompatible target type"); + "'test.inconvertible' uses an incompatible target type");
} }
@Test
public void invalidReplacementHandled() throws IOException {
this.environment.getPropertySources().addFirst(loadPropertySource("first",
"config/config-error-invalid-replacement.properties"));
String report = createErrorReport(
loadRepository("metadata/sample-metadata-invalid-replacement.json"));
assertThat(report).isNotNull();
assertThat(report).containsSubsequence("Property source 'first'",
"deprecated.six.test", "Line: 1", "Reason",
"Replacement key 'does.not.exist' uses an incompatible target type");
assertThat(report).doesNotContain("null");
}
private List<String> mapToNames(PropertySources sources) { private List<String> mapToNames(PropertySources sources) {
List<String> names = new ArrayList<>(); List<String> names = new ArrayList<>();
for (PropertySource<?> source : sources) { for (PropertySource<?> source : sources) {

1
spring-boot-project/spring-boot-properties-migrator/src/test/resources/config/config-error-invalid-replacement.properties

@ -0,0 +1 @@
deprecated.six.test=abc

12
spring-boot-project/spring-boot-properties-migrator/src/test/resources/metadata/sample-metadata-invalid-replacement.json

@ -0,0 +1,12 @@
{
"properties": [
{
"name": "deprecated.six.test",
"type": "java.lang.String",
"deprecation": {
"replacement": "does.not.exist",
"level": "error"
}
}
]
}
Loading…
Cancel
Save