Skip to content

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
merged 23 commits into from
May 23, 2025

Conversation

janrieke
Copy link
Contributor

@janrieke janrieke commented Apr 1, 2025

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:

@Jacksonized @Accessors(fluent = true)
@Getter @Setter
public class MyDTO {
	private int field;
}

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.

@rzwitserloot rzwitserloot merged commit dc690df into projectlombok:master May 23, 2025
55 of 61 checks passed
@rzwitserloot
Copy link
Collaborator

Awesome work. Nothing of note during review; just used it as a springboard to make a couple of style things more consistent.

@rzwitserloot rzwitserloot added this to the next-version milestone May 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate fluent methods plus Getter and Setters
2 participants