-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New Feature: @Jacksonized @Accessors #3860
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
Merged
rzwitserloot
merged 23 commits into
projectlombok:master
from
janrieke:jacksonizedAccessors
May 23, 2025
Merged
New Feature: @Jacksonized @Accessors #3860
rzwitserloot
merged 23 commits into
projectlombok:master
from
janrieke:jacksonizedAccessors
May 23, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
revert unnecessary import changes
Awesome work. Nothing of note during review; just used it as a springboard to make a couple of style things more consistent. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The PR adds a new feature for the existing
@Jacksonized
annotation. Up to now,@Jacksonized
only affected@Builder
or@SuperBuilder
. With this PR,@Jacksonized
can be used in combination with@Accessors(fluent=true)
.Jackson identifies corresponding accessors following the beanspec conventions. If you use fluent accessors, Jackson does not make the connection between the field and the corresponding accessors, leading to strange behavior when de-/serializing. You can manually mitigate this by adding
@JsonPropery("propertyName")
annotations on the field, the setter and the getter (using@Getter/@Setter(onMethod_={@JsonPropery("propertyName")})
). However, this is cumbersome and error-prone (because when renaming a field you also have to manually adjust all annotations).With
@Jacksonized @Accessors(fluent=true)
, lombok will automatically create the relevant annotations such that Jackson correctly identifies fluent accessors.You can simply write classes like this:
Lombok will generate fluent getters and setters with the required Jackson annotations, such that this class can be de-/serialized by Jackson without further modifications.
This resolves #3265, #3270.
Related issue: #2141
Further changes:
Together with this new feature, this PR changes a lombok behavior that has caused some issues since its introduction in v1.18.16. With that version, lombok started automatically copying certain Jackson annotations to the setter. However, this behavior cannot be switched off, and it may even be harmful in certain situations (e.g. #3769, #2769, #3263): Jackson considers some annotations inheritable, and this "virtual inheritance" only affects annotations on setter/getter, but not on private fields. Automatically copying annotations therefore may change the de-/serialization behavior of subclasses.
In general, it is not necessary to copy those annotations at all: Jackson considers the Java field and the corresponding setter and getter methods to be connected (given that Jackson can "beanspec"-identify the connection), and it is sufficient for an annotation to be present on one of those to become effective for all. Thus, the new default behavior with this PR is not to copy, but you can switch back to the previous behavior using
lombok.copyJacksonAnnotationsToAccessors = true
.