Skip to content

Commit

Permalink
Merge pull request quarkusio#18491 from geoand/quarkusio#18473
Browse files Browse the repository at this point in the history
Fix injection of indexed based configuration
  • Loading branch information
geoand authored Jul 7, 2021
2 parents 8c7075e + fe4c3eb commit 80a04f0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.jboss.jandex.AnnotationValue.createStringValue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -182,9 +183,18 @@ void validateConfigProperties(ConfigRecorder recorder, List<ConfigPropertyBuildI

Set<ConfigValidationMetadata> propertiesToValidate = new HashSet<>();
for (ConfigPropertyBuildItem configProperty : configProperties) {
String rawTypeName = configProperty.getPropertyType().name().toString();
List<String> actualTypeArgumentNames = Collections.emptyList();
if (configProperty.getPropertyType().kind() == Kind.PARAMETERIZED_TYPE) {
List<Type> argumentTypes = configProperty.getPropertyType().asParameterizedType().arguments();
actualTypeArgumentNames = new ArrayList<>(argumentTypes.size());
for (Type argumentType : argumentTypes) {
actualTypeArgumentNames.add(argumentType.name().toString());
}

}
propertiesToValidate.add(new ConfigValidationMetadata(configProperty.getPropertyName(),
configProperty.getPropertyType().name().toString(),
configProperty.getDefaultValue()));
rawTypeName, actualTypeArgumentNames, configProperty.getDefaultValue()));
}

recorder.validateConfigProperties(propertiesToValidate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.stream.Stream;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.spi.DeploymentException;
import javax.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperty;
Expand Down Expand Up @@ -48,8 +47,7 @@ public class IndexedPropertiesInjectionTest {
"optionals.indexed[1]=b\n" +
"supplier.indexed[0]=a\n" +
"supplier.indexed[1]=b\n"),
"application.properties"))
.setExpectedException(DeploymentException.class);
"application.properties"));

@Inject
IndexedBean indexedBean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.arc.runtime;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand All @@ -9,6 +11,8 @@
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;

import io.quarkus.arc.impl.ParameterizedTypeImpl;
import io.quarkus.runtime.annotations.RecordableConstructor;
import io.quarkus.runtime.annotations.Recorder;
import io.smallrye.config.ConfigMappings;
import io.smallrye.config.ConfigMappings.ConfigClassWithPrefix;
Expand All @@ -30,17 +34,27 @@ public void validateConfigProperties(Set<ConfigValidationMetadata> properties) {
}

for (ConfigValidationMetadata property : properties) {
Class<?> propertyClass = load(property.getType(), cl);
Class<?> propertyType = load(property.getRawTypeName(), cl);
Type effectivePropertyType = propertyType;
// For parameterized types and arrays, we only check if the property config exists without trying to convert it
if (propertyClass.isArray() || (propertyClass.getTypeParameters().length > 0 && propertyClass != Map.class)) {
propertyClass = String.class;
if (propertyType.isArray() || (propertyType.getTypeParameters().length > 0 && propertyType != Map.class
&& propertyType != List.class && propertyType != Set.class)) {
effectivePropertyType = String.class;
} else if (property.getActualTypeArgumentNames().size() > 0) {
// this is a really simplified way of constructing the generic types, but we don't need anything more complex
// here due to what SR Config checks (which is basically if the type is a collection)
Type[] genericTypes = new Type[(property.getActualTypeArgumentNames().size())];
for (int i = 0; i < property.getActualTypeArgumentNames().size(); i++) {
genericTypes[i] = load(property.getActualTypeArgumentNames().get(i), cl);
}
effectivePropertyType = new ParameterizedTypeImpl(propertyType, genericTypes);
}

try {
ConfigProducerUtil.getValue(property.getName(), propertyClass, property.getDefaultValue(), config);
ConfigProducerUtil.getValue(property.getName(), effectivePropertyType, property.getDefaultValue(), config);
} catch (Exception e) {
throw new DeploymentException(
"Failed to load config value of type " + propertyClass + " for: " + property.getName(), e);
"Failed to load config value of type " + effectivePropertyType + " for: " + property.getName(), e);
}
}
}
Expand Down Expand Up @@ -94,15 +108,16 @@ private Class<?> load(String className, ClassLoader cl) {

public static class ConfigValidationMetadata {
private String name;
private String type;
private String rawTypeName;
private List<String> actualTypeArgumentNames;
private String defaultValue;

public ConfigValidationMetadata() {
}

public ConfigValidationMetadata(final String name, final String type, final String defaultValue) {
@RecordableConstructor
public ConfigValidationMetadata(final String name, final String rawTypeName, List<String> actualTypeArgumentNames,
final String defaultValue) {
this.name = name;
this.type = type;
this.rawTypeName = rawTypeName;
this.actualTypeArgumentNames = actualTypeArgumentNames;
this.defaultValue = defaultValue;
}

Expand All @@ -114,39 +129,33 @@ public void setName(final String name) {
this.name = name;
}

public String getType() {
return type;
public String getRawTypeName() {
return rawTypeName;
}

public void setType(final String type) {
this.type = type;
public List<String> getActualTypeArgumentNames() {
return actualTypeArgumentNames;
}

public String getDefaultValue() {
return defaultValue;
}

public void setDefaultValue(final String defaultValue) {
this.defaultValue = defaultValue;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
public boolean equals(Object o) {
if (this == o)
return true;
}
if (o == null || getClass() != o.getClass()) {
if (o == null || getClass() != o.getClass())
return false;
}
final ConfigValidationMetadata that = (ConfigValidationMetadata) o;
return name.equals(that.name) &&
type.equals(that.type) &&
Objects.equals(defaultValue, that.defaultValue);
ConfigValidationMetadata that = (ConfigValidationMetadata) o;
return name.equals(that.name) && rawTypeName.equals(that.rawTypeName)
&& actualTypeArgumentNames.equals(that.actualTypeArgumentNames)
&& Objects.equals(defaultValue, that.defaultValue);
}

@Override
public int hashCode() {
return Objects.hash(name, type, defaultValue);
return Objects.hash(name, rawTypeName, actualTypeArgumentNames, defaultValue);
}
}
}

0 comments on commit 80a04f0

Please sign in to comment.