Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#10797 Re-add Try-Catch for ConfigurationExceptions To DefaultPropertyPlaceholderResolver #10798

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,18 @@ public <T> T getValue(Class<T> type) throws ConfigurationException {

@Override
public <T> Optional<T> findValue(Class<T> type) {
for (String expression: expressions) {
Optional<T> optionalValue = resolveOptionalExpression(expression, type);
if (optionalValue.isPresent()) {
return optionalValue;
try {
for (String expression: expressions) {
Optional<T> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public interface Engine {
int getCylinders();

String start();

String getDescription();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

// tag::imports[]
import io.micronaut.context.annotation.Value;
import io.micronaut.core.annotation.Nullable;

import jakarta.inject.Singleton;
// end::imports[]
Expand All @@ -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;
Expand All @@ -38,5 +43,10 @@ public String start() {// <2>
return "Starting V" + getCylinders() + " Engine";
}

@Override
public String getDescription() {
return description;
}

}
// end::class[]
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -56,4 +57,34 @@ void testStartVehicleWithoutConfiguration() {
assertEquals("Starting V6 Engine", vehicle.start());
}

@Test
void testStartVehicleWithNonEmptyPlaceholder() {
// tag::start[]
ApplicationContext applicationContext = new DefaultApplicationContext("test");
LinkedHashMap<String, Object> 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<String, Object> 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());
}
}
Loading