Skip to content

Commit

Permalink
FIX: don't list routers in api docs index that can't be loaded by api…
Browse files Browse the repository at this point in the history
… 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.
21 changes: 12 additions & 9 deletions restx-apidocs/src/main/java/restx/apidocs/ApiDeclarationRoute.java
@@ -1,10 +1,8 @@
package restx.apidocs; package restx.apidocs;


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


if (!router.isPresent()) { if (!router.isPresent()) {
return Optional.absent(); return Optional.absent();
Expand All @@ -76,6 +68,17 @@ protected Optional<?> doRoute(RestxRequest restxRequest, RestxRequestMatch match
.build()); .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) { private List<ResourceDescription> buildApis(NamedComponent<RestxRouter> router) {
return fillRelatedOperations(router.getName().getName(), describeAllRoutes(router.getComponent())); return fillRelatedOperations(router.getName().getName(), describeAllRoutes(router.getComponent()));
} }
Expand Down
14 changes: 9 additions & 5 deletions restx-apidocs/src/main/java/restx/apidocs/ApiDocsIndexRoute.java
Expand Up @@ -63,11 +63,15 @@ private List<ImmutableMap<String, String>> buildApis() {
List<ImmutableMap<String, String>> apis = Lists.newArrayList(); List<ImmutableMap<String, String>> apis = Lists.newArrayList();
for (NamedComponent<RestxRouter> router : routers) { for (NamedComponent<RestxRouter> router : routers) {
String routerApiPath = getRouterApiPath(router.getName().getName()); String routerApiPath = getRouterApiPath(router.getName().getName());
apis.add(ImmutableMap.of( if (ApiDeclarationRoute.getRouterByName(factory, routerApiPath).isPresent()) {
"path", "/@/api-docs/" + routerApiPath, // we add the api only if we can find back the router from the name, otherwise it will trigger
"name", routerApiPath, // 404 errors in API-DOCS
"group", router.getComponent().getGroupName(), apis.add(ImmutableMap.of(
"description", "")); "path", "/@/api-docs/" + routerApiPath,
"name", routerApiPath,
"group", router.getComponent().getGroupName(),
"description", ""));
}
} }
return apis; return apis;
} }
Expand Down

0 comments on commit 98f97f1

Please sign in to comment.