From 98f97f12653bd2a8006b2d6b1cfbd27667ef1e82 Mon Sep 17 00:00:00 2001 From: Xavier Hanin Date: Tue, 3 Feb 2015 21:46:48 +0100 Subject: [PATCH] FIX: don't list routers in api docs index that can't be loaded by api declaration route (Fixes #130) --- .../restx/apidocs/ApiDeclarationRoute.java | 21 +++++++++++-------- .../java/restx/apidocs/ApiDocsIndexRoute.java | 14 ++++++++----- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/restx-apidocs/src/main/java/restx/apidocs/ApiDeclarationRoute.java b/restx-apidocs/src/main/java/restx/apidocs/ApiDeclarationRoute.java index f8b126827..1ee926464 100644 --- a/restx-apidocs/src/main/java/restx/apidocs/ApiDeclarationRoute.java +++ b/restx-apidocs/src/main/java/restx/apidocs/ApiDeclarationRoute.java @@ -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> router = Optional.absent(); - ImmutableList 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> router = getRouterByName(factory, routerName); if (!router.isPresent()) { return Optional.absent(); @@ -76,6 +68,17 @@ protected Optional doRoute(RestxRequest restxRequest, RestxRequestMatch match .build()); } + static Optional> getRouterByName(Factory f, String routerName) { + routerName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, routerName); + + Optional> router = Optional.absent(); + ImmutableList 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 buildApis(NamedComponent router) { return fillRelatedOperations(router.getName().getName(), describeAllRoutes(router.getComponent())); } diff --git a/restx-apidocs/src/main/java/restx/apidocs/ApiDocsIndexRoute.java b/restx-apidocs/src/main/java/restx/apidocs/ApiDocsIndexRoute.java index 78271bc89..206f07b4e 100644 --- a/restx-apidocs/src/main/java/restx/apidocs/ApiDocsIndexRoute.java +++ b/restx-apidocs/src/main/java/restx/apidocs/ApiDocsIndexRoute.java @@ -63,11 +63,15 @@ private List> buildApis() { List> apis = Lists.newArrayList(); for (NamedComponent 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; }