Skip to content

Commit cb27ee7

Browse files
authored
Merge pull request #47065 from Postremus/issues/40831-rest-resource-methods-same-name
Fix ClassCastException when ResourceMethod with same method name exist
2 parents 44c26d6 + 2db147e commit cb27ee7

File tree

4 files changed

+95
-11
lines changed

4 files changed

+95
-11
lines changed

core/runtime/src/main/java/io/quarkus/runtime/util/HashUtil.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ private static MessageDigest getMessageDigest(String alg) {
1616

1717
private static void toHex(byte[] digest, StringBuilder sb) {
1818
for (int i = 0; i < digest.length; ++i) {
19-
sb.append(Integer.toHexString((digest[i] & 0xFF) | 0x100).substring(1, 3));
19+
sb.append(Integer.toHexString((digest[i] & 0xFF) | 0x100), 1, 3);
2020
}
2121
}
2222

extensions/resteasy-reactive/rest/deployment/src/main/java/io/quarkus/resteasy/reactive/server/deployment/QuarkusInvokerFactory.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package io.quarkus.resteasy.reactive.server.deployment;
22

33
import java.lang.reflect.Modifier;
4+
import java.util.Arrays;
45
import java.util.function.Supplier;
56

67
import org.jboss.jandex.ClassInfo;
78
import org.jboss.jandex.MethodInfo;
89
import org.jboss.jandex.Type;
9-
import org.jboss.resteasy.reactive.common.model.MethodParameter;
1010
import org.jboss.resteasy.reactive.common.model.ResourceMethod;
1111
import org.jboss.resteasy.reactive.common.processor.HashUtil;
1212
import org.jboss.resteasy.reactive.server.processor.EndpointInvokerFactory;
@@ -34,14 +34,14 @@ public QuarkusInvokerFactory(BuildProducer<GeneratedClassBuildItem> generatedCla
3434
@Override
3535
public Supplier<EndpointInvoker> create(ResourceMethod method, ClassInfo currentClassInfo, MethodInfo info) {
3636

37-
StringBuilder sigBuilder = new StringBuilder();
38-
sigBuilder.append(method.getName())
39-
.append(method.getReturnType());
40-
for (MethodParameter t : method.getParameters()) {
41-
sigBuilder.append(t);
42-
}
37+
String endpointIdentifier = info.toString() +
38+
method.getHttpMethod() +
39+
method.getPath() +
40+
Arrays.toString(method.getConsumes()) +
41+
Arrays.toString(method.getProduces());
42+
4343
String baseName = currentClassInfo.name() + "$quarkusrestinvoker$" + method.getName() + "_"
44-
+ HashUtil.sha1(sigBuilder.toString());
44+
+ HashUtil.sha1(endpointIdentifier);
4545
try (ClassCreator classCreator = new ClassCreator(
4646
new GeneratedClassGizmoAdaptor(generatedClassBuildItemBuildProducer, true), baseName, null,
4747
Object.class.getName(), EndpointInvoker.class.getName())) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package io.quarkus.resteasy.reactive.server.test.resource.basic;
2+
3+
import java.util.List;
4+
5+
import jakarta.enterprise.context.ApplicationScoped;
6+
import jakarta.ws.rs.Consumes;
7+
import jakarta.ws.rs.GET;
8+
import jakarta.ws.rs.POST;
9+
import jakarta.ws.rs.Path;
10+
import jakarta.ws.rs.PathParam;
11+
import jakarta.ws.rs.Produces;
12+
import jakarta.ws.rs.core.MediaType;
13+
14+
import org.hamcrest.Matchers;
15+
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.api.extension.RegisterExtension;
17+
18+
import io.quarkus.test.QuarkusUnitTest;
19+
import io.restassured.RestAssured;
20+
21+
class ResourceMethodSameSignatureTest {
22+
@RegisterExtension
23+
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
24+
.withApplicationRoot((jar) -> jar
25+
.addClass(GreetingResource.class));
26+
27+
@Test
28+
void basicTest() {
29+
RestAssured.get("/greetings/Quarkus")
30+
.then()
31+
.statusCode(200)
32+
.body(Matchers.equalTo("Hello Quarkus"));
33+
34+
RestAssured.get("/greetings/Quarkus/with-question")
35+
.then()
36+
.statusCode(200)
37+
.body(Matchers.equalTo("Hello [Quarkus], how are you?"));
38+
39+
RestAssured.given().contentType("text/plain").post("/greetings/Quarkus")
40+
.then()
41+
.statusCode(200)
42+
.body(Matchers.equalTo("Hello Quarkus"));
43+
44+
RestAssured.given().contentType("application/xml").post("/greetings/Quarkus")
45+
.then()
46+
.statusCode(200)
47+
.body(Matchers.equalTo("Hello [Quarkus], how are you?"));
48+
}
49+
50+
@Path("/greetings")
51+
@ApplicationScoped
52+
public static class GreetingResource {
53+
54+
@GET
55+
@Path("/{name}")
56+
@Produces(MediaType.TEXT_PLAIN)
57+
public String hello(@PathParam("name") String name) {
58+
return "Hello %s".formatted(name);
59+
}
60+
61+
@GET
62+
@Path("/{name}/with-question")
63+
@Produces(MediaType.TEXT_PLAIN)
64+
public String hello(@PathParam("name") List<String> name) {
65+
return "Hello %s, how are you?".formatted(name);
66+
}
67+
68+
@POST
69+
@Path("/{name}")
70+
@Consumes(MediaType.TEXT_PLAIN)
71+
@Produces(MediaType.TEXT_PLAIN)
72+
public String hello(@PathParam("name") String name, String ignored) {
73+
return "Hello %s".formatted(name);
74+
}
75+
76+
@POST
77+
@Path("/{name}")
78+
@Consumes(MediaType.APPLICATION_XML)
79+
@Produces(MediaType.TEXT_PLAIN)
80+
public String hello(@PathParam("name") List<String> name, String ignored) {
81+
return "Hello %s, how are you?".formatted(name);
82+
}
83+
}
84+
}

independent-projects/resteasy-reactive/common/processor/src/main/java/org/jboss/resteasy/reactive/common/processor/HashUtil.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static String sha1(byte[] value) {
2020
byte[] digest = md.digest(value);
2121
StringBuilder sb = new StringBuilder(40);
2222
for (int i = 0; i < digest.length; ++i) {
23-
sb.append(Integer.toHexString((digest[i] & 0xFF) | 0x100).substring(1, 3));
23+
sb.append(Integer.toHexString((digest[i] & 0xFF) | 0x100), 1, 3);
2424
}
2525
return sb.toString();
2626
} catch (NoSuchAlgorithmException e) {
@@ -38,7 +38,7 @@ public static String sha256(byte[] value) {
3838
byte[] digest = md.digest(value);
3939
StringBuilder sb = new StringBuilder(40);
4040
for (int i = 0; i < digest.length; ++i) {
41-
sb.append(Integer.toHexString((digest[i] & 0xFF) | 0x100).substring(1, 3));
41+
sb.append(Integer.toHexString((digest[i] & 0xFF) | 0x100), 1, 3);
4242
}
4343
return sb.toString();
4444
} catch (NoSuchAlgorithmException e) {

0 commit comments

Comments
 (0)