Add since attribute to @DeprecatedConfigurationProperty annotation

Closes gh-36482
This commit is contained in:
Scott Frederick
2023-07-28 16:20:23 -05:00
parent 0646eabd4a
commit 2e50d11d86
18 changed files with 116 additions and 64 deletions

View File

@@ -104,12 +104,12 @@ class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGene
.fromSource(SimpleProperties.class)
.withDescription("The name of this simple properties.")
.withDefaultValue("boot")
.withDeprecation(null, null));
.withDeprecation());
assertThat(metadata).has(Metadata.withProperty("simple.flag", Boolean.class)
.withDefaultValue(false)
.fromSource(SimpleProperties.class)
.withDescription("A simple flag.")
.withDeprecation(null, null));
.withDeprecation());
assertThat(metadata).has(Metadata.withProperty("simple.comparator"));
assertThat(metadata).doesNotHave(Metadata.withProperty("simple.counter"));
assertThat(metadata).doesNotHave(Metadata.withProperty("simple.size"));
@@ -188,10 +188,9 @@ class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGene
ConfigurationMetadata metadata = compile(type);
assertThat(metadata).has(Metadata.withGroup("deprecated").fromSource(type));
assertThat(metadata)
.has(Metadata.withProperty("deprecated.name", String.class).fromSource(type).withDeprecation(null, null));
assertThat(metadata).has(Metadata.withProperty("deprecated.description", String.class)
.fromSource(type)
.withDeprecation(null, null));
.has(Metadata.withProperty("deprecated.name", String.class).fromSource(type).withDeprecation());
assertThat(metadata)
.has(Metadata.withProperty("deprecated.description", String.class).fromSource(type).withDeprecation());
}
@Test
@@ -202,7 +201,7 @@ class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGene
assertThat(metadata).has(Metadata.withProperty("singledeprecated.new-name", String.class).fromSource(type));
assertThat(metadata).has(Metadata.withProperty("singledeprecated.name", String.class)
.fromSource(type)
.withDeprecation("renamed", "singledeprecated.new-name"));
.withDeprecation("renamed", "singledeprecated.new-name", "1.2.3"));
}
@Test
@@ -210,9 +209,8 @@ class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGene
Class<?> type = DeprecatedFieldSingleProperty.class;
ConfigurationMetadata metadata = compile(type);
assertThat(metadata).has(Metadata.withGroup("singlefielddeprecated").fromSource(type));
assertThat(metadata).has(Metadata.withProperty("singlefielddeprecated.name", String.class)
.fromSource(type)
.withDeprecation(null, null));
assertThat(metadata)
.has(Metadata.withProperty("singlefielddeprecated.name", String.class).fromSource(type).withDeprecation());
}
@Test
@@ -246,7 +244,7 @@ class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGene
assertThat(metadata).has(Metadata.withGroup("deprecated-record").fromSource(type));
assertThat(metadata).has(Metadata.withProperty("deprecated-record.alpha", String.class)
.fromSource(type)
.withDeprecation("some-reason", null));
.withDeprecation("some-reason", null, null));
assertThat(metadata).has(Metadata.withProperty("deprecated-record.bravo", String.class).fromSource(type));
}

View File

@@ -43,7 +43,7 @@ class ImmutablePropertiesMetadataGenerationTests extends AbstractMetadataGenerat
.withDefaultValue(false)
.fromSource(ImmutableSimpleProperties.class)
.withDescription("A simple flag.")
.withDeprecation(null, null));
.withDeprecation());
assertThat(metadata).has(Metadata.withProperty("immutable.comparator"));
assertThat(metadata).has(Metadata.withProperty("immutable.counter"));
assertThat(metadata.getItems()).hasSize(5);

View File

@@ -139,10 +139,8 @@ class LombokMetadataGenerationTests extends AbstractMetadataGenerationTests {
.withDescription("Name description."));
assertThat(metadata).has(Metadata.withProperty(prefix + ".description"));
assertThat(metadata).has(Metadata.withProperty(prefix + ".counter"));
assertThat(metadata).has(Metadata.withProperty(prefix + ".number")
.fromSource(source)
.withDefaultValue(0)
.withDeprecation(null, null));
assertThat(metadata)
.has(Metadata.withProperty(prefix + ".number").fromSource(source).withDefaultValue(0).withDeprecation());
assertThat(metadata).has(Metadata.withProperty(prefix + ".items"));
assertThat(metadata).doesNotHave(Metadata.withProperty(prefix + ".ignored"));
}

View File

@@ -74,7 +74,7 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
assertThat(metadata).has(Metadata.withProperty("simple.flag", Boolean.class)
.fromSource(SimpleProperties.class)
.withDescription("A simple flag.")
.withDeprecation(null, null)
.withDeprecation()
.withDefaultValue(true));
assertThat(metadata.getItems()).hasSize(4);
}
@@ -125,36 +125,36 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
@Test
void mergeExistingPropertyDeprecation() throws Exception {
ItemMetadata property = ItemMetadata.newProperty("simple", "comparator", null, null, null, null, null,
new ItemDeprecation("Don't use this.", "simple.complex-comparator", "error"));
new ItemDeprecation("Don't use this.", "simple.complex-comparator", "1.2.3", "error"));
String additionalMetadata = buildAdditionalMetadata(property);
ConfigurationMetadata metadata = compile(additionalMetadata, SimpleProperties.class);
assertThat(metadata).has(Metadata.withProperty("simple.comparator", "java.util.Comparator<?>")
.fromSource(SimpleProperties.class)
.withDeprecation("Don't use this.", "simple.complex-comparator", "error"));
.withDeprecation("Don't use this.", "simple.complex-comparator", "1.2.3", "error"));
assertThat(metadata.getItems()).hasSize(4);
}
@Test
void mergeExistingPropertyDeprecationOverride() throws Exception {
ItemMetadata property = ItemMetadata.newProperty("singledeprecated", "name", null, null, null, null, null,
new ItemDeprecation("Don't use this.", "single.name"));
new ItemDeprecation("Don't use this.", "single.name", "1.2.3"));
String additionalMetadata = buildAdditionalMetadata(property);
ConfigurationMetadata metadata = compile(additionalMetadata, DeprecatedSingleProperty.class);
assertThat(metadata).has(Metadata.withProperty("singledeprecated.name", String.class.getName())
.fromSource(DeprecatedSingleProperty.class)
.withDeprecation("Don't use this.", "single.name"));
.withDeprecation("Don't use this.", "single.name", "1.2.3"));
assertThat(metadata.getItems()).hasSize(3);
}
@Test
void mergeExistingPropertyDeprecationOverrideLevel() throws Exception {
ItemMetadata property = ItemMetadata.newProperty("singledeprecated", "name", null, null, null, null, null,
new ItemDeprecation(null, null, "error"));
new ItemDeprecation(null, null, null, "error"));
String additionalMetadata = buildAdditionalMetadata(property);
ConfigurationMetadata metadata = compile(additionalMetadata, DeprecatedSingleProperty.class);
assertThat(metadata).has(Metadata.withProperty("singledeprecated.name", String.class.getName())
.fromSource(DeprecatedSingleProperty.class)
.withDeprecation("renamed", "singledeprecated.new-name", "error"));
.withDeprecation("renamed", "singledeprecated.new-name", "1.2.3", "error"));
assertThat(metadata.getItems()).hasSize(3);
}
@@ -175,7 +175,7 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
.fromSource(SimpleProperties.class)
.withDescription("The name of this simple properties.")
.withDefaultValue("boot")
.withDeprecation(null, null));
.withDeprecation());
assertThat(metadata)
.has(Metadata.withHint("simple.the-name").withValue(0, "boot", "Bla bla").withValue(1, "spring", null));
}
@@ -189,7 +189,7 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
.fromSource(SimpleProperties.class)
.withDescription("The name of this simple properties.")
.withDefaultValue("boot")
.withDeprecation(null, null));
.withDeprecation());
assertThat(metadata).has(Metadata.withHint("simple.the-name").withValue(0, "boot", "Bla bla"));
}
@@ -203,18 +203,19 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
.fromSource(SimpleProperties.class)
.withDescription("The name of this simple properties.")
.withDefaultValue("boot")
.withDeprecation(null, null));
.withDeprecation());
assertThat(metadata).has(
Metadata.withHint("simple.the-name").withProvider("first", "target", "org.foo").withProvider("second"));
}
@Test
void mergingOfAdditionalDeprecation() throws Exception {
String deprecations = buildPropertyDeprecations(ItemMetadata.newProperty("simple", "wrongName",
"java.lang.String", null, null, null, null, new ItemDeprecation("Lame name.", "simple.the-name")));
String deprecations = buildPropertyDeprecations(
ItemMetadata.newProperty("simple", "wrongName", "java.lang.String", null, null, null, null,
new ItemDeprecation("Lame name.", "simple.the-name", "1.2.3")));
ConfigurationMetadata metadata = compile(deprecations, SimpleProperties.class);
assertThat(metadata).has(Metadata.withProperty("simple.wrong-name", String.class)
.withDeprecation("Lame name.", "simple.the-name"));
.withDeprecation("Lame name.", "simple.the-name", "1.2.3"));
}
@Test
@@ -268,6 +269,9 @@ class MergeMetadataGenerationTests extends AbstractMetadataGenerationTests {
if (deprecation.getReplacement() != null) {
deprecationJson.put("replacement", deprecation.getReplacement());
}
if (deprecation.getSince() != null) {
deprecationJson.put("since", deprecation.getSince());
}
jsonObject.put("deprecation", deprecationJson);
}
propertiesArray.put(jsonObject);

View File

@@ -114,11 +114,11 @@ class MethodBasedMetadataGenerationTests extends AbstractMetadataGenerationTests
assertThat(metadata).has(Metadata.withGroup("foo").fromSource(type));
assertThat(metadata).has(Metadata.withProperty("foo.name", String.class)
.fromSource(DeprecatedMethodConfig.Foo.class)
.withDeprecation(null, null));
.withDeprecation());
assertThat(metadata).has(Metadata.withProperty("foo.flag", Boolean.class)
.withDefaultValue(false)
.fromSource(DeprecatedMethodConfig.Foo.class)
.withDeprecation(null, null));
.withDeprecation());
}
@Test
@@ -129,11 +129,11 @@ class MethodBasedMetadataGenerationTests extends AbstractMetadataGenerationTests
assertThat(metadata).has(Metadata.withGroup("foo").fromSource(type));
assertThat(metadata).has(Metadata.withProperty("foo.name", String.class)
.fromSource(org.springframework.boot.configurationsample.method.DeprecatedClassMethodConfig.Foo.class)
.withDeprecation(null, null));
.withDeprecation());
assertThat(metadata).has(Metadata.withProperty("foo.flag", Boolean.class)
.withDefaultValue(false)
.fromSource(org.springframework.boot.configurationsample.method.DeprecatedClassMethodConfig.Foo.class)
.withDeprecation(null, null));
.withDeprecation());
}
}

View File

@@ -39,7 +39,7 @@ class JsonMarshallerTests {
void marshallAndUnmarshal() throws Exception {
ConfigurationMetadata metadata = new ConfigurationMetadata();
metadata.add(ItemMetadata.newProperty("a", "b", StringBuffer.class.getName(), InputStream.class.getName(),
"sourceMethod", "desc", "x", new ItemDeprecation("Deprecation comment", "b.c.d")));
"sourceMethod", "desc", "x", new ItemDeprecation("Deprecation comment", "b.c.d", "1.2.3")));
metadata.add(ItemMetadata.newProperty("b.c.d", null, null, null, null, null, null, null));
metadata.add(ItemMetadata.newProperty("c", null, null, null, null, null, 123, null));
metadata.add(ItemMetadata.newProperty("d", null, null, null, null, null, true, null));
@@ -59,7 +59,7 @@ class JsonMarshallerTests {
.fromSource(InputStream.class)
.withDescription("desc")
.withDefaultValue("x")
.withDeprecation("Deprecation comment", "b.c.d"));
.withDeprecation("Deprecation comment", "b.c.d", "1.2.3"));
assertThat(read).has(Metadata.withProperty("b.c.d"));
assertThat(read).has(Metadata.withProperty("c").withDefaultValue(123));
assertThat(read).has(Metadata.withProperty("d").withDefaultValue(true));
@@ -96,10 +96,10 @@ class JsonMarshallerTests {
ConfigurationMetadata metadata = new ConfigurationMetadata();
metadata.add(ItemMetadata.newProperty("com.example.bravo", "bbb", null, null, null, null, null, null));
metadata.add(ItemMetadata.newProperty("com.example.bravo", "aaa", null, null, null, null, null,
new ItemDeprecation(null, null, "warning")));
new ItemDeprecation(null, null, null, "warning")));
metadata.add(ItemMetadata.newProperty("com.example.alpha", "ddd", null, null, null, null, null, null));
metadata.add(ItemMetadata.newProperty("com.example.alpha", "ccc", null, null, null, null, null,
new ItemDeprecation(null, null, "warning")));
new ItemDeprecation(null, null, null, "warning")));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonMarshaller marshaller = new JsonMarshaller();
marshaller.write(metadata, outputStream);

View File

@@ -193,13 +193,17 @@ public final class Metadata {
this.description, defaultValue, this.deprecation);
}
public MetadataItemCondition withDeprecation(String reason, String replacement) {
return withDeprecation(reason, replacement, null);
public MetadataItemCondition withDeprecation() {
return withDeprecation(null, null, null, null);
}
public MetadataItemCondition withDeprecation(String reason, String replacement, String level) {
public MetadataItemCondition withDeprecation(String reason, String replacement, String since) {
return withDeprecation(reason, replacement, since, null);
}
public MetadataItemCondition withDeprecation(String reason, String replacement, String since, String level) {
return new MetadataItemCondition(this.itemType, this.name, this.type, this.sourceType, this.sourceMethod,
this.description, this.defaultValue, new ItemDeprecation(reason, replacement, level));
this.description, this.defaultValue, new ItemDeprecation(reason, replacement, since, level));
}
public MetadataItemCondition withNoDeprecation() {

View File

@@ -50,4 +50,10 @@ public @interface DeprecatedConfigurationProperty {
*/
String replacement() default "";
/**
* The version in which the property became deprecated.
* @return the version
*/
String since() default "";
}

View File

@@ -30,7 +30,7 @@ public class DeprecatedSingleProperty {
private String newName;
@Deprecated
@DeprecatedConfigurationProperty(reason = "renamed", replacement = "singledeprecated.new-name")
@DeprecatedConfigurationProperty(reason = "renamed", replacement = "singledeprecated.new-name", since = "1.2.3")
public String getName() {
return getNewName();
}