Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for short and uncommon field names like set, get, and is #44457

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ protected enum FieldKind {
MAP(true),
TYPE_VARIABLE(true);

private boolean generic;
private final boolean generic;

FieldKind(boolean generic) {
this.generic = generic;
Expand Down Expand Up @@ -281,7 +281,7 @@ private Type fieldType() {
if (isPublicField()) {
return fieldInfo.type();
}
if (methodInfo.name().startsWith("set")) {
if (methodInfo.parametersCount() == 1 && methodInfo.name().startsWith("set")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return methodInfo.parameterType(0);
}
return methodInfo.returnType();
Expand All @@ -304,6 +304,9 @@ private String fieldName() {

private String fieldNameFromMethod(MethodInfo methodInfo) {
String methodName = methodInfo.name();
if (methodName.equals("get") || methodName.equals("set") || methodName.equals("is")) {
return methodName;
}
if (methodName.startsWith("is")) {
return methodName.substring(2, 3).toLowerCase() + methodName.substring(3);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.quarkus.resteasy.reactive.jackson.deployment.test;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import org.hamcrest.Matchers;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.smallrye.common.annotation.NonBlocking;

// Ensures uncommon field names like "set", "get", and "is" are generated correctly.
class FieldNameSetGetPrefixResourceTest {
@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(Resource.class, Resource.UncommonBody.class).addAsResource(
new StringAsset(
"quarkus.rest.jackson.optimization.enable-reflection-free-serializers=true\n"),
"application.properties"));

@Test
void testFieldNameSetGetIsPrefix() {
RestAssured.get("/field-name-prefixes")
.then()
.statusCode(200)
.contentType("application/json")
.body("id", Matchers.equalTo("id"))
.body("set", Matchers.is(true))
.body("get", Matchers.is(true))
.body("is", Matchers.is(false))
.body("setText", Matchers.equalTo("setText"));
}

@NonBlocking
@Path("/field-name-prefixes")
private static class Resource {
@GET
public UncommonBody get() {
return new UncommonBody("id", true, true, false, "setText");
}

private record UncommonBody(String id, boolean set, boolean get, boolean is, String setText) {
}
}

}
Loading