Skip to content

Commit

Permalink
Validate that @ConfigMapping and @ConfigProperties can only be placed…
Browse files Browse the repository at this point in the history
… in interfaces and classes
  • Loading branch information
radcortez committed Jul 14, 2021
1 parent 32f0635 commit 1a54f05
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.microprofile.config.inject.ConfigProperties;

import io.smallrye.common.classloader.ClassDefiner;

public final class ConfigMappingLoader {
Expand Down Expand Up @@ -39,6 +41,8 @@ static ConfigMappingInterface getConfigMappingInterface(final Class<?> type) {
}

static Class<?> getConfigMappingClass(final Class<?> type) {
validateAnnotations(type);

final ConfigMappingClass configMappingClass = ConfigMappingClass.getConfigurationClass(type);
if (configMappingClass == null) {
return type;
Expand Down Expand Up @@ -86,6 +90,16 @@ static Class<?> loadClass(final Class<?> parent, final ConfigMappingMetadata con
}
}

static void validateAnnotations(Class<?> type) {
if (!type.isInterface() && type.isAnnotationPresent(ConfigMapping.class)) {
throw ConfigMessages.msg.mappingAnnotationNotSupportedInClass(type);
}

if (type.isInterface() && type.isAnnotationPresent(ConfigProperties.class)) {
throw ConfigMessages.msg.propertiesAnnotationNotSupportedInInterface(type);
}
}

/**
* Do not remove this method or inline it. It is keep separate on purpose, so it is easier to substitute it with
* the GraalVM API for native image compilation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,9 @@ IllegalArgumentException converterException(@Cause Throwable converterException,
@Message(id = 42, value = "Value does not match the expected map format \"<key1>=<value1>;<key2>=<value2>...\" (value was \"%s\")")
NoSuchElementException valueNotMatchMapFormat(String value);

@Message(id = 43, value = "The @ConfigMapping annotation can only be placed in interfaces, %s is a class")
IllegalStateException mappingAnnotationNotSupportedInClass(Class<?> type);

@Message(id = 44, value = "The @ConfigProperties annotation can only be placed in classes, %s is an interface")
IllegalStateException propertiesAnnotationNotSupportedInInterface(Class<?> type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -128,6 +129,31 @@ void validateDisableOnConfigProperties() {
assertEquals(8080, server.port);
}

@Test
void validateAnnotations() {
SmallRyeConfig config = new SmallRyeConfigBuilder().build();
IllegalStateException exception = assertThrows(IllegalStateException.class,
() -> registerConfigMappings(config, singleton(configClassWithPrefix(ServerMappingClass.class, "server"))));
assertTrue(exception.getMessage()
.startsWith("SRCFG00043: The @ConfigMapping annotation can only be placed in interfaces"));

exception = assertThrows(IllegalStateException.class,
() -> new SmallRyeConfigBuilder().withMapping(ServerMappingClass.class, "server").build());
assertTrue(exception.getMessage()
.startsWith("SRCFG00043: The @ConfigMapping annotation can only be placed in interfaces"));

exception = assertThrows(IllegalStateException.class,
() -> registerConfigMappings(config,
singleton(configClassWithPrefix(ServerPropertiesInterface.class, "server"))));
assertTrue(exception.getMessage()
.startsWith("SRCFG00044: The @ConfigProperties annotation can only be placed in classes"));

exception = assertThrows(IllegalStateException.class,
() -> new SmallRyeConfigBuilder().withMapping(ServerPropertiesInterface.class, "server").build());
assertTrue(exception.getMessage()
.startsWith("SRCFG00044: The @ConfigProperties annotation can only be placed in classes"));
}

@ConfigMapping(prefix = "server")
interface Server {
String host();
Expand All @@ -150,6 +176,16 @@ static class ServerClass {
Map<String, Integer> numbers;
}

@ConfigMapping(prefix = "server")
static class ServerMappingClass {
String host;
}

@ConfigProperties(prefix = "server")
interface ServerPropertiesInterface {
String host();
}

static class Version {
int id;
String name;
Expand All @@ -176,7 +212,6 @@ public int hashCode() {
}

static class VersionConverter implements Converter<Version> {

@Override
public Version convert(String value) {
return new Version(Integer.parseInt(value.substring(0, 1)), value.substring(2));
Expand Down

0 comments on commit 1a54f05

Please sign in to comment.