diff --git a/implementation/src/main/java/io/smallrye/config/ExpressionConfigSourceInterceptor.java b/implementation/src/main/java/io/smallrye/config/ExpressionConfigSourceInterceptor.java index 1d495f557..e6828c2ce 100644 --- a/implementation/src/main/java/io/smallrye/config/ExpressionConfigSourceInterceptor.java +++ b/implementation/src/main/java/io/smallrye/config/ExpressionConfigSourceInterceptor.java @@ -40,6 +40,8 @@ private ConfigValue getValue(final ConfigSourceInterceptorContext context, final stringBuilder.append(resolve.getValue()); } else if (resolveContext.hasDefault()) { resolveContext.expandDefault(); + } else { + throw ConfigMessages.msg.expandingElementNotFound(resolveContext.getKey(), configValue.getName()); } }); diff --git a/implementation/src/main/java/io/smallrye/config/ProfileConfigSourceInterceptor.java b/implementation/src/main/java/io/smallrye/config/ProfileConfigSourceInterceptor.java index 4d00fa7d2..920c238f8 100644 --- a/implementation/src/main/java/io/smallrye/config/ProfileConfigSourceInterceptor.java +++ b/implementation/src/main/java/io/smallrye/config/ProfileConfigSourceInterceptor.java @@ -3,6 +3,7 @@ import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; +import java.util.NoSuchElementException; import java.util.Optional; import java.util.Set; @@ -48,12 +49,15 @@ public ConfigValue getValue(final ConfigSourceInterceptorContext context, final final String normalizeName = normalizeName(name); final ConfigValue profileValue = context.proceed("%" + profile + "." + normalizeName); if (profileValue != null) { - final ConfigValue originalValue = context.proceed(normalizeName); - if (originalValue != null && CONFIG_SOURCE_COMPARATOR.compare(profileValue, originalValue) > 0) { - return originalValue; - } else { - return profileValue.withName(normalizeName); + try { + final ConfigValue originalValue = context.proceed(normalizeName); + if (originalValue != null && CONFIG_SOURCE_COMPARATOR.compare(profileValue, originalValue) > 0) { + return originalValue; + } + } catch (final NoSuchElementException e) { + // We couldn't find the main property so we fallback to the profile property because it exists. } + return profileValue.withName(normalizeName); } } diff --git a/implementation/src/test/java/io/smallrye/config/ExpressionConfigSourceInterceptorTest.java b/implementation/src/test/java/io/smallrye/config/ExpressionConfigSourceInterceptorTest.java index c06668465..6f1f06336 100644 --- a/implementation/src/test/java/io/smallrye/config/ExpressionConfigSourceInterceptorTest.java +++ b/implementation/src/test/java/io/smallrye/config/ExpressionConfigSourceInterceptorTest.java @@ -64,7 +64,7 @@ public void noExpression() { final NoSuchElementException exception = assertThrows(NoSuchElementException.class, () -> config.getValue("expression", String.class)); - assertEquals("SRCFG00014: Property expression not found", exception.getMessage()); + assertEquals("SRCFG00011: Could not expand value my.prop in property expression", exception.getMessage()); } @Test @@ -73,7 +73,7 @@ public void noExpressionComposed() { final NoSuchElementException exception = assertThrows(NoSuchElementException.class, () -> config.getValue("expression", String.class)); - assertEquals("SRCFG00014: Property expression not found", exception.getMessage()); + assertEquals("SRCFG00011: Could not expand value compose in property expression", exception.getMessage()); } @Test @@ -118,6 +118,14 @@ public void escape() { .getRawValue("camel.expression")); } + @Test + void expressionMissing() { + final SmallRyeConfig config = buildConfig("my.prop", "${expression}", "my.prop.partial", "${expression}partial"); + + assertThrows(Exception.class, () -> config.getValue("my.prop", String.class)); + assertThrows(Exception.class, () -> config.getValue("my.prop.partial", String.class)); + } + private static SmallRyeConfig buildConfig(String... keyValues) { return new SmallRyeConfigBuilder() .addDefaultSources() diff --git a/implementation/src/test/java/io/smallrye/config/ProfileConfigSourceInterceptorTest.java b/implementation/src/test/java/io/smallrye/config/ProfileConfigSourceInterceptorTest.java index 2b09dd587..1b810eec3 100644 --- a/implementation/src/test/java/io/smallrye/config/ProfileConfigSourceInterceptorTest.java +++ b/implementation/src/test/java/io/smallrye/config/ProfileConfigSourceInterceptorTest.java @@ -1,6 +1,7 @@ package io.smallrye.config; import static io.smallrye.config.ProfileConfigSourceInterceptor.SMALLRYE_PROFILE; +import static java.util.Collections.singletonMap; import static java.util.stream.Collectors.toList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -48,6 +49,19 @@ public void expressions() { assertEquals("1", config.getValue("my.prop", String.class)); } + @Test + public void expressionsOrdinals() { + final SmallRyeConfig config = new SmallRyeConfigBuilder() + .withSources(new PropertiesConfigSource(singletonMap("my.prop", "1"), "test", 100)) + .withSources(new PropertiesConfigSource(singletonMap("%prof.my.prop", "${my.prop}"), "test", 200)) + .withInterceptors( + new ProfileConfigSourceInterceptor("prof"), + new ExpressionConfigSourceInterceptor()) + .build(); + + assertEquals("1", config.getValue("my.prop", String.class)); + } + @Test public void profileExpressions() { final Config config = buildConfig("my.prop", "1",