From 7c65c3e260a7df15870b66b2352bdf81573b54e2 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 17 Nov 2022 20:50:26 +0000 Subject: [PATCH] Handle invalid names gracefully in properties migrator Closes gh-32729 --- .../migrator/PropertiesMigrationReporter.java | 8 +++++--- .../PropertiesMigrationReporterTests.java | 12 +++++++++++- .../metadata/sample-metadata-invalid-name.json | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 spring-boot-project/spring-boot-properties-migrator/src/test/resources/metadata/sample-metadata-invalid-name.json diff --git a/spring-boot-project/spring-boot-properties-migrator/src/main/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReporter.java b/spring-boot-project/spring-boot-properties-migrator/src/main/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReporter.java index d8d99d2c64ed..28bd7d1a85c7 100644 --- a/spring-boot-project/spring-boot-properties-migrator/src/main/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReporter.java +++ b/spring-boot-project/spring-boot-properties-migrator/src/main/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationReporter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -99,8 +99,10 @@ private Map> getMatchingProperties( List candidates = this.allProperties.values().stream().filter(filter) .collect(Collectors.toList()); getPropertySourcesAsMap().forEach((name, source) -> candidates.forEach((metadata) -> { - ConfigurationProperty configurationProperty = source - .getConfigurationProperty(ConfigurationPropertyName.of(metadata.getId())); + ConfigurationPropertyName metadataName = ConfigurationPropertyName.isValid(metadata.getId()) + ? ConfigurationPropertyName.of(metadata.getId()) + : ConfigurationPropertyName.adapt(metadata.getId(), '.'); + ConfigurationProperty configurationProperty = source.getConfigurationProperty(metadataName); if (configurationProperty != null) { result.add(name, new PropertyMigration(configurationProperty, metadata, determineReplacementMetadata(metadata))); 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 4fed9c1902fc..157e4c4e50de 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -144,6 +145,15 @@ void invalidReplacementHandled() throws IOException { assertThat(report).doesNotContain("null"); } + @Test + void invalidNameHandledGracefully() { + this.environment.getPropertySources() + .addFirst(new MapPropertySource("first", Collections.singletonMap("invalid.property-name", "value"))); + String report = createWarningReport(loadRepository("metadata/sample-metadata-invalid-name.json")); + assertThat(report).isNotNull(); + assertThat(report).contains("Key: invalid.PropertyName").contains("Replacement: valid.property-name"); + } + 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/metadata/sample-metadata-invalid-name.json b/spring-boot-project/spring-boot-properties-migrator/src/test/resources/metadata/sample-metadata-invalid-name.json new file mode 100644 index 000000000000..b225ab036f68 --- /dev/null +++ b/spring-boot-project/spring-boot-properties-migrator/src/test/resources/metadata/sample-metadata-invalid-name.json @@ -0,0 +1,16 @@ +{ + "properties": [ + { + "name": "invalid.PropertyName", + "type": "java.lang.String", + "deprecation": { + "replacement": "valid.property-name", + "level": "error" + } + }, + { + "name": "valid.property-name", + "type": "java.lang.String" + } + ] +} \ No newline at end of file