Skip to content

Commit

Permalink
Merge pull request #19570 from geoand/#19366
Browse files Browse the repository at this point in the history
Provide clear build-time error message when using Map in @ConfigProperties
  • Loading branch information
gsmet authored Aug 24, 2021
2 parents 77e8f88 + b7524c0 commit fb575c1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.Optional;
import java.util.function.IntFunction;

import javax.enterprise.inject.spi.DeploymentException;

import org.eclipse.microprofile.config.Config;
import org.jboss.jandex.DotName;
import org.jboss.jandex.ParameterizedType;
Expand Down Expand Up @@ -42,6 +44,10 @@ static ResultHandle createReadMandatoryValueAndConvertIfNeeded(String propertyNa
DotName declaringClass,
BytecodeCreator bytecodeCreator, ResultHandle config) {

if (isMap(resultType)) {
throw new DeploymentException(
"Using a Map is not supported for classes annotated with '@ConfigProperties'. Consider using https://quarkus.io/guides/config-mappings instead.");
}
if (isCollection(resultType)) {
ResultHandle smallryeConfig = bytecodeCreator.checkCast(config, SmallRyeConfig.class);

Expand Down Expand Up @@ -79,6 +85,10 @@ static ReadOptionalResponse createReadOptionalValueAndConvertIfNeeded(String pro
BytecodeCreator bytecodeCreator, ResultHandle config) {

ResultHandle optionalValue;
if (isMap(resultType)) {
throw new DeploymentException(
"Using a Map is not supported for classes annotated with '@ConfigProperties'. Consider using https://quarkus.io/guides/config-mappings instead.");
}
if (isCollection(resultType)) {
ResultHandle smallryeConfig = bytecodeCreator.checkCast(config, SmallRyeConfig.class);

Expand Down Expand Up @@ -130,6 +140,11 @@ private static boolean isCollection(final Type resultType) {
DotNames.SET.equals(resultType.name());
}

private static boolean isMap(final Type resultType) {
return DotNames.MAP.equals(resultType.name()) ||
DotNames.HASH_MAP.equals(resultType.name());
}

static Type determineSingleGenericType(Type type, DotName declaringClass) {
if (!(type.kind() == Type.Kind.PARAMETERIZED_TYPE)) {
throw new IllegalArgumentException("Type " + type.name().toString() + " which is used in class " + declaringClass
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.quarkus.arc.deployment.configproperties;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

Expand All @@ -23,6 +25,8 @@ private DotNames() {
static final DotName LIST = DotName.createSimple(List.class.getName());
static final DotName SET = DotName.createSimple(Set.class.getName());
static final DotName COLLECTION = DotName.createSimple(Collection.class.getName());
static final DotName MAP = DotName.createSimple(Map.class.getName());
static final DotName HASH_MAP = DotName.createSimple(HashMap.class.getName());
static final DotName ENUM = DotName.createSimple(Enum.class.getName());
static final DotName MP_CONFIG_PROPERTIES = DotName
.createSimple(org.eclipse.microprofile.config.inject.ConfigProperties.class.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.quarkus.arc.test.configproperties;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.Map;

import javax.enterprise.inject.spi.DeploymentException;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.config.ConfigProperties;
import io.quarkus.test.QuarkusUnitTest;

public class MapTest {

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClasses(WithMap.class))
.assertException(e -> {
assertEquals(DeploymentException.class, e.getClass());
assertTrue(e.getMessage().contains("config-mappings"));
});

@Test
public void shouldNotBeInvoked() {
// This method should not be invoked
fail();
}

@ConfigProperties
public static class WithMap {

public String name;
public Map<String, String> other;
}
}

0 comments on commit fb575c1

Please sign in to comment.