Skip to content

Commit 138d854

Browse files
committed
Support mixed case endpoint IDs with time-to-live
Update the endpoint time-to-live binding logic so that mixed case endpoint IDs are supported. Prior to this commit an `InvalidConfigurationPropertyNameException` would be thrown when using a camel case endpoint ID. See gh-14773
1 parent 3105a38 commit 138d854

File tree

8 files changed

+37
-21
lines changed

8 files changed

+37
-21
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/EndpointIdTimeToLivePropertyFunction.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.time.Duration;
2020
import java.util.function.Function;
2121

22+
import org.springframework.boot.actuate.endpoint.EndpointId;
2223
import org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvokerAdvisor;
2324
import org.springframework.boot.context.properties.bind.BindResult;
2425
import org.springframework.boot.context.properties.bind.Bindable;
@@ -33,7 +34,7 @@
3334
* @author Stephane Nicoll
3435
* @author Phillip Webb
3536
*/
36-
class EndpointIdTimeToLivePropertyFunction implements Function<String, Long> {
37+
class EndpointIdTimeToLivePropertyFunction implements Function<EndpointId, Long> {
3738

3839
private static final Bindable<Duration> DURATION = Bindable.of(Duration.class);
3940

@@ -48,9 +49,9 @@ class EndpointIdTimeToLivePropertyFunction implements Function<String, Long> {
4849
}
4950

5051
@Override
51-
public Long apply(String endpointId) {
52+
public Long apply(EndpointId endpointId) {
5253
String name = String.format("management.endpoint.%s.cache.time-to-live",
53-
endpointId);
54+
endpointId.toLowerCaseString());
5455
BindResult<Duration> duration = Binder.get(this.environment).bind(name, DURATION);
5556
return duration.map(Duration::toMillis).orElse(null);
5657
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/CloudFoundryWebEndpointDiscovererTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ private void load(Class<?> configuration,
7373
this.load((id) -> null, (id) -> id, configuration, consumer);
7474
}
7575

76-
private void load(Function<String, Long> timeToLive, PathMapper endpointPathMapper,
77-
Class<?> configuration,
76+
private void load(Function<EndpointId, Long> timeToLive,
77+
PathMapper endpointPathMapper, Class<?> configuration,
7878
Consumer<CloudFoundryWebEndpointDiscoverer> consumer) {
7979
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
8080
configuration);

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/EndpointIdTimeToLivePropertyFunctionTests.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020

2121
import org.junit.Test;
2222

23+
import org.springframework.boot.actuate.endpoint.EndpointId;
2324
import org.springframework.mock.env.MockEnvironment;
2425

2526
import static org.assertj.core.api.Assertions.assertThat;
@@ -34,21 +35,26 @@ public class EndpointIdTimeToLivePropertyFunctionTests {
3435

3536
private final MockEnvironment environment = new MockEnvironment();
3637

37-
private final Function<String, Long> timeToLive = new EndpointIdTimeToLivePropertyFunction(
38+
private final Function<EndpointId, Long> timeToLive = new EndpointIdTimeToLivePropertyFunction(
3839
this.environment);
3940

4041
@Test
4142
public void defaultConfiguration() {
42-
Long result = this.timeToLive.apply("test");
43+
Long result = this.timeToLive.apply(EndpointId.of("test"));
4344
assertThat(result).isNull();
4445
}
4546

4647
@Test
4748
public void userConfiguration() {
48-
this.environment.setProperty("management.endpoint.test.cache.time-to-live",
49-
"500");
50-
Long result = this.timeToLive.apply("test");
49+
this.environment.setProperty(
50+
"management.endpoint.another-test.cache.time-to-live", "500");
51+
Long result = this.timeToLive.apply(EndpointId.of("anotherTest"));
5152
assertThat(result).isEqualTo(500L);
5253
}
5354

55+
@Test
56+
public void mixedCaseUserConfiguration() {
57+
58+
}
59+
5460
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/invoker/cache/CachingOperationInvokerAdvisor.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.function.Function;
2020

21+
import org.springframework.boot.actuate.endpoint.EndpointId;
2122
import org.springframework.boot.actuate.endpoint.OperationType;
2223
import org.springframework.boot.actuate.endpoint.SecurityContext;
2324
import org.springframework.boot.actuate.endpoint.invoke.OperationInvoker;
@@ -33,16 +34,23 @@
3334
*/
3435
public class CachingOperationInvokerAdvisor implements OperationInvokerAdvisor {
3536

36-
private final Function<String, Long> endpointIdTimeToLive;
37+
private final Function<EndpointId, Long> endpointIdTimeToLive;
3738

38-
public CachingOperationInvokerAdvisor(Function<String, Long> endpointIdTimeToLive) {
39+
public CachingOperationInvokerAdvisor(
40+
Function<EndpointId, Long> endpointIdTimeToLive) {
3941
this.endpointIdTimeToLive = endpointIdTimeToLive;
4042
}
4143

4244
@Override
4345
@Deprecated
4446
public OperationInvoker apply(String endpointId, OperationType operationType,
4547
OperationParameters parameters, OperationInvoker invoker) {
48+
return apply(EndpointId.of(endpointId), operationType, parameters, invoker);
49+
}
50+
51+
@Override
52+
public OperationInvoker apply(EndpointId endpointId, OperationType operationType,
53+
OperationParameters parameters, OperationInvoker invoker) {
4654
if (operationType == OperationType.READ && !hasMandatoryParameter(parameters)) {
4755
Long timeToLive = this.endpointIdTimeToLive.apply(endpointId);
4856
if (timeToLive != null && timeToLive > 0) {

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscovererTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,12 @@ static class TestEndpointDiscoverer
500500
}
501501

502502
TestEndpointDiscoverer(ApplicationContext applicationContext,
503-
Function<String, Long> timeToLive) {
503+
Function<EndpointId, Long> timeToLive) {
504504
this(applicationContext, timeToLive, Collections.emptyList());
505505
}
506506

507507
TestEndpointDiscoverer(ApplicationContext applicationContext,
508-
Function<String, Long> timeToLive,
508+
Function<EndpointId, Long> timeToLive,
509509
Collection<EndpointFilter<TestExposableEndpoint>> filters) {
510510
this(applicationContext, new ConversionServiceParameterValueMapper(),
511511
Collections.singleton(new CachingOperationInvokerAdvisor(timeToLive)),

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/invoker/cache/CachingOperationInvokerAdvisorTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class CachingOperationInvokerAdvisorTests {
5151
private OperationInvoker invoker;
5252

5353
@Mock
54-
private Function<String, Long> timeToLive;
54+
private Function<EndpointId, Long> timeToLive;
5555

5656
private CachingOperationInvokerAdvisor advisor;
5757

@@ -85,7 +85,7 @@ public void applyWhenTimeToLiveReturnsNullShouldNotAddAdvise() {
8585
OperationInvoker advised = this.advisor.apply(EndpointId.of("foo"),
8686
OperationType.READ, parameters, this.invoker);
8787
assertThat(advised).isSameAs(this.invoker);
88-
verify(this.timeToLive).apply("foo");
88+
verify(this.timeToLive).apply(EndpointId.of("foo"));
8989
}
9090

9191
@Test
@@ -95,7 +95,7 @@ public void applyWhenTimeToLiveIsZeroShouldNotAddAdvise() {
9595
OperationInvoker advised = this.advisor.apply(EndpointId.of("foo"),
9696
OperationType.READ, parameters, this.invoker);
9797
assertThat(advised).isSameAs(this.invoker);
98-
verify(this.timeToLive).apply("foo");
98+
verify(this.timeToLive).apply(EndpointId.of("foo"));
9999
}
100100

101101
@Test

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/jmx/annotation/JmxEndpointDiscovererTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private void load(Class<?> configuration, Consumer<JmxEndpointDiscoverer> consum
306306
load(configuration, (id) -> null, consumer);
307307
}
308308

309-
private void load(Class<?> configuration, Function<String, Long> timeToLive,
309+
private void load(Class<?> configuration, Function<EndpointId, Long> timeToLive,
310310
Consumer<JmxEndpointDiscoverer> consumer) {
311311
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
312312
configuration)) {

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/annotation/WebEndpointDiscovererTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,9 @@ private void load(Class<?> configuration, Consumer<WebEndpointDiscoverer> consum
257257
this.load((id) -> null, (id) -> id, configuration, consumer);
258258
}
259259

260-
private void load(Function<String, Long> timeToLive, PathMapper endpointPathMapper,
261-
Class<?> configuration, Consumer<WebEndpointDiscoverer> consumer) {
260+
private void load(Function<EndpointId, Long> timeToLive,
261+
PathMapper endpointPathMapper, Class<?> configuration,
262+
Consumer<WebEndpointDiscoverer> consumer) {
262263
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
263264
configuration);
264265
try {

0 commit comments

Comments
 (0)