Skip to content

Commit

Permalink
Partially revert #345, to keep exception in case expansion cannot be …
Browse files Browse the repository at this point in the history
…done, but only if required. For instance, Profile evaluation can fallback to the profile property.
  • Loading branch information
radcortez committed Jul 9, 2020
1 parent d58963a commit e5a5e07
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit e5a5e07

Please sign in to comment.