projectlombok / lombok

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

[BUG] Jackson annotation @JsonProperty not working with @Jacksonized #2824

Closed davidvc closed 12 months ago

davidvc commented 3 years ago

Describe the bug If you use the @JsonProperty annotation on a field to provide a property name that differs from the field name, it does not work. This is with Lombok 1.8.2

To Reproduce Here is a sample class and a unit test that fails:

  @Value
  @Builder
  @Jacksonized
  private static class JacksonExample {
    @JsonProperty("myName")
    String name;
    int age;
  }

  @Test
  public void jsonPropertyAnnotationLetsYouMapAPropertyToAFieldWithDifferentName() throws Exception {
    JacksonExample jacksonExample = JacksonExample.builder().age(27).name("Joe").build();
    String json = "{\"myName\": \"Joe\", \"age\": 27}";

    ObjectMapper objectMapper = new ObjectMapper();
    JacksonExample result = objectMapper.readValue(json, JacksonExample.class);

    assertThat(result, is(equalTo(jacksonExample)));
  }

Here is the error I get:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "myName" (class com.ebay.raptor.viexpsvc.common.lombok.LombokExamplesTest$JacksonExample$JacksonExampleBuilder), not marked as ignorable (2 known properties: "name", "age"])
 at [Source: (String)"{"myName": "Joe", "age": 27}"; line: 1, column: 13] (through reference chain: com.ebay.raptor.viexpsvc.common.lombok.LombokExamplesTest$JacksonExample$JacksonExampleBuilder["myName"])

When I look at the generated code, the Builder does not have any Jackson annotations on it:

    @Generated
    public static class JacksonExampleBuilder {
      @Generated
      private String name;
      @Generated
      private int age;

      @Generated
      JacksonExampleBuilder() {
      }

      @Generated
      public LombokExamplesTest.JacksonExample.JacksonExampleBuilder name(String name) {
        this.name = name;
        return this;
      }

      @Generated
      public LombokExamplesTest.JacksonExample.JacksonExampleBuilder age(int age) {
        this.age = age;
        return this;
      }

      @Generated
      public LombokExamplesTest.JacksonExample build() {
        return new LombokExamplesTest.JacksonExample(this.name, this.age);
      }

      @Generated
      public String toString() {
        return "LombokExamplesTest.JacksonExample.JacksonExampleBuilder(name=" + this.name + ", age=" + this.age + ")";
      }
    }

Expected behavior Attaching @JsonProperty to a field should be honored using @Jacksonized with @Value

Version info (please complete the following information):

dingsheng1214 commented 3 years ago

It works for me image

P041 commented 2 years ago

This combination of annotations does not work.

Screenshot 2022-01-27 at 22 27 18
janrieke commented 2 years ago

I'm quite sure this has been fixed in the meantime. Can you confirm that this does not work with Lombok 1.18.22?

P041 commented 2 years ago

I'm quite sure this has been fixed in the meantime. Can you confirm that this does not work with Lombok 1.18.22?

yep, I'm using this version

org.projectlombok lombok 1.18.22
bkuberek commented 2 years ago

same here

janrieke commented 2 years ago

I just checked again, and cannot reproduce. Your example works for me both in Eclipse and javac (via maven compile). Please double-check that you are actually using the latest version of lombok (also check the version installed in Eclipse, if you are using that IDE).

bkuberek commented 2 years ago

I am. But I circunvented the issue by renaming the field instead of using @JsonProperty. I did not have time to go deeper and I wonder if this could be an Intellij IDEA problem. I experienced this while using Intellij to build. If I have time, I will try reproducing it later.

janrieke commented 2 years ago

Yes, it could be an IntelliJ IDEA issue. @Jacksonized is rather new and experimental, and given that the IntelliJ lombok plugin has its own builder processing, it is very likely that IntelliJ does not (or not correctly) consider this annotation.

Try building with maven/gradle/whatever you use and check whether the problem persists. (If it does, it's a lombok issue, if not, it's on IntelliJ.)

rzwitserloot commented 2 years ago

I think @janrieke has it right. Without feedback, this can be closed 2022-04-01.

sysid commented 1 year ago

Same problem for me (1.18.24). Also using Intellij. Will rename field, no time to go deeper.

nucatus commented 1 year ago

Make sure that all the jackson annotations are imported from the com.fasterxml.jackson.annotation package and NOT from org.codehaus.jackson.annotate package. This is a very easy mistake to make.

serpmelon commented 1 year ago

@nucatus You save my ass

rzwitserloot commented 12 months ago

seems fixed then.