projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.89k stars 2.39k forks source link

[BUG] Lombok wont copy @JsonFormat annotation to getter method #3511

Open bolds07 opened 1 year ago

bolds07 commented 1 year ago

Describe the bug I think it is a bug on Lombok library or I am missing something really stupid here. It is only copying the @JsonFormat annotation into the setter method, not into the getter

To Reproduce I have a pojo class

@Setter
@Getter
@RequiredArgsConstructor
public class AnnounceV2 {

@JsonProperty("postedAt")
   @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", shape = JsonFormat.Shape.STRING, lenient = OptBoolean.TRUE)
   private Date postedAt;

}

and i've a lombok.config file:

lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Qualifier
lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Value
lombok.copyableAnnotations += com.fasterxml.jackson.annotation.JsonProperty
lombok.copyableAnnotations += com.fasterxml.jackson.annotation.JsonFormat

Expected behavior But even with this configuration ObjectMapper was still serializing that field as ISO date... I lost at least 3 hours and debugged a lot then I decided to look at the generated sources folder in my project

there i fould:

@JsonProperty("postedAt")
   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss",
      shape = Shape.STRING,
      lenient = OptBoolean.TRUE
   )
   public void setPostedAt(@JsonProperty("postedAt") final Date postedAt) {
      this.postedAt = postedAt;
   }

 @JsonProperty("postedAt")
   public Date getPostedAt() {
      return this.postedAt;
   }

the jsonformat annotation is only being copied at the setter, so the objectmapper really can't have a clue on how i want to serialize this field.

Am I doing something wrong? I am missing some detail?

Version info (please complete the following information):

1.18.26

javac 15.0.1

rzwitserloot commented 1 year ago

This sounds related to #3315 which we fixed in the current release (1.18.30). I'm considering this done-already fixed unless you still run into this issue on 1.18.30.

close if no feedback after: 2023-10-02.

magicprinc commented 11 months ago

@bolds07 BTW, Why do you need lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Value? Spring sees it on the field and injects directly into the field. With this config line, don't you have two injections (first into the field and second into the setter)?

Could I recommend my lombok.config lines:

lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Qualifier
lombok.copyableAnnotations += com.fasterxml.jackson.annotation.JsonProperty
lombok.copyableAnnotations += com.fasterxml.jackson.databind.annotation.JsonDeserialize
lombok.copyableAnnotations += com.fasterxml.jackson.annotation.JsonFormat

It probably has sense for constructor injections, though…