Skip to content

Commit

Permalink
Fixes #240
Browse files Browse the repository at this point in the history
  • Loading branch information
fmaffioletti committed Nov 20, 2017
1 parent d6a5262 commit 8dff425
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ public class SpringPathBuilder {
* the method level! When used at the type level, all method-level mappings
* inherit this primary mapping, narrowing it for a specific handler method.
*
* @param apiMethodDoc
* @param method
* @param controller
* @return
*/
public static Set<String> buildPath(Method method) {
Expand Down Expand Up @@ -52,7 +50,13 @@ public static Set<String> buildPath(Method method) {

for (String controllerPath : controllerMapping) {
for (String methodPath : methodMapping) {
paths.add(controllerPath + methodPath);
String resolvedPath;
if(needsToJoinWithSlash(controllerPath, methodPath)) {
resolvedPath = controllerPath + "/" + methodPath;
} else {
resolvedPath = controllerPath + methodPath;
}
paths.add(resolvedPath);
}
}

Expand All @@ -66,6 +70,10 @@ public static Set<String> buildPath(Method method) {
return paths;
}

private static boolean needsToJoinWithSlash(String controllerPath, String methodPath) {
return (!controllerPath.isEmpty() && !controllerPath.endsWith("/")) && (!methodPath.isEmpty() && !methodPath.startsWith("/"));
}

//Handle the fact that this method is only in Spring 4, not available in Spring 3
private static String[] pathMapping(RequestMapping requestMapping) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Sets;
import org.springframework.web.bind.annotation.RequestMethod;

public class SpringPathBuilderTest {

Expand Down Expand Up @@ -85,6 +86,17 @@ public void none() {

}

@Controller
@RequestMapping(path = {"/api/widget"})
public class SpringController6 {

@RequestMapping(method = RequestMethod.GET, path = "frame")
public void getItem() {

}

}

@Test
public void testPath() {
ApiDoc apiDoc = jsondocScanner.getApiDocs(Sets.<Class<?>> newHashSet(SpringController.class), MethodDisplay.URI).iterator().next();
Expand Down Expand Up @@ -182,6 +194,20 @@ public boolean apply(ApiMethodDoc input) {
});
Assert.assertTrue(allRight);
}

@Test
public void testPath6() {
ApiDoc apiDoc = jsondocScanner.getApiDocs(Sets.<Class<?>> newHashSet(SpringController6.class), MethodDisplay.URI).iterator().next();
Assert.assertEquals("SpringController6", apiDoc.getName());

boolean allRight = FluentIterable.from(apiDoc.getMethods()).anyMatch(new Predicate<ApiMethodDoc>() {
@Override
public boolean apply(ApiMethodDoc input) {
return input.getPath().contains("/api/widget/frame");
}
});
Assert.assertTrue(allRight);
}

@Test
public void testPathWithMethodDisplayMethod() {
Expand Down

0 comments on commit 8dff425

Please sign in to comment.