From bdd532cc20842ed09ff8dec31e58cf3ec8a456a3 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 29 Jan 2018 10:30:59 +0100 Subject: [PATCH] Clarify when a property was not renamed due to an incompatible type Closes gh-11794 --- .../migrator/PropertiesMigrationReport.java | 18 ++++++++++++++---- .../PropertiesMigrationReporterTests.java | 12 ++++++++++++ .../config-error-no-compatible-type.properties | 1 + .../metadata/type-conversion-metadata.json | 12 ++++++++++++ 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 spring-boot-project/spring-boot-properties-migrator/src/test/resources/config/config-error-no-compatible-type.properties diff --git a/spring-boot-project/spring-boot-properties-migrator/src/main/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReport.java b/spring-boot-project/spring-boot-properties-migrator/src/main/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReport.java index 7d59b67b7eb..778c7a7f0ed 100644 --- a/spring-boot-project/spring-boot-properties-migrator/src/main/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReport.java +++ b/spring-boot-project/spring-boot-properties-migrator/src/main/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReport.java @@ -25,6 +25,7 @@ import java.util.function.Function; import java.util.stream.Collectors; import org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty; +import org.springframework.boot.configurationmetadata.Deprecation; import org.springframework.util.StringUtils; /** @@ -74,10 +75,7 @@ class PropertiesMigrationReport { StringBuilder report = new StringBuilder(); report.append(String.format("%nThe use of configuration keys that are no longer " + "supported was found in the environment:%n%n")); - append(report, content, - (metadata) -> "Reason: " - + (StringUtils.hasText(metadata.getDeprecation().getShortReason()) - ? metadata.getDeprecation().getShortReason() : "none")); + append(report, content, this::determineReason); report.append(String.format("%n")); report.append("Please refer to the migration guide or reference guide for " + "potential alternatives."); @@ -85,6 +83,18 @@ class PropertiesMigrationReport { return report.toString(); } + private String determineReason(ConfigurationMetadataProperty metadata) { + Deprecation deprecation = metadata.getDeprecation(); + if (StringUtils.hasText(deprecation.getShortReason())) { + return deprecation.getShortReason(); + } + if (StringUtils.hasText(deprecation.getReplacement())) { + return String.format("Reason: Replacement key '%s' uses an incompatible " + + "target type", deprecation.getReplacement()); + } + return "none"; + } + private Map> getContent( Function> extractor) { return this.content.entrySet().stream() diff --git a/spring-boot-project/spring-boot-properties-migrator/src/test/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReporterTests.java b/spring-boot-project/spring-boot-properties-migrator/src/test/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReporterTests.java index 7e03fef0a20..1bf1ea30beb 100644 --- a/spring-boot-project/spring-boot-properties-migrator/src/test/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReporterTests.java +++ b/spring-boot-project/spring-boot-properties-migrator/src/test/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReporterTests.java @@ -141,6 +141,18 @@ public class PropertiesMigrationReporterTests { assertMappedProperty(propertySource, "test.mapped.ttl", 5678L, null); } + @Test + public void reasonIsProvidedIfPropertyCouldNotBeRenamed() throws IOException { + this.environment.getPropertySources().addFirst(loadPropertySource("test", + "config/config-error-no-compatible-type.properties")); + String report = createErrorReport( + loadRepository("metadata/type-conversion-metadata.json")); + assertThat(report).isNotNull(); + assertThat(report).containsSubsequence("Property source 'test'", + "wrong.inconvertible", "Line: 1", "Reason: Replacement key " + + "'test.inconvertible' uses an incompatible target type"); + } + private List mapToNames(PropertySources sources) { List names = new ArrayList<>(); for (PropertySource source : sources) { diff --git a/spring-boot-project/spring-boot-properties-migrator/src/test/resources/config/config-error-no-compatible-type.properties b/spring-boot-project/spring-boot-properties-migrator/src/test/resources/config/config-error-no-compatible-type.properties new file mode 100644 index 00000000000..4a1047b43af --- /dev/null +++ b/spring-boot-project/spring-boot-properties-migrator/src/test/resources/config/config-error-no-compatible-type.properties @@ -0,0 +1 @@ +wrong.inconvertible=abc diff --git a/spring-boot-project/spring-boot-properties-migrator/src/test/resources/metadata/type-conversion-metadata.json b/spring-boot-project/spring-boot-properties-migrator/src/test/resources/metadata/type-conversion-metadata.json index d58e2f60241..3c247d1ebdf 100644 --- a/spring-boot-project/spring-boot-properties-migrator/src/test/resources/metadata/type-conversion-metadata.json +++ b/spring-boot-project/spring-boot-properties-migrator/src/test/resources/metadata/type-conversion-metadata.json @@ -12,6 +12,10 @@ "name": "test.mapped", "type": "java.util.Map" }, + { + "name": "test.inconvertible", + "type": "com.example.One" + }, { "name": "test.cache-seconds", "type": "java.lang.Integer", @@ -35,6 +39,14 @@ "replacement": "test.mapped.ttl", "level": "error" } + }, + { + "name": "wrong.inconvertible", + "type": "com.example.Two", + "deprecation": { + "replacement": "test.inconvertible", + "level": "error" + } } ] } \ No newline at end of file