Skip to content

Commit 98f97f1

Browse files
committed
FIX: don't list routers in api docs index that can't be loaded by api declaration route (Fixes #130)
1 parent e076409 commit 98f97f1

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

restx-apidocs/src/main/java/restx/apidocs/ApiDeclarationRoute.java

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package restx.apidocs;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
43
import com.fasterxml.jackson.databind.ObjectWriter;
54
import com.google.common.base.CaseFormat;
65
import com.google.common.base.Optional;
7-
import com.google.common.base.Predicate;
86
import com.google.common.collect.*;
97
import restx.*;
108
import restx.description.*;
@@ -54,13 +52,7 @@ public ApiDeclarationRoute(@Named(FrontObjectMapperFactory.WRITER_NAME) ObjectWr
5452
@Override
5553
protected Optional<?> doRoute(RestxRequest restxRequest, RestxRequestMatch match, Object body) throws IOException {
5654
String routerName = match.getPathParam("router");
57-
routerName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, routerName);
58-
59-
Optional<NamedComponent<RestxRouter>> router = Optional.absent();
60-
ImmutableList<String> suffixes = ImmutableList.of("ResourceRouter", "", "Resource", "Router");
61-
for (int i = 0; i < suffixes.size() && !router.isPresent(); i++) {
62-
router = factory.queryByName(Name.of(RestxRouter.class, routerName + suffixes.get(i))).optional().findOne();
63-
}
55+
Optional<NamedComponent<RestxRouter>> router = getRouterByName(factory, routerName);
6456

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

71+
static Optional<NamedComponent<RestxRouter>> getRouterByName(Factory f, String routerName) {
72+
routerName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, routerName);
73+
74+
Optional<NamedComponent<RestxRouter>> router = Optional.absent();
75+
ImmutableList<String> suffixes = ImmutableList.of("ResourceRouter", "", "Resource", "Router");
76+
for (int i = 0; i < suffixes.size() && !router.isPresent(); i++) {
77+
router = f.queryByName(Name.of(RestxRouter.class, routerName + suffixes.get(i))).optional().findOne();
78+
}
79+
return router;
80+
}
81+
7982
private List<ResourceDescription> buildApis(NamedComponent<RestxRouter> router) {
8083
return fillRelatedOperations(router.getName().getName(), describeAllRoutes(router.getComponent()));
8184
}

restx-apidocs/src/main/java/restx/apidocs/ApiDocsIndexRoute.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,15 @@ private List<ImmutableMap<String, String>> buildApis() {
6363
List<ImmutableMap<String, String>> apis = Lists.newArrayList();
6464
for (NamedComponent<RestxRouter> router : routers) {
6565
String routerApiPath = getRouterApiPath(router.getName().getName());
66-
apis.add(ImmutableMap.of(
67-
"path", "/@/api-docs/" + routerApiPath,
68-
"name", routerApiPath,
69-
"group", router.getComponent().getGroupName(),
70-
"description", ""));
66+
if (ApiDeclarationRoute.getRouterByName(factory, routerApiPath).isPresent()) {
67+
// we add the api only if we can find back the router from the name, otherwise it will trigger
68+
// 404 errors in API-DOCS
69+
apis.add(ImmutableMap.of(
70+
"path", "/@/api-docs/" + routerApiPath,
71+
"name", routerApiPath,
72+
"group", router.getComponent().getGroupName(),
73+
"description", ""));
74+
}
7175
}
7276
return apis;
7377
}

0 commit comments

Comments
 (0)