Skip to content

Commit 8d90950

Browse files
jaxrs: fixed resource path
1 parent c9deda3 commit 8d90950

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/compiler/ModelCompiler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,11 @@ private TsModel createJaxrsInterface(SymbolTable symbolTable, TsModel tsModel, M
287287
tsModel.getTypeAliases().add(responseTypeAlias);
288288
// application interface
289289
final String applicationPath = jaxrsApplication.getApplicationPath();
290-
final String pathPrefix = applicationPath != null && !applicationPath.isEmpty() ? applicationPath + "/" : "";
291290
final List<TsMethodModel> methods = new ArrayList<>();
292291
final Map<String, Long> methodNamesCount = groupingByMethodName(jaxrsApplication.getMethods());
293292
for (JaxrsMethodModel method : jaxrsApplication.getMethods()) {
294293
final boolean createLongName = methodNamesCount.get(method.getName()) > 1;
295-
methods.add(processJaxrsMethod(symbolTable, pathPrefix, responseSymbol, method, createLongName));
294+
methods.add(processJaxrsMethod(symbolTable, applicationPath, responseSymbol, method, createLongName));
296295
}
297296
final String applicationName = jaxrsApplication.getApplicationName() != null ? jaxrsApplication.getApplicationName() : "RestApplication";
298297
final TsBeanModel interfaceModel = new TsBeanModel(null, false, symbolTable.getSyntheticSymbol(applicationName), null, null, null, null, null, methods, null);
@@ -301,6 +300,7 @@ private TsModel createJaxrsInterface(SymbolTable symbolTable, TsModel tsModel, M
301300
}
302301

303302
private static Map<String, Long> groupingByMethodName(List<JaxrsMethodModel> methods) {
303+
// Java 8
304304
// return methods.stream().collect(Collectors.groupingBy(JaxrsMethodModel::getName, Collectors.counting()));
305305
final Map<String, Long> methodNamesCount = new LinkedHashMap<>();
306306
for (JaxrsMethodModel method : methods) {
@@ -314,7 +314,7 @@ private static Map<String, Long> groupingByMethodName(List<JaxrsMethodModel> met
314314

315315
private TsMethodModel processJaxrsMethod(SymbolTable symbolTable, String pathPrefix, Symbol responseSymbol, JaxrsMethodModel method, boolean createLongName) {
316316
final List<String> comments = new ArrayList<>();
317-
final String path = pathPrefix + method.getPath();
317+
final String path = Utils.joinPath(pathPrefix, method.getPath());
318318
comments.add("HTTP " + method.getHttpMethod() + " /" + path);
319319
final List<TsParameterModel> parameters = new ArrayList<>();
320320
// path params

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/parser/JaxrsApplicationParser.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,6 @@ private void parseResourceMethod(Result result, ResourceContext context, Class<?
175175
}
176176
}
177177

178-
private static String trimSlash(String path) {
179-
final int begin = path.startsWith("/") ? 1 : 0;
180-
final int end = path.length() - (path.endsWith("/") ? 1 : 0);
181-
return path.substring(begin, end);
182-
}
183-
184178
private void foundType(Result result, Type type, Class<?> usedInClass, String usedInMember) {
185179
if (!isExcluded(type)) {
186180
result.discoveredTypes.add(new SourceType<>(type, usedInClass, usedInMember));
@@ -278,8 +272,7 @@ private static class ResourceContext {
278272
public final Map<String, Type> pathParamTypes;
279273

280274
public ResourceContext(String path) {
281-
this.path = path;
282-
this.pathParamTypes = new LinkedHashMap<>();
275+
this(path, new LinkedHashMap<String, Type>());
283276
}
284277

285278
private ResourceContext(String path, Map<String, Type> pathParamTypes) {
@@ -288,8 +281,8 @@ private ResourceContext(String path, Map<String, Type> pathParamTypes) {
288281
}
289282

290283
ResourceContext subPath(Path pathAnnotation) {
291-
final String subPath = path + (pathAnnotation != null ? "/" + trimSlash(pathAnnotation.value()) : "");
292-
return new ResourceContext(subPath, pathParamTypes);
284+
final String subPath = pathAnnotation != null ? pathAnnotation.value() : null;
285+
return new ResourceContext(Utils.joinPath(path, subPath), pathParamTypes);
293286
}
294287

295288
ResourceContext subPathParamTypes(Map<String, Type> subPathParamTypes) {

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/util/Utils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ public static String join(Iterable<? extends Object> values, String delimiter) {
2626
return sb.toString();
2727
}
2828

29+
public static String joinPath(String part1, String part2) {
30+
final List<String> parts = new ArrayList<>();
31+
addPathPart(parts, part1);
32+
addPathPart(parts, part2);
33+
return join(parts, "/");
34+
}
35+
36+
private static void addPathPart(List<String> parts, String part) {
37+
if (part != null) {
38+
final String trimmed = trimSlash(part);
39+
if (!trimmed.isEmpty()) {
40+
parts.add(trimmed);
41+
}
42+
}
43+
}
44+
45+
private static String trimSlash(String path) {
46+
path = path.startsWith("/") ? path.substring(1) : path;
47+
path = path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
48+
return path;
49+
}
50+
2951
public static Class<?> getRawClassOrNull(Type type) {
3052
if (type instanceof Class<?>) {
3153
return (Class<?>) type;

0 commit comments

Comments
 (0)