From a6fd8c366f3a23aefed9bb0c5653a315cf205714 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Wed, 8 May 2024 08:07:55 -0500 Subject: [PATCH] fix: Re-add Try-Catch for ConfigurationExceptions To DefaultPropertyPlaceholderResolver (#10798) Fixes #10797 * Adding try-catch for ConfigurationExceptions which are thrown when resolving env properties * Adding test cases for placeholder resolution, both with and without the placeholder set * Removing irrelevant print statement and method call to vehicle for the purposes of the specified unit tests --- .../DefaultPropertyPlaceholderResolver.java | 18 ++++++----- .../micronaut/docs/config/value/Engine.java | 2 ++ .../docs/config/value/EngineImpl.java | 10 ++++++ .../docs/config/value/VehicleSpec.java | 31 +++++++++++++++++++ 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/inject/src/main/java/io/micronaut/context/env/DefaultPropertyPlaceholderResolver.java b/inject/src/main/java/io/micronaut/context/env/DefaultPropertyPlaceholderResolver.java index d716c025813..b6425cc43b1 100644 --- a/inject/src/main/java/io/micronaut/context/env/DefaultPropertyPlaceholderResolver.java +++ b/inject/src/main/java/io/micronaut/context/env/DefaultPropertyPlaceholderResolver.java @@ -394,14 +394,18 @@ public T getValue(Class type) throws ConfigurationException { @Override public Optional findValue(Class type) { - for (String expression: expressions) { - Optional optionalValue = resolveOptionalExpression(expression, type); - if (optionalValue.isPresent()) { - return optionalValue; + try { + for (String expression: expressions) { + Optional optionalValue = resolveOptionalExpression(expression, type); + if (optionalValue.isPresent()) { + return optionalValue; + } } - } - if (defaultValue != null) { - return conversionService.convert(defaultValue, type); + if (defaultValue != null) { + return conversionService.convert(defaultValue, type); + } + } catch (ConfigurationException e) { + // Swallow exception. } return Optional.empty(); } diff --git a/test-suite/src/test/java/io/micronaut/docs/config/value/Engine.java b/test-suite/src/test/java/io/micronaut/docs/config/value/Engine.java index 5dd2d167c36..4776d738602 100644 --- a/test-suite/src/test/java/io/micronaut/docs/config/value/Engine.java +++ b/test-suite/src/test/java/io/micronaut/docs/config/value/Engine.java @@ -19,4 +19,6 @@ public interface Engine { int getCylinders(); String start(); + + String getDescription(); } diff --git a/test-suite/src/test/java/io/micronaut/docs/config/value/EngineImpl.java b/test-suite/src/test/java/io/micronaut/docs/config/value/EngineImpl.java index 2e64d2d8f3f..baff4a73eeb 100644 --- a/test-suite/src/test/java/io/micronaut/docs/config/value/EngineImpl.java +++ b/test-suite/src/test/java/io/micronaut/docs/config/value/EngineImpl.java @@ -17,6 +17,7 @@ // tag::imports[] import io.micronaut.context.annotation.Value; +import io.micronaut.core.annotation.Nullable; import jakarta.inject.Singleton; // end::imports[] @@ -28,6 +29,10 @@ public class EngineImpl implements Engine { @Value("${my.engine.cylinders:6}") // <1> protected int cylinders; + @Nullable + @Value("${my.engine.description}") + protected String description; + @Override public int getCylinders() { return cylinders; @@ -38,5 +43,10 @@ public String start() {// <2> return "Starting V" + getCylinders() + " Engine"; } + @Override + public String getDescription() { + return description; + } + } // end::class[] diff --git a/test-suite/src/test/java/io/micronaut/docs/config/value/VehicleSpec.java b/test-suite/src/test/java/io/micronaut/docs/config/value/VehicleSpec.java index 8d225a39827..4709869dea0 100644 --- a/test-suite/src/test/java/io/micronaut/docs/config/value/VehicleSpec.java +++ b/test-suite/src/test/java/io/micronaut/docs/config/value/VehicleSpec.java @@ -24,6 +24,7 @@ import java.util.LinkedHashMap; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; class VehicleSpec { @@ -56,4 +57,34 @@ void testStartVehicleWithoutConfiguration() { assertEquals("Starting V6 Engine", vehicle.start()); } + @Test + void testStartVehicleWithNonEmptyPlaceholder() { + // tag::start[] + ApplicationContext applicationContext = new DefaultApplicationContext("test"); + LinkedHashMap map = new LinkedHashMap(1); + map.put("my.engine.description", "${DESCRIPTION}"); + map.put("DESCRIPTION", "V8 Engine"); + applicationContext.getEnvironment().addPropertySource(PropertySource.of("test", map)); + applicationContext.start(); + + Vehicle vehicle = applicationContext.getBean(Vehicle.class); + // end::start[] + + assertEquals("V8 Engine", vehicle.getEngine().getDescription()); + } + + @Test + void testStartVehicleWithEmptyPlaceholder() { + // tag::start[] + ApplicationContext applicationContext = new DefaultApplicationContext("test"); + LinkedHashMap map = new LinkedHashMap(1); + map.put("my.engine.description", "${DESCRIPTION}"); + applicationContext.getEnvironment().addPropertySource(PropertySource.of("test", map)); + applicationContext.start(); + + Vehicle vehicle = applicationContext.getBean(Vehicle.class); + // end::start[] + + assertNull(vehicle.getEngine().getDescription()); + } }