From 7bbb0a83d9ca27f97adea79c7ac57c5316df2d4c Mon Sep 17 00:00:00 2001 From: Cristian Burlacu Date: Tue, 12 Nov 2024 21:59:29 +0100 Subject: [PATCH] fix: support for short and uncommon field names like set, get, and is. --- .../processor/JacksonCodeGenerator.java | 7 ++- .../FieldNameSetGetPrefixResourceTest.java | 52 +++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 extensions/resteasy-reactive/rest-jackson/deployment/src/test/java/io/quarkus/resteasy/reactive/jackson/deployment/test/FieldNameSetGetPrefixResourceTest.java diff --git a/extensions/resteasy-reactive/rest-jackson/deployment/src/main/java/io/quarkus/resteasy/reactive/jackson/deployment/processor/JacksonCodeGenerator.java b/extensions/resteasy-reactive/rest-jackson/deployment/src/main/java/io/quarkus/resteasy/reactive/jackson/deployment/processor/JacksonCodeGenerator.java index d407c7e6ec443..b822619d7bc9f 100644 --- a/extensions/resteasy-reactive/rest-jackson/deployment/src/main/java/io/quarkus/resteasy/reactive/jackson/deployment/processor/JacksonCodeGenerator.java +++ b/extensions/resteasy-reactive/rest-jackson/deployment/src/main/java/io/quarkus/resteasy/reactive/jackson/deployment/processor/JacksonCodeGenerator.java @@ -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; @@ -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")) { return methodInfo.parameterType(0); } return methodInfo.returnType(); @@ -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); } diff --git a/extensions/resteasy-reactive/rest-jackson/deployment/src/test/java/io/quarkus/resteasy/reactive/jackson/deployment/test/FieldNameSetGetPrefixResourceTest.java b/extensions/resteasy-reactive/rest-jackson/deployment/src/test/java/io/quarkus/resteasy/reactive/jackson/deployment/test/FieldNameSetGetPrefixResourceTest.java new file mode 100644 index 0000000000000..6a483942bc139 --- /dev/null +++ b/extensions/resteasy-reactive/rest-jackson/deployment/src/test/java/io/quarkus/resteasy/reactive/jackson/deployment/test/FieldNameSetGetPrefixResourceTest.java @@ -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) { + } + } + +}