Skip to content
Permalink
Browse files
FIX: don't list routers in api docs index that can't be loaded by api…
… declaration route (Fixes #130)
  • Loading branch information
xhanin committed Feb 3, 2015
1 parent e076409 commit 98f97f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
@@ -1,10 +1,8 @@
package restx.apidocs;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.common.base.CaseFormat;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.*;
import restx.*;
import restx.description.*;
@@ -54,13 +52,7 @@ public ApiDeclarationRoute(@Named(FrontObjectMapperFactory.WRITER_NAME) ObjectWr
@Override
protected Optional<?> doRoute(RestxRequest restxRequest, RestxRequestMatch match, Object body) throws IOException {
String routerName = match.getPathParam("router");
routerName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, routerName);

Optional<NamedComponent<RestxRouter>> router = Optional.absent();
ImmutableList<String> suffixes = ImmutableList.of("ResourceRouter", "", "Resource", "Router");
for (int i = 0; i < suffixes.size() && !router.isPresent(); i++) {
router = factory.queryByName(Name.of(RestxRouter.class, routerName + suffixes.get(i))).optional().findOne();
}
Optional<NamedComponent<RestxRouter>> router = getRouterByName(factory, routerName);

if (!router.isPresent()) {
return Optional.absent();
@@ -76,6 +68,17 @@ protected Optional<?> doRoute(RestxRequest restxRequest, RestxRequestMatch match
.build());
}

static Optional<NamedComponent<RestxRouter>> getRouterByName(Factory f, String routerName) {
routerName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, routerName);

Optional<NamedComponent<RestxRouter>> router = Optional.absent();
ImmutableList<String> suffixes = ImmutableList.of("ResourceRouter", "", "Resource", "Router");
for (int i = 0; i < suffixes.size() && !router.isPresent(); i++) {
router = f.queryByName(Name.of(RestxRouter.class, routerName + suffixes.get(i))).optional().findOne();
}
return router;
}

private List<ResourceDescription> buildApis(NamedComponent<RestxRouter> router) {
return fillRelatedOperations(router.getName().getName(), describeAllRoutes(router.getComponent()));
}
@@ -63,11 +63,15 @@ private List<ImmutableMap<String, String>> buildApis() {
List<ImmutableMap<String, String>> apis = Lists.newArrayList();
for (NamedComponent<RestxRouter> router : routers) {
String routerApiPath = getRouterApiPath(router.getName().getName());
apis.add(ImmutableMap.of(
"path", "/@/api-docs/" + routerApiPath,
"name", routerApiPath,
"group", router.getComponent().getGroupName(),
"description", ""));
if (ApiDeclarationRoute.getRouterByName(factory, routerApiPath).isPresent()) {
// we add the api only if we can find back the router from the name, otherwise it will trigger
// 404 errors in API-DOCS
apis.add(ImmutableMap.of(
"path", "/@/api-docs/" + routerApiPath,
"name", routerApiPath,
"group", router.getComponent().getGroupName(),
"description", ""));
}
}
return apis;
}

0 comments on commit 98f97f1

Please sign in to comment.