From 0c0056884a6c98ae54920e8b7901c1148609fe66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Barto=C5=A1?= Date: Tue, 14 Mar 2023 17:30:29 +0100 Subject: [PATCH] NPE when obtaining ConfigValue via getValue Fixes #906 --- .../ExpressionConfigSourceInterceptor.java | 2 +- ...ExpressionConfigSourceInterceptorTest.java | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/implementation/src/main/java/io/smallrye/config/ExpressionConfigSourceInterceptor.java b/implementation/src/main/java/io/smallrye/config/ExpressionConfigSourceInterceptor.java index 740799f9f..09b73ecef 100644 --- a/implementation/src/main/java/io/smallrye/config/ExpressionConfigSourceInterceptor.java +++ b/implementation/src/main/java/io/smallrye/config/ExpressionConfigSourceInterceptor.java @@ -49,7 +49,7 @@ private ConfigValue getValue(final ConfigSourceInterceptorContext context, final return configValue; } - if (configValue == null) { + if (configValue == null || configValue.getValue() == null) { return null; } diff --git a/implementation/src/test/java/io/smallrye/config/ExpressionConfigSourceInterceptorTest.java b/implementation/src/test/java/io/smallrye/config/ExpressionConfigSourceInterceptorTest.java index 7abe50a76..6ca5abf49 100644 --- a/implementation/src/test/java/io/smallrye/config/ExpressionConfigSourceInterceptorTest.java +++ b/implementation/src/test/java/io/smallrye/config/ExpressionConfigSourceInterceptorTest.java @@ -12,6 +12,8 @@ import java.util.Optional; import java.util.stream.Stream; +import jakarta.annotation.Priority; + import org.junit.jupiter.api.Test; class ExpressionConfigSourceInterceptorTest { @@ -203,10 +205,39 @@ void windowPath() { assertEquals("C:\\Some\\Path", config.getRawValue("window.path")); } + @Test + void nullValue() { + SmallRyeConfig config = buildConfigWithCustomInterceptor("sth", null); + ConfigValue configValue = config.getConfigValue("sth"); + + assertNotNull(configValue); + + // No exception is thrown, only null is returned + assertNull(configValue.getValue()); + } + private static SmallRyeConfig buildConfig(String... keyValues) { return new SmallRyeConfigBuilder() .addDefaultInterceptors() .withSources(KeyValuesConfigSource.config(keyValues)) .build(); } + + private static SmallRyeConfig buildConfigWithCustomInterceptor(String... keyValues) { + return new SmallRyeConfigBuilder() + .addDefaultInterceptors() + .withInterceptors(new CustomConfigSourceInterceptor()) + .withInterceptors(new ExpressionConfigSourceInterceptor()) + .withSources(KeyValuesConfigSource.config(keyValues)) + .build(); + } + + @Priority(Priorities.LIBRARY + 201) + private static class CustomConfigSourceInterceptor implements ConfigSourceInterceptor { + + @Override + public ConfigValue getValue(ConfigSourceInterceptorContext context, String name) { + return ConfigValue.builder().withName(name).withValue(null).build(); + } + } }