Skip to content

Commit a6d0788

Browse files
committed
- fix duplicate method name when overloading a method
- remove println statements from tests
1 parent 85bafea commit a6d0788

File tree

6 files changed

+91
-39
lines changed

6 files changed

+91
-39
lines changed

modules/jooby-apt/src/main/java/io/jooby/apt/JoobyProcessor.java

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,22 +299,51 @@ private Map<TypeElement, MvcRouter> buildRouteRegistry(
299299

300300
// Generate unique method name by router
301301
for (var router : registry.values()) {
302-
// Initialize with supports/create method from MvcFactory (avoid name collision)
303-
var names = new HashSet<>();
304-
for (var route : router.getRoutes()) {
305-
if (!names.add(route.getMethodName())) {
302+
// Split routes by their target generated classes to avoid false collisions
303+
var restAndTrpcRoutes = router.getRoutes().stream().filter(r -> !r.isJsonRpc()).toList();
304+
305+
var rpcRoutes = router.getRoutes().stream().filter(MvcRoute::isJsonRpc).toList();
306+
307+
resolveGeneratedNames(restAndTrpcRoutes);
308+
resolveGeneratedNames(rpcRoutes);
309+
}
310+
return registry;
311+
}
312+
313+
private void resolveGeneratedNames(List<MvcRoute> routes) {
314+
// Group by the actual target method name in the generated class
315+
var grouped =
316+
routes.stream()
317+
.collect(
318+
Collectors.groupingBy(
319+
route -> {
320+
String baseName = route.getMethodName();
321+
return route.isTrpc()
322+
? "trpc"
323+
+ Character.toUpperCase(baseName.charAt(0))
324+
+ baseName.substring(1)
325+
: baseName;
326+
}));
327+
328+
for (var overloads : grouped.values()) {
329+
if (overloads.size() == 1) {
330+
// No conflict in this specific output file, use the clean original name
331+
overloads.get(0).setGeneratedName(overloads.get(0).getMethodName());
332+
} else {
333+
// Conflict detected: generate names based on parameter types
334+
for (var route : overloads) {
306335
var paramsString =
307336
route.getRawParameterTypes(true).stream()
308337
.map(it -> it.substring(Math.max(0, it.lastIndexOf(".") + 1)))
309338
.map(it -> Character.toUpperCase(it.charAt(0)) + it.substring(1))
310339
.collect(Collectors.joining());
340+
341+
// A 0-arg method gets exactly the base name.
342+
// Methods with args get the base name + their parameter types.
311343
route.setGeneratedName(route.getMethodName() + paramsString);
312-
} else {
313-
route.setGeneratedName(route.getMethodName());
314344
}
315345
}
316346
}
317-
return registry;
318347
}
319348

320349
/**

modules/jooby-apt/src/main/java/io/jooby/internal/apt/MvcRoute.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public String getProjection() {
8383
return projection.stream().findFirst().orElse(null);
8484
}
8585

86+
public boolean isTrpc() {
87+
return isTrpc;
88+
}
89+
8690
public boolean isProjection() {
8791
if (returnType.is(Types.PROJECTED)) {
8892
return false;

modules/jooby-apt/src/test/java/tests/i3863/C3863.java

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
package tests.i3863;
77

8+
import java.util.List;
9+
10+
import io.jooby.Context;
811
import io.jooby.annotation.*;
912

1013
@Trpc("users")
@@ -15,34 +18,31 @@ public String ping(Integer year) {
1518
return null;
1619
}
1720

18-
// @Trpc.Query
19-
// public String ping() {
20-
// return null;
21-
// }
22-
//
23-
// @Trpc.Query
24-
// public void clear() {
25-
//
26-
// }
27-
//
28-
// @Trpc.Query
29-
// public U3863 findUser(Context ctx, @PathParam long id) {
30-
// return null;
31-
// }
32-
//
33-
// @Trpc.Query
34-
// public List<U3863> multipleSimpleArgs(String q, byte type) {
35-
// return null;
36-
// }
37-
//
38-
// @Trpc.Query
39-
// public List<U3863> multipleComplexArguments(U3863 current, List<U3863> users) {
40-
// return null;
41-
// }
42-
//
43-
// @Trpc.Mutation
44-
// public U3863 updateUser(String id, U3863 payload) {
45-
// return null;
46-
// }
21+
@Trpc.Query
22+
public String ping() {
23+
return null;
24+
}
25+
26+
@Trpc.Query
27+
public void clear() {}
28+
29+
@Trpc.Query
30+
public U3863 findUser(Context ctx, @PathParam long id) {
31+
return null;
32+
}
33+
34+
@Trpc.Query
35+
public List<U3863> multipleSimpleArgs(String q, byte type) {
36+
return null;
37+
}
38+
39+
@Trpc.Query
40+
public List<U3863> multipleComplexArguments(U3863 current, List<U3863> users) {
41+
return null;
42+
}
4743

44+
@Trpc.Mutation
45+
public U3863 updateUser(String id, U3863 payload) {
46+
return null;
47+
}
4848
}

modules/jooby-apt/src/test/java/tests/i3863/Issue3863.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
package tests.i3863;
77

8+
import static org.assertj.core.api.Assertions.assertThat;
9+
810
import org.junit.jupiter.api.Test;
911

1012
import io.jooby.apt.ProcessorRunner;
@@ -15,7 +17,12 @@ public void shouldGenerateTrpcService() throws Exception {
1517
new ProcessorRunner(new C3863())
1618
.withSourceCode(
1719
source -> {
18-
System.out.println(source);
20+
assertThat(source)
21+
.contains("app.get(\"/trpc/users.ping\", this::trpcPingInteger);")
22+
.contains("app.get(\"/trpc/users.ping\", this::trpcPing);")
23+
.contains(
24+
"public io.jooby.trpc.TrpcResponse<java.util.List<tests.i3863.U3863>>"
25+
+ " trpcMultipleSimpleArgs(io.jooby.Context ctx) throws Exception {");
1926
});
2027
}
2128
}

modules/jooby-apt/src/test/java/tests/i3864/C3864.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
@JsonRpc("users")
1212
public class C3864 {
1313

14-
@JsonRpc
1514
public String ping(Context ctx, int year) {
1615
return null;
1716
}
17+
18+
public String createUser(Context ctx, String name) {
19+
return null;
20+
}
1821
}

modules/jooby-apt/src/test/java/tests/i3864/Issue3868.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
package tests.i3864;
77

8+
import static org.assertj.core.api.Assertions.assertThat;
9+
810
import org.junit.jupiter.api.Test;
911

1012
import io.jooby.apt.ProcessorRunner;
@@ -15,7 +17,14 @@ public void shouldGenerateJsonRpcService() throws Exception {
1517
new ProcessorRunner(new C3864())
1618
.withSourceCode(
1719
source -> {
18-
System.out.println(source);
20+
assertThat(source)
21+
.contains(
22+
"public class C3864Rpc_ implements io.jooby.jsonrpc.JsonRpcService,"
23+
+ " io.jooby.Extension {")
24+
.contains("public java.util.List<String> getMethods() {")
25+
.contains(
26+
"public Object execute(io.jooby.Context ctx, io.jooby.jsonrpc.JsonRpcRequest"
27+
+ " req) throws Exception {");
1928
});
2029
}
2130
}

0 commit comments

Comments
 (0)