Skip to content

Commit

Permalink
Update SmallRye Config to 2.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
essobedo authored and radcortez committed Jun 28, 2021
1 parent c611a45 commit 73b7594
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 25 deletions.
2 changes: 1 addition & 1 deletion bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<microprofile-rest-client.version>2.0</microprofile-rest-client.version>
<microprofile-jwt.version>1.2</microprofile-jwt.version>
<smallrye-common.version>1.6.0</smallrye-common.version>
<smallrye-config.version>2.3.0</smallrye-config.version>
<smallrye-config.version>2.4.0</smallrye-config.version>
<smallrye-health.version>3.0.2</smallrye-health.version>
<smallrye-metrics.version>3.0.1</smallrye-metrics.version>
<smallrye-open-api.version>2.1.6</smallrye-open-api.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import com.oracle.svm.core.annotate.RecomputeFieldValue;

import io.smallrye.config.common.utils.StringUtil;

/**
* Utility methods to log configuration problems.
*/
Expand Down Expand Up @@ -77,14 +79,14 @@ public static void unknownProperties(List<String> properties) {
continue;
}

usedProperties.add(replaceNonAlphanumericByUnderscores(property));
usedProperties.add(StringUtil.replaceNonAlphanumericByUnderscores(property));
}
usedProperties.removeAll(properties);

for (String property : properties) {
boolean found = false;
for (String usedProperty : usedProperties) {
if (usedProperty.equalsIgnoreCase(replaceNonAlphanumericByUnderscores(property))) {
if (usedProperty.equalsIgnoreCase(StringUtil.replaceNonAlphanumericByUnderscores(property))) {
found = true;
break;
}
Expand Down Expand Up @@ -137,20 +139,4 @@ public static String getNiceErrorMessage() {
}
return b.toString();
}

private static String replaceNonAlphanumericByUnderscores(final String name) {
int length = name.length();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
char c = name.charAt(i);
if ('a' <= c && c <= 'z' ||
'A' <= c && c <= 'Z' ||
'0' <= c && c <= '9') {
sb.append(c);
} else {
sb.append('_');
}
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.function.BiConsumer;
import java.util.function.IntFunction;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;
Expand All @@ -35,10 +34,10 @@
import io.smallrye.config.ConfigSourceInterceptorFactory;
import io.smallrye.config.DotEnvConfigSourceProvider;
import io.smallrye.config.EnvConfigSource;
import io.smallrye.config.Expressions;
import io.smallrye.config.FallbackConfigSourceInterceptor;
import io.smallrye.config.Priorities;
import io.smallrye.config.RelocateConfigSourceInterceptor;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.SmallRyeConfigBuilder;
import io.smallrye.config.SysPropConfigSource;
import io.smallrye.config.common.utils.ConfigSourceUtil;
Expand Down Expand Up @@ -194,8 +193,7 @@ public static void addSourceProviders(SmallRyeConfigBuilder builder, Collection<
* @return true if the property is present or false otherwise.
*/
public static boolean isPropertyPresent(String propertyName) {
Config config = ConfigProvider.getConfig();
return Expressions.withoutExpansion(() -> config.getOptionalValue(propertyName, String.class)).isPresent();
return ConfigProvider.getConfig().unwrap(SmallRyeConfig.class).isPropertyPresent(propertyName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
Expand Down Expand Up @@ -73,6 +74,7 @@ public class ConfigBuildStep {
private static final DotName MP_CONFIG_VALUE_NAME = DotName.createSimple(ConfigValue.class.getName());

private static final DotName CONFIG_MAPPING_NAME = DotName.createSimple(ConfigMapping.class.getName());
private static final DotName MAP_NAME = DotName.createSimple(Map.class.getName());
private static final DotName SET_NAME = DotName.createSimple(Set.class.getName());
private static final DotName LIST_NAME = DotName.createSimple(List.class.getName());
private static final DotName SUPPLIER_NAME = DotName.createSimple(Supplier.class.getName());
Expand Down Expand Up @@ -426,6 +428,7 @@ public static boolean isHandledByProducers(Type type) {
DotNames.OPTIONAL_INT.equals(type.name()) ||
DotNames.OPTIONAL_LONG.equals(type.name()) ||
DotNames.OPTIONAL_DOUBLE.equals(type.name()) ||
MAP_NAME.equals(type.name()) ||
SET_NAME.equals(type.name()) ||
LIST_NAME.equals(type.name()) ||
DotNames.LONG.equals(type.name()) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.Map;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.spi.CDI;
import javax.inject.Inject;
Expand All @@ -26,10 +28,16 @@ public class ConfigPropertiesTest {
"smallrye.config.mapping.validate-unknown=false\n" +
"server.host=localhost\n" +
"server.port=8080\n" +
"server.reasons.200=OK Server\n" +
"server.reasons.201=Created Server\n" +
"cloud.host=cloud\n" +
"cloud.port=9090\n" +
"cloud.reasons.200=OK Cloud\n" +
"cloud.reasons.201=Created Cloud\n" +
"host=empty\n" +
"port=0\n"),
"port=0\n" +
"reasons.200=OK\n" +
"reasons.201=Created\n"),
"application.properties"));

@Inject
Expand Down Expand Up @@ -78,14 +86,23 @@ void configProperties() {
assertNotNull(configProperties);
assertEquals("localhost", configProperties.host);
assertEquals(8080, configProperties.port);
assertEquals(2, configProperties.reasons.size());
assertEquals("OK Server", configProperties.reasons.get(200));
assertEquals("Created Server", configProperties.reasons.get(201));

assertNotNull(configPropertiesCloud);
assertEquals("cloud", configPropertiesCloud.host);
assertEquals(9090, configPropertiesCloud.port);
assertEquals(2, configPropertiesCloud.reasons.size());
assertEquals("OK Cloud", configPropertiesCloud.reasons.get(200));
assertEquals("Created Cloud", configPropertiesCloud.reasons.get(201));

assertNotNull(configPropertiesEmpty);
assertEquals("empty", configPropertiesEmpty.host);
assertEquals(0, configPropertiesEmpty.port);
assertEquals(2, configPropertiesEmpty.reasons.size());
assertEquals("OK", configPropertiesEmpty.reasons.get(200));
assertEquals("Created", configPropertiesEmpty.reasons.get(201));
}

@Test
Expand Down Expand Up @@ -142,6 +159,7 @@ public interface Client {
public static class ServerConfigProperties {
public String host;
public int port;
public Map<Integer, String> reasons;
}

@Dependent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package io.quarkus.arc.test.config;

import static io.smallrye.common.constraint.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.config.spi.Converter;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class ConfigPropertyMapInjectionTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addAsServiceProvider(Converter.class, VersionConverter.class)
.addAsResource(new StringAsset(
"root.numbers.1=one\n" +
"root.numbers.2=two\n" +
"root.numbers.3=three\n" +
"versions.v1=1.The version 1.2.3\n" +
"versions.v1.2=1.The version 1.2.0\n" +
"versions.v2=2.The version 2.0.0\n"),
"application.properties"));

@ConfigProperty(name = "root.numbers")
Map<Integer, String> numbers;

@ConfigProperty(name = "root.numbers")
Optional<Map<Integer, String>> oNumbers;

@ConfigProperty(name = "root.numbers")
Supplier<Map<Integer, String>> sNumbers;

@ConfigProperty(name = "versions")
Map<String, Version> versions;

@ConfigProperty(name = "default.versions", defaultValue = "v0.1=0.The version 0;v1\\=1\\;2\\;3=1.The version 1\\;2\\;3;v2\\=2\\;1\\;0=2.The version 2\\;1\\;0")
Map<String, Version> versionsDefault;

@Test
void mapInjection() {
assertNotNull(numbers);
assertEquals(3, numbers.size());
assertEquals("one", numbers.get(1));
assertEquals("two", numbers.get(2));
assertEquals("three", numbers.get(3));

assertNotNull(numbers);
assertEquals(3, numbers.size());
assertEquals("one", numbers.get(1));
assertEquals("two", numbers.get(2));
assertEquals("three", numbers.get(3));

assertNotNull(oNumbers);
assertTrue(oNumbers.isPresent());
assertEquals(3, oNumbers.get().size());
assertEquals("one", oNumbers.get().get(1));
assertEquals("two", oNumbers.get().get(2));
assertEquals("three", oNumbers.get().get(3));

assertNotNull(sNumbers);
assertEquals(3, sNumbers.get().size());
assertEquals("one", sNumbers.get().get(1));
assertEquals("two", sNumbers.get().get(2));
assertEquals("three", sNumbers.get().get(3));

assertEquals(2, versions.size());
assertEquals(new Version(1, "The version 1.2.3"), versions.get("v1"));
assertEquals(new Version(2, "The version 2.0.0"), versions.get("v2"));
assertEquals(2, versionsDefault.size());
assertEquals(new Version(1, "The version 1;2;3"), versionsDefault.get("v1=1;2;3"));
assertEquals(new Version(2, "The version 2;1;0"), versionsDefault.get("v2=2;1;0"));
}

static class Version {
int id;
String name;

Version(int id, String name) {
this.id = id;
this.name = name;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Version version = (Version) o;
return id == version.id && Objects.equals(name, version.name);
}

@Override
public int hashCode() {
return Objects.hash(id, name);
}
}

public static class VersionConverter implements Converter<Version> {

@Override
public Version convert(String value) {
return new Version(Integer.parseInt(value.substring(0, 1)), value.substring(2));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.arc.runtime;

import java.util.Map;
import java.util.Objects;
import java.util.Set;

Expand Down Expand Up @@ -31,7 +32,7 @@ public void validateConfigProperties(Set<ConfigValidationMetadata> properties) {
for (ConfigValidationMetadata property : properties) {
Class<?> propertyClass = load(property.getType(), cl);
// 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) {
if (propertyClass.isArray() || (propertyClass.getTypeParameters().length > 0 && propertyClass != Map.class)) {
propertyClass = String.class;
}

Expand Down

0 comments on commit 73b7594

Please sign in to comment.