Skip to content
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

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

Closed
davidvc opened this issue Apr 28, 2021 · 13 comments
Closed

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

davidvc opened this issue Apr 28, 2021 · 13 comments
Labels
parked Without further feedback this bug cannot be processed. If no feedback is provided, we close these.

Comments

@davidvc
Copy link

davidvc commented Apr 28, 2021

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):

  • Lombok version 1.8.2
  • java version 11.
openjdk version "11.0.1" 2018-10-16 LTS
OpenJDK Runtime Environment Zulu11.2+2 (build 11.0.1+13-LTS)
OpenJDK 64-Bit Server VM Zulu11.2+2 (build 11.0.1+13-LTS, mixed mode)
@dingsheng1214
Copy link

It works for me
image

@P041
Copy link

P041 commented Jan 27, 2022

This combination of annotations does not work.

Screenshot 2022-01-27 at 22 27 18

@janrieke
Copy link
Contributor

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
Copy link

P041 commented Jan 29, 2022

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
Copy link

same here

@janrieke
Copy link
Contributor

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
Copy link

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
Copy link
Contributor

janrieke commented Mar 16, 2022

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
Copy link
Collaborator

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

@rzwitserloot rzwitserloot added the parked Without further feedback this bug cannot be processed. If no feedback is provided, we close these. label Mar 16, 2022
@sysid
Copy link

sysid commented Nov 11, 2022

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

@nucatus
Copy link

nucatus commented May 17, 2023

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
Copy link

@nucatus
You save my ass

@rzwitserloot
Copy link
Collaborator

seems fixed then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parked Without further feedback this bug cannot be processed. If no feedback is provided, we close these.
Projects
None yet
Development

No branches or pull requests

9 participants