Skip to content

Commit c306e03

Browse files
committed
Support '-' in endpoint names
Update the `EndpointId` constraints to allow '-' in names. Closes gh-14773
1 parent d8b9685 commit c306e03

File tree

2 files changed

+41
-14
lines changed
  • spring-boot-project/spring-boot-actuator/src

2 files changed

+41
-14
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,44 @@
2323

2424
/**
2525
* An identifier for an actuator endpoint. Endpoint IDs may contain only letters, numbers
26-
* and {@code '.'}. They must begin with a lower-case letter. Case is ignored when
27-
* comparing endpoint IDs.
26+
* {@code '.'} and {@code '-'}. They must begin with a lower-case letter. Case and syntax
27+
* characters are ignored when comparing endpoint IDs.
2828
*
2929
* @author Phillip Webb
3030
* @since 2.0.6
3131
*/
3232
public final class EndpointId {
3333

34-
private static final Pattern VALID_CHARS = Pattern.compile("[a-zA-Z0-9\\.]+");
34+
private static final Pattern VALID_CHARS = Pattern.compile("[a-zA-Z0-9\\.\\-]+");
3535

3636
private final String value;
3737

3838
private final String lowerCaseValue;
3939

40+
private final String lowerCaseAlphaNumeric;
41+
4042
private EndpointId(String value) {
4143
Assert.hasText(value, "Value must not be empty");
4244
Assert.isTrue(VALID_CHARS.matcher(value).matches(),
43-
"Value must be alpha-numeric or '.'");
45+
"Value must only contain valid chars");
4446
Assert.isTrue(!Character.isDigit(value.charAt(0)),
4547
"Value must not start with a number");
4648
Assert.isTrue(!Character.isUpperCase(value.charAt(0)),
4749
"Value must not start with an uppercase letter");
4850
this.value = value;
4951
this.lowerCaseValue = value.toLowerCase(Locale.ENGLISH);
52+
this.lowerCaseAlphaNumeric = getAlphaNumerics(this.lowerCaseValue);
53+
}
54+
55+
private String getAlphaNumerics(String value) {
56+
StringBuilder result = new StringBuilder(value.length());
57+
for (int i = 0; i < value.length(); i++) {
58+
char ch = value.charAt(i);
59+
if (ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9') {
60+
result.append(ch);
61+
}
62+
}
63+
return result.toString();
5064
}
5165

5266
@Override
@@ -57,12 +71,13 @@ public boolean equals(Object obj) {
5771
if (obj == null || getClass() != obj.getClass()) {
5872
return false;
5973
}
60-
return toLowerCaseString().equals(((EndpointId) obj).toLowerCaseString());
74+
return this.lowerCaseAlphaNumeric
75+
.equals(((EndpointId) obj).lowerCaseAlphaNumeric);
6176
}
6277

6378
@Override
6479
public int hashCode() {
65-
return toLowerCaseString().hashCode();
80+
return this.lowerCaseAlphaNumeric.hashCode();
6681
}
6782

6883
/**

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ public void ofWhenEmptyThrowsException() {
4747
}
4848

4949
@Test
50-
public void ofWhenContainsDashThrowsException() {
50+
public void ofWhenContainsInvalidCharThrowsException() {
5151
this.thrown.expect(IllegalArgumentException.class);
52-
this.thrown.expectMessage("Value must be alpha-numeric");
53-
EndpointId.of("foo-bar");
52+
this.thrown.expectMessage("Value must only contain valid chars");
53+
EndpointId.of("foo/bar");
5454
}
5555

5656
@Test
5757
public void ofWhenHasBadCharThrowsException() {
5858
this.thrown.expect(IllegalArgumentException.class);
59-
this.thrown.expectMessage("Value must be alpha-numeric");
59+
this.thrown.expectMessage("Value must only contain valid chars");
6060
EndpointId.of("foo!bar");
6161
}
6262

@@ -82,13 +82,25 @@ public void ofWhenContainsDotIsValid() {
8282
assertThat(endpointId.toString()).isEqualTo("foo.bar");
8383
}
8484

85+
@Test
86+
public void ofWhenContainsDashIsValid() {
87+
// Ideally we wouldn't support this but there are existing endpoints using the
88+
// pattern. See gh-14773
89+
EndpointId endpointId = EndpointId.of("foo-bar");
90+
assertThat(endpointId.toString()).isEqualTo("foo-bar");
91+
}
92+
8593
@Test
8694
public void equalsAndHashCode() {
87-
EndpointId one = EndpointId.of("foobar");
88-
EndpointId two = EndpointId.of("fooBar");
89-
EndpointId three = EndpointId.of("barfoo");
95+
EndpointId one = EndpointId.of("foobar1");
96+
EndpointId two = EndpointId.of("fooBar1");
97+
EndpointId three = EndpointId.of("foo-bar1");
98+
EndpointId four = EndpointId.of("foo.bar1");
99+
EndpointId five = EndpointId.of("barfoo1");
100+
EndpointId six = EndpointId.of("foobar2");
90101
assertThat(one.hashCode()).isEqualTo(two.hashCode());
91-
assertThat(one).isEqualTo(one).isEqualTo(two).isNotEqualTo(three);
102+
assertThat(one).isEqualTo(one).isEqualTo(two).isEqualTo(two).isEqualTo(three)
103+
.isEqualTo(four).isNotEqualTo(five).isNotEqualTo(six);
92104
}
93105

94106
@Test

0 commit comments

Comments
 (0)