Skip to content

Commit e41cb3f

Browse files
committed
#1468 - Mapping discoverer now keeps trailing slashes around.
1 parent 5aebab4 commit e41cb3f

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

Lines changed: 6 additions & 4 deletions
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
/**
@@ -42,8 +42,6 @@
4242
@Deprecated
4343
public class AnnotationMappingDiscoverer implements MappingDiscoverer {
4444

45-
private static final Pattern MULTIPLE_SLASHES = Pattern.compile("/{2,}");
46-
4745
private final Class<? extends Annotation> annotationType;
4846
private final String mappingAttributeName;
4947

@@ -195,14 +193,18 @@ private static String cleanup(String mapping) {
195193

196194
String part = parts[i];
197195

196+
if (!StringUtils.hasText(part)) {
197+
continue;
198+
}
199+
198200
if (i != 0) {
199201
result.append("/");
200202
}
201203

202204
result.append(part.contains(":") ? cleanupPart(part) : part);
203205
}
204206

205-
return MULTIPLE_SLASHES.matcher(result.toString()).replaceAll("/");
207+
return (mapping.endsWith("/") ? result.append("/") : result).toString();
206208
}
207209

208210
private static String cleanupPart(String part) {

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

Lines changed: 16 additions & 0 deletions
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 // #1469
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+
// #1469
266+
267+
interface TrailingSlashes {
268+
269+
@RequestMapping("/api/myentities/")
270+
Object trailingSlash();
271+
}
256272
}

0 commit comments

Comments
 (0)