Don't report already migrated properties

Update `PropertiesMigrationReporter` so that already migrated properties
are not reported. Prior to this commit, if a deprecated property was
replaced by a property that could bind with the name relaxed name it
would be reported. For example: `test.someproperty` being replaced with
`test.some-property`.

In order to check the actual underlying property name, the
`PropertySourceOrigin` class has been updated so that it is always
returned, even if another `Origin` is available.

Fixes gh-35774
This commit is contained in:
Phillip Webb
2024-06-26 16:18:30 -07:00
parent 07442f8366
commit 962936370a
7 changed files with 172 additions and 84 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
@@ -31,6 +31,7 @@ import org.springframework.boot.configurationmetadata.SimpleConfigurationMetadat
import org.springframework.boot.env.PropertiesPropertySourceLoader;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginLookup;
import org.springframework.boot.origin.PropertySourceOrigin;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
@@ -87,6 +88,13 @@ class PropertiesMigrationReporterTests {
assertThat(report).doesNotContain("wrong.one");
}
@Test
void warningReportReplacedWithSameRelaxedName() throws IOException {
this.environment.getPropertySources().addFirst(loadPropertySource("test", "config/config-relaxed.properties"));
String report = createWarningReport(loadRepository("metadata/sample-metadata.json"));
assertThat(report).isNull();
}
@Test
void errorReport() throws IOException {
this.environment.getPropertySources()
@@ -232,7 +240,11 @@ class PropertiesMigrationReporterTests {
assertThat(propertySource.getProperty(name)).isEqualTo(value);
if (origin != null) {
assertThat(propertySource).isInstanceOf(OriginLookup.class);
assertThat(((OriginLookup<Object>) propertySource).getOrigin(name)).isEqualTo(origin);
Origin actualOrigin = ((OriginLookup<Object>) propertySource).getOrigin(name);
if (actualOrigin instanceof PropertySourceOrigin propertySourceOrigin) {
actualOrigin = propertySourceOrigin.getOrigin();
}
assertThat(actualOrigin).isEqualTo(origin);
}
}

View File

@@ -54,6 +54,17 @@
{
"name": "custom.the-map-replacement",
"type": "java.util.Map<java.lang.String,java.lang.String>"
},
{
"name": "relaxed.thisthat-theother",
"type": "java.lang.String",
"deprecation": {
"replacement": "relaxed.this-that-the-other"
}
},
{
"name": "relaxed.this-that-the-other",
"type": "java.lang.String"
}
]
}