From 26c7ad1eb055ab26bcbe0a032a61f5d7f89ff76c Mon Sep 17 00:00:00 2001 From: Jan Rieke Date: Mon, 31 Mar 2025 11:48:17 +0200 Subject: [PATCH] copy Jackson annotations to the plural builder setter method --- src/core/lombok/ConfigurationKeys.java | 2 +- src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java | 6 +++--- src/core/lombok/eclipse/handlers/HandleSetter.java | 1 + .../handlers/singulars/EclipseGuavaSingularizer.java | 2 +- .../singulars/EclipseJavaUtilListSetSingularizer.java | 2 +- .../handlers/singulars/EclipseJavaUtilMapSingularizer.java | 2 +- test/transform/resource/before/JacksonJsonProperty.java | 1 + 7 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/core/lombok/ConfigurationKeys.java b/src/core/lombok/ConfigurationKeys.java index 18d7cb9a..e2ec1426 100644 --- a/src/core/lombok/ConfigurationKeys.java +++ b/src/core/lombok/ConfigurationKeys.java @@ -747,7 +747,7 @@ public class ConfigurationKeys { /** * lombok configuration: {@code lombok.copyJacksonAnnotationsToSetters} = {@code true} | {@code false}. * - * If true, copy certain Jackson annotations from a field to its corresponding getter. This was the behavior from lombok 1.18.16 to 1.18.36. + * If true, copy certain Jackson annotations from a field to its corresponding getter. This was the behavior from lombok 1.18.16 to 1.18.38. * However, it turned out to be harmful in certain situations. Thus, the default is now false. */ public static final ConfigurationKey COPY_JACKSON_ANNOTATIONS_TO_SETTERS = new ConfigurationKey("lombok.copyJacksonAnnotationsToSetters", "Copy these annotations to getters, setters, with methods, builder-setters, etc.") {}; diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 3a18d049..6f5ae8bc 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -829,10 +829,10 @@ public class EclipseHandlerUtil { /** * Searches the given field node for annotations that are specifically intentioned to be copied to the setter. * - * @param forceCopy If true, always copy the annotations regardless of the lombok configuration. + * @param forceCopyJacksonAnnotations If true, always copy the annotations regardless of the lombok configuration. */ - public static Annotation[] findCopyableToSetterAnnotations(EclipseNode node, boolean forceCopy) { - if (!forceCopy) { + public static Annotation[] findCopyableToSetterAnnotations(EclipseNode node, boolean forceCopyJacksonAnnotations) { + if (!forceCopyJacksonAnnotations) { Boolean copyAnnotations = node.getAst().readConfiguration(ConfigurationKeys.COPY_JACKSON_ANNOTATIONS_TO_SETTERS); if (copyAnnotations == null || !copyAnnotations) return EMPTY_ANNOTATIONS_ARRAY; } diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java index d05d923e..e0bea89b 100644 --- a/src/core/lombok/eclipse/handlers/HandleSetter.java +++ b/src/core/lombok/eclipse/handlers/HandleSetter.java @@ -113,6 +113,7 @@ public class HandleSetter extends EclipseAnnotationHandler { // If a fluent setter should be generated, force-copy annotations from the field. AnnotationValues accessorsForField = EclipseHandlerUtil.getAccessorsForField(fieldNode); + // Copying Jackson annotations is required for fluent accessors (otherwise Jackson would not find the accessor). Annotation[] copyableToSetterAnnotations = findCopyableToSetterAnnotations(fieldNode, accessorsForField.isExplicit("fluent")); onMethod = new ArrayList(onMethod); onMethod.addAll(Arrays.asList(copyableToSetterAnnotations)); diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java index eff24cd4..e77bde74 100755 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseGuavaSingularizer.java @@ -219,7 +219,7 @@ abstract class EclipseGuavaSingularizer extends EclipseSingularizer { char[] prefixedSelector = data.getSetterPrefix().length == 0 ? data.getPluralName() : HandlerUtil.buildAccessorName(builderType, new String(data.getSetterPrefix()), new String(data.getPluralName())).toCharArray(); md.selector = fluent ? prefixedSelector : HandlerUtil.buildAccessorName(builderType, "addAll", new String(data.getPluralName())).toCharArray(); Annotation[] selfReturnAnnotations = generateSelfReturnAnnotations(deprecate, data.getSource()); - Annotation[] copyToSetterAnnotations = copyAnnotations(md, findCopyableToSetterAnnotations(data.getAnnotation().up(), false)); + Annotation[] copyToSetterAnnotations = copyAnnotations(md, findCopyableToSetterAnnotations(data.getAnnotation().up(), true)); md.annotations = concat(selfReturnAnnotations, copyToSetterAnnotations, Annotation.class); if (returnStatement != null) createRelevantNonNullAnnotation(builderType, md); diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSetSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSetSingularizer.java index c94666e0..2802bf7a 100755 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSetSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilListSetSingularizer.java @@ -195,7 +195,7 @@ abstract class EclipseJavaUtilListSetSingularizer extends EclipseJavaUtilSingula char[] prefixedSelector = data.getSetterPrefix().length == 0 ? data.getPluralName() : HandlerUtil.buildAccessorName(builderType, new String(data.getSetterPrefix()), new String(data.getPluralName())).toCharArray(); md.selector = fluent ? prefixedSelector : HandlerUtil.buildAccessorName(builderType, "addAll", new String(data.getPluralName())).toCharArray(); Annotation[] selfReturnAnnotations = generateSelfReturnAnnotations(deprecate, data.getSource()); - Annotation[] copyToSetterAnnotations = copyAnnotations(md, findCopyableToSetterAnnotations(data.getAnnotation().up(), false)); + Annotation[] copyToSetterAnnotations = copyAnnotations(md, findCopyableToSetterAnnotations(data.getAnnotation().up(), true)); md.annotations = concat(selfReturnAnnotations, copyToSetterAnnotations, Annotation.class); if (returnStatement != null) createRelevantNonNullAnnotation(builderType, md); diff --git a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java index 0e3ab0dd..e9ee2252 100755 --- a/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java +++ b/src/core/lombok/eclipse/handlers/singulars/EclipseJavaUtilMapSingularizer.java @@ -332,7 +332,7 @@ public class EclipseJavaUtilMapSingularizer extends EclipseJavaUtilSingularizer md.selector = setterName.toCharArray(); Annotation[] selfReturnAnnotations = generateSelfReturnAnnotations(deprecate, data.getSource()); - Annotation[] copyToSetterAnnotations = copyAnnotations(md, findCopyableToSetterAnnotations(data.getAnnotation().up(), false)); + Annotation[] copyToSetterAnnotations = copyAnnotations(md, findCopyableToSetterAnnotations(data.getAnnotation().up(), true)); md.annotations = concat(selfReturnAnnotations, copyToSetterAnnotations, Annotation.class); if (returnStatement != null) createRelevantNonNullAnnotation(builderType, md); diff --git a/test/transform/resource/before/JacksonJsonProperty.java b/test/transform/resource/before/JacksonJsonProperty.java index c0bccfae..4d3018cc 100644 --- a/test/transform/resource/before/JacksonJsonProperty.java +++ b/test/transform/resource/before/JacksonJsonProperty.java @@ -1,3 +1,4 @@ +//CONF: lombok.copyJacksonAnnotationsToSetters = true //version 8: Jackson deps are at least Java7+. import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter;