Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide clear build-time error message when using Map in @ConfigProperties #19570

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}