Skip to content

Commit 0f9f10f

Browse files
Larishosnicoll
authored andcommitted
Migrate legacy endpoint that have a dash in their id
See gh-21615
1 parent 8962d6c commit 0f9f10f

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public static EndpointId of(Environment environment, String value) {
131131

132132
private static String migrateLegacyId(Environment environment, String value) {
133133
if (environment.getProperty(MIGRATE_LEGACY_NAMES_PROPERTY, Boolean.class, false)) {
134-
return value.replace(".", "");
134+
return value.replaceAll("[-.]+", "");
135135
}
136136
return value;
137137
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ void ofWhenMigratingLegacyNameRemovesDots(CapturedOutput output) {
109109
assertThat(output).doesNotContain("contains invalid characters");
110110
}
111111

112+
@Test
113+
void ofWhenMigratingLegacyNameRemovesHyphens(CapturedOutput output) {
114+
EndpointId.resetLoggedWarnings();
115+
MockEnvironment environment = new MockEnvironment();
116+
environment.setProperty("management.endpoints.migrate-legacy-ids", "true");
117+
EndpointId endpointId = EndpointId.of(environment, "foo-bar");
118+
assertThat(endpointId.toString()).isEqualTo("foobar");
119+
assertThat(output).doesNotContain("contains invalid characters");
120+
}
121+
112122
@Test
113123
void equalsAndHashCode() {
114124
EndpointId one = EndpointId.of("foobar1");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package smoketest.actuator;
18+
19+
import java.util.Collections;
20+
import java.util.Map;
21+
22+
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
23+
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
24+
import org.springframework.stereotype.Component;
25+
26+
@Component
27+
@Endpoint(id = "another-legacy")
28+
public class SampleLegacyEndpointWithHyphen {
29+
30+
@ReadOperation
31+
public Map<String, String> example() {
32+
return Collections.singletonMap("legacy", "legacy");
33+
}
34+
35+
}

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/SampleActuatorApplicationTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,21 @@ void testConfigProps() {
173173
}
174174

175175
@Test
176-
void testLegacy() {
176+
void testLegacyDot() {
177177
ResponseEntity<Map<String, Object>> entity = asMapEntity(
178178
this.restTemplate.withBasicAuth("user", "password").getForEntity("/actuator/legacy", Map.class));
179179
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
180180
assertThat(entity.getBody()).contains(entry("legacy", "legacy"));
181181
}
182182

183+
@Test
184+
void testLegacyHyphen() {
185+
ResponseEntity<Map<String, Object>> entity = asMapEntity(
186+
this.restTemplate.withBasicAuth("user", "password").getForEntity("/actuator/anotherlegacy", Map.class));
187+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
188+
assertThat(entity.getBody()).contains(entry("legacy", "legacy"));
189+
}
190+
183191
@SuppressWarnings({ "unchecked", "rawtypes" })
184192
static <K, V> ResponseEntity<Map<K, V>> asMapEntity(ResponseEntity<Map> entity) {
185193
return (ResponseEntity) entity;

0 commit comments

Comments
 (0)