Skip to content

Commit

Permalink
Merge pull request quarkusio#30603 from mkouba/issue-30538
Browse files Browse the repository at this point in the history
Log a warning when a deprecated extension config item is used
  • Loading branch information
mkouba authored Jan 25, 2023
2 parents 080da4a + 19c4457 commit aeaf874
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
import org.wildfly.common.Assert;

import io.quarkus.deployment.configuration.definition.ClassDefinition;
import io.quarkus.deployment.configuration.definition.ClassDefinition.ClassMember;
import io.quarkus.deployment.configuration.definition.ClassDefinition.GroupMember;
import io.quarkus.deployment.configuration.definition.ClassDefinition.ItemMember;
import io.quarkus.deployment.configuration.definition.ClassDefinition.MapMember;
import io.quarkus.deployment.configuration.definition.GroupDefinition;
import io.quarkus.deployment.configuration.definition.RootDefinition;
import io.quarkus.deployment.configuration.matching.ConfigPatternMap;
Expand Down Expand Up @@ -91,6 +95,9 @@ public final class BuildTimeConfigurationReader {
final List<ConfigClassWithPrefix> runTimeMappings;
final List<ConfigClassWithPrefix> buildTimeVisibleMappings;

final Set<String> deprecatedProperties;
final Set<String> deprecatedRuntimeProperties;

/**
* Construct a new instance.
*
Expand Down Expand Up @@ -188,6 +195,9 @@ public BuildTimeConfigurationReader(final List<Class<?>> configRoots) {
buildTimeVisibleMappings = new ArrayList<>(buildTimeMappings.size() + buildTimeRunTimeMappings.size());
buildTimeVisibleMappings.addAll(buildTimeMappings);
buildTimeVisibleMappings.addAll(buildTimeRunTimeMappings);

deprecatedProperties = getDeprecatedProperties(allRoots);
deprecatedRuntimeProperties = getDeprecatedProperties(runTimeRoots);
}

private static void processClass(ClassDefinition.Builder builder, Class<?> clazz,
Expand Down Expand Up @@ -322,6 +332,57 @@ public ReadResult readConfiguration(final SmallRyeConfig config) {
return SecretKeys.doUnlocked(() -> new ReadOperation(config).run());
}

private Set<String> getDeprecatedProperties(Iterable<RootDefinition> rootDefinitions) {
Set<String> ret = new HashSet<>();
StringBuilder nameBuilder = new StringBuilder();
for (RootDefinition rootDefinition : rootDefinitions) {
int len = nameBuilder.length();
try {
nameBuilder.append(rootDefinition.getName());
collectDeprecatedConfigItems(rootDefinition, ret, nameBuilder);
} finally {
nameBuilder.setLength(len);
}
}
return Set.copyOf(ret);
}

private void collectDeprecatedConfigItems(ClassDefinition classDefinition, Set<String> deprecatedConfigItems,
StringBuilder nameBuilder) {
for (ClassMember m : classDefinition.getMembers()) {
collectDeprecatedConfigItems(m, deprecatedConfigItems, nameBuilder);
}
}

private void collectDeprecatedConfigItems(ClassMember classMember, Set<String> deprecatedConfigItems,
StringBuilder nameBuilder) {
if (classMember instanceof ItemMember) {
ItemMember im = (ItemMember) classMember;
if (im.getField().isAnnotationPresent(Deprecated.class)) {
int len = nameBuilder.length();
try {
nameBuilder.append(".").append(im.getPropertyName());
deprecatedConfigItems.add(nameBuilder.toString());
} finally {
nameBuilder.setLength(len);
}
}
} else if (classMember instanceof GroupMember) {
GroupMember gm = (GroupMember) classMember;
int len = nameBuilder.length();
try {
nameBuilder.append(".").append(gm.getPropertyName());
collectDeprecatedConfigItems(gm.getGroupDefinition(), deprecatedConfigItems, nameBuilder);
} finally {
nameBuilder.setLength(len);
}

} else if (classMember instanceof MapMember) {
MapMember mm = (MapMember) classMember;
collectDeprecatedConfigItems(mm.getNested(), deprecatedConfigItems, nameBuilder);
}
}

final class ReadOperation {
final SmallRyeConfig config;
final Set<String> processedNames = new HashSet<>();
Expand Down Expand Up @@ -374,6 +435,9 @@ ReadResult run() {
if (propertyName.equals(ConfigSource.CONFIG_ORDINAL)) {
continue;
}
if (deprecatedProperties.contains(propertyName)) {
log.warnf("The '%s' config property is deprecated and should not be used anymore", propertyName);
}

NameIterator ni = new NameIterator(propertyName);
if (ni.hasNext() && PropertiesUtil.isPropertyInRoot(registeredRoots, ni)) {
Expand Down Expand Up @@ -543,6 +607,7 @@ ReadResult run() {
.setBuildTimeRunTimeMappings(buildTimeRunTimeMappings)
.setRunTimeMappings(runTimeMappings)
.setUnknownBuildProperties(unknownBuildProperties)
.setDeprecatedRuntimeProperties(deprecatedRuntimeProperties)
.createReadResult();
}

Expand Down Expand Up @@ -1009,6 +1074,7 @@ public static final class ReadResult {
final Map<Class<?>, ConfigClassWithPrefix> allMappings;

final Set<String> unknownBuildProperties;
final Set<String> deprecatedRuntimeProperties;

public ReadResult(final Builder builder) {
this.objectsByClass = builder.getObjectsByClass();
Expand All @@ -1033,6 +1099,7 @@ public ReadResult(final Builder builder) {
this.allMappings = mappingsToMap(builder);

this.unknownBuildProperties = builder.getUnknownBuildProperties();
this.deprecatedRuntimeProperties = builder.deprecatedRuntimeProperties;
}

private static Map<Class<?>, RootDefinition> rootsToMap(Builder builder) {
Expand Down Expand Up @@ -1121,6 +1188,10 @@ public Set<String> getUnknownBuildProperties() {
return unknownBuildProperties;
}

public Set<String> getDeprecatedRuntimeProperties() {
return deprecatedRuntimeProperties;
}

public Object requireObjectForClass(Class<?> clazz) {
Object obj = objectsByClass.get(clazz);
if (obj == null) {
Expand All @@ -1144,6 +1215,7 @@ static class Builder {
private List<ConfigClassWithPrefix> buildTimeRunTimeMappings;
private List<ConfigClassWithPrefix> runTimeMappings;
private Set<String> unknownBuildProperties;
private Set<String> deprecatedRuntimeProperties;

Map<Class<?>, Object> getObjectsByClass() {
return objectsByClass;
Expand Down Expand Up @@ -1271,6 +1343,11 @@ Builder setUnknownBuildProperties(final Set<String> unknownBuildProperties) {
return this;
}

Builder setDeprecatedRuntimeProperties(Set<String> deprecatedRuntimeProperties) {
this.deprecatedRuntimeProperties = deprecatedRuntimeProperties;
return this;
}

ReadResult createReadResult() {
return new ReadResult(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.deployment.steps;

import static io.quarkus.deployment.annotations.ExecutionTime.RUNTIME_INIT;

import java.util.Set;

import io.quarkus.deployment.IsNormal;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.ConfigurationBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.runtime.configuration.DeprecatedRuntimePropertiesRecorder;

public class DeprecatedRuntimePropertiesBuildStep {

@BuildStep(onlyIf = IsNormal.class)
@Record(RUNTIME_INIT)
void reportDeprecatedProperties(LaunchModeBuildItem launchMode, ConfigurationBuildItem configItem,
DeprecatedRuntimePropertiesRecorder recorder) {
Set<String> deprecatedProperties = configItem.getReadResult().getDeprecatedRuntimeProperties();
if (!deprecatedProperties.isEmpty()) {
recorder.reportDeprecatedProperties(deprecatedProperties);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.runtime.configuration;

import java.util.Set;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.logging.Logger;

import io.quarkus.runtime.annotations.Recorder;

@Recorder
public class DeprecatedRuntimePropertiesRecorder {

private static final Logger log = Logger.getLogger(DeprecatedRuntimePropertiesRecorder.class);

public void reportDeprecatedProperties(Set<String> deprecatedRuntimeProperties) {
Config config = ConfigProvider.getConfig();
for (String property : config.getPropertyNames()) {
if (deprecatedRuntimeProperties.contains(property)) {
log.warnf("The '%s' config property is deprecated and should not be used anymore", property);
}
}
}
}

0 comments on commit aeaf874

Please sign in to comment.