Skip to content

Commit c141896

Browse files
committed
#1470 - Mapping discoverer now keeps trailing slashes around.
1 parent 15e8a65 commit c141896

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/main/java/org/springframework/hateoas/server/core/AnnotationMappingDiscoverer.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
import java.util.Collection;
2525
import java.util.Collections;
2626
import java.util.List;
27-
import java.util.regex.Pattern;
2827

2928
import org.springframework.http.HttpMethod;
3029
import org.springframework.lang.Nullable;
3130
import org.springframework.util.Assert;
31+
import org.springframework.util.StringUtils;
3232
import org.springframework.web.bind.annotation.RequestMethod;
3333

3434
/**
@@ -40,8 +40,6 @@
4040
*/
4141
public class AnnotationMappingDiscoverer implements MappingDiscoverer {
4242

43-
private static final Pattern MULTIPLE_SLASHES = Pattern.compile("/{2,}");
44-
4543
private final Class<? extends Annotation> annotationType;
4644
private final String mappingAttributeName;
4745

@@ -193,14 +191,18 @@ private static String cleanup(String mapping) {
193191

194192
String part = parts[i];
195193

194+
if (!StringUtils.hasText(part)) {
195+
continue;
196+
}
197+
196198
if (i != 0) {
197199
result.append("/");
198200
}
199201

200202
result.append(part.contains(":") ? cleanupPart(part) : part);
201203
}
202204

203-
return MULTIPLE_SLASHES.matcher(result.toString()).replaceAll("/");
205+
return (mapping.endsWith("/") ? result.append("/") : result).toString();
204206
}
205207

206208
private static String cleanupPart(String part) {

src/test/java/org/springframework/hateoas/server/core/AnnotationMappingDiscovererUnitTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ void extractsMultipleRegularExpressionVariablesCorrectly() throws Exception {
168168
assertThat(discoverer.getMapping(method)).isEqualTo("/type/spring-web/{symbolicName}-{version}{extension}");
169169
}
170170

171+
@Test // #1470
172+
void keepsTrailingSlash() throws Exception {
173+
174+
Method method = TrailingSlashes.class.getMethod("trailingSlash");
175+
176+
assertThat(discoverer.getMapping(method)).isEqualTo("/api/myentities/");
177+
}
178+
171179
@RequestMapping("/type")
172180
interface MyController {
173181

@@ -253,4 +261,12 @@ interface MultipleMappingsController {
253261
@RequestMapping({ "/method", "/methodAlias" })
254262
void method();
255263
}
264+
265+
// #1470
266+
267+
interface TrailingSlashes {
268+
269+
@RequestMapping("/api/myentities/")
270+
Object trailingSlash();
271+
}
256272
}

0 commit comments

Comments
 (0)