Skip to content

Commit 536e87a

Browse files
committed
Fix kebab and snake case formatting of all upper case words
Closes gh-658
1 parent baaf4f2 commit 536e87a

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolver.java

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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.
@@ -16,9 +16,6 @@
1616

1717
package org.springframework.restdocs.snippet;
1818

19-
import java.util.regex.Matcher;
20-
import java.util.regex.Pattern;
21-
2219
import org.springframework.restdocs.RestDocumentationContext;
2320
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
2421

@@ -51,8 +48,6 @@
5148
*/
5249
public class RestDocumentationContextPlaceholderResolver implements PlaceholderResolver {
5350

54-
private static final Pattern CAMEL_CASE_PATTERN = Pattern.compile("([A-Z])");
55-
5651
private final RestDocumentationContext context;
5752

5853
/**
@@ -130,14 +125,18 @@ protected final RestDocumentationContext getContext() {
130125
}
131126

132127
private String camelCaseToSeparator(String string, String separator) {
133-
Matcher matcher = CAMEL_CASE_PATTERN.matcher(string);
134128
StringBuffer result = new StringBuffer();
135-
while (matcher.find()) {
136-
String replacement = (matcher.start() > 0) ? separator + matcher.group(1).toLowerCase()
137-
: matcher.group(1).toLowerCase();
138-
matcher.appendReplacement(result, replacement);
129+
char[] chars = string.toCharArray();
130+
for (int i = 0; i < chars.length; i++) {
131+
char current = chars[i];
132+
if (Character.isUpperCase(current) && i > 0) {
133+
if (Character.isLowerCase(chars[i - 1])
134+
|| (i < chars.length - 1 && Character.isLowerCase(chars[i + 1]))) {
135+
result.append(separator);
136+
}
137+
}
138+
result.append(Character.toLowerCase(chars[i]));
139139
}
140-
matcher.appendTail(result);
141140
return result.toString();
142141
}
143142

spring-restdocs-core/src/test/java/org/springframework/restdocs/snippet/RestDocumentationContextPlaceholderResolverTests.java

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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.
@@ -38,12 +38,48 @@ public void kebabCaseMethodName() throws Exception {
3838
.isEqualTo("dash-separated-method-name");
3939
}
4040

41+
@Test
42+
public void kebabCaseMethodNameWithUpperCaseOpeningSection() throws Exception {
43+
assertThat(createResolver("URIDashSeparatedMethodName").resolvePlaceholder("method-name"))
44+
.isEqualTo("uri-dash-separated-method-name");
45+
}
46+
47+
@Test
48+
public void kebabCaseMethodNameWithUpperCaseMidSection() throws Exception {
49+
assertThat(createResolver("dashSeparatedMethodNameWithURIInIt").resolvePlaceholder("method-name"))
50+
.isEqualTo("dash-separated-method-name-with-uri-in-it");
51+
}
52+
53+
@Test
54+
public void kebabCaseMethodNameWithUpperCaseEndSection() throws Exception {
55+
assertThat(createResolver("dashSeparatedMethodNameWithURI").resolvePlaceholder("method-name"))
56+
.isEqualTo("dash-separated-method-name-with-uri");
57+
}
58+
4159
@Test
4260
public void snakeCaseMethodName() throws Exception {
4361
assertThat(createResolver("underscoreSeparatedMethodName").resolvePlaceholder("method_name"))
4462
.isEqualTo("underscore_separated_method_name");
4563
}
4664

65+
@Test
66+
public void snakeCaseMethodNameWithUpperCaseOpeningSection() throws Exception {
67+
assertThat(createResolver("URIUnderscoreSeparatedMethodName").resolvePlaceholder("method_name"))
68+
.isEqualTo("uri_underscore_separated_method_name");
69+
}
70+
71+
@Test
72+
public void snakeCaseMethodNameWithUpperCaseMidSection() throws Exception {
73+
assertThat(createResolver("underscoreSeparatedMethodNameWithURIInIt").resolvePlaceholder("method_name"))
74+
.isEqualTo("underscore_separated_method_name_with_uri_in_it");
75+
}
76+
77+
@Test
78+
public void snakeCaseMethodNameWithUpperCaseEndSection() throws Exception {
79+
assertThat(createResolver("underscoreSeparatedMethodNameWithURI").resolvePlaceholder("method_name"))
80+
.isEqualTo("underscore_separated_method_name_with_uri");
81+
}
82+
4783
@Test
4884
public void camelCaseMethodName() throws Exception {
4985
assertThat(createResolver("camelCaseMethodName").resolvePlaceholder("methodName"))

0 commit comments

Comments
 (0)