projectlombok / lombok

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

[BUG] com.fasterxml.jackson.annotation.JsonAlias annotation is not copied to builder #2406

Open AngryGami opened 4 years ago

AngryGami commented 4 years ago

Describe the bug com.fasterxml.jackson.annotation.JsonAlias annotation is not copied to builder

To Reproduce

@Builder
@ToString
@JsonDeserialize(builder = AliasTest.AliasTestBuilder.class)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class AliasTest {
    @JsonProperty("testx")
    @JsonAlias({"testxx.a","testx"})
    public final String xxxx;

    @JsonPOJOBuilder(withPrefix = "")
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class AliasTestBuilder {
        //// adding explicit field with annotation helps but boilerplate 
        //@JsonAlias({"testxx.a","testx"})
        //private String xxxx;
    }
}

@Test
public void aaa () throws IOException {
    ObjectMapper om = new ObjectMapper();
    AliasTest at = om.readValue("{\"testxx.a\":\"12312312\"}", AliasTest.class);
    AliasTest at2 = om.readValue("{\"testx\":\"12312312\"}", AliasTest.class);
    // both should print 12312312 and now only at2 does
    System.out.println(at.xxxx);
    System.out.println(at2.xxxx);
}

Expected behavior JsonAlias work with lombok generated builder for deserialization I assume this should be easy to fix here.

Version info:

nithatech commented 3 years ago

Hi,

I am seeing the same issue @JsonAlias does not work with lombok Builder. I am using lombok 1.18.16. Is this issue fixed?

janrieke commented 3 years ago

I can confirm this issue is fixed since 1.18.16. Please double-check that you are using that version (both in Maven/Gradle and Eclipse).

nithatech commented 3 years ago

Thanks for the prompt response @janrieke . And apologies for the delay in my response. I have been trying to mask my code to give you the snippet for reproducing the issue.

When I tried to extract that part of the code into a new temp project to share with you the actual error, I am actually seeing another issue: Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Builder class com.test.common.TestResponse$TestResponseBuilder does not have build method (name: 'build'). My original issue is the @JsonAlias is not working (I get nulls instead of the alias)

I am using IntelliJ 2.4 community edition with Lombok 3.3 plugin with Gradle and the dependencies I have are: dependencies { // https://mvnrepository.com/artifact/org.projectlombok/lombok compile group: 'org.projectlombok', name: 'lombok', version: '1.18.16' compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.11.3' compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.11.3' testCompile group: 'junit', name: 'junit', version: '4.12' compile group: 'io.vertx', name: 'vertx-core', version: '3.9.4' }

Below is my code. I have MyResponse which includes (BaseResponse, TestResponseMeta, TestResponse(includes TestResponseData))

`package com.test.common;

import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.annotation.JsonNaming; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; import lombok.extern.jackson.Jacksonized;

@SuperBuilder @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) @Jacksonized @Data @NoArgsConstructor public class BaseResponse {

@JsonProperty Boolean isSuccess;

@JsonProperty Integer httpStatusCode;

@JsonProperty TestResponseMeta responseMetadata; }`

`package com.test.common;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonNaming; import lombok.Builder; import lombok.Value;

@Value @Builder @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) @JsonDeserialize(builder = TestResponseMeta.TestResponseMetaBuilder.class) public class TestResponseMeta {

@JsonProperty String boxName;

@JsonProperty String version;

@JsonIgnoreProperties(ignoreUnknown = true) @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) public static class TestResponseMetaBuilder {

} }`

`package com.test.common;

import com.fasterxml.jackson.annotation.JsonAlias; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonNaming; import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import lombok.Value;

@Builder @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) @JsonDeserialize(builder = TestResponseData.TestResponseDataBuilder.class) @Data public class TestResponseData {

@JsonProperty(value = "my_id") @JsonAlias({"any_id", "all_id"}) String sId;

@JsonProperty Float score;

@JsonPOJOBuilder(withPrefix = "") @JsonIgnoreProperties(ignoreUnknown = true) @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) public static class TestResponseDataBuilder {

} } `

`package com.test.common;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonNaming; import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import java.util.List; import lombok.Builder; import lombok.Getter; import lombok.Value;

@Value @Builder @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) @JsonDeserialize(builder = TestResponse.TestResponseBuilder.class) public class TestResponse { @JsonProperty List data;

@JsonPOJOBuilder(withPrefix = "") @JsonIgnoreProperties(ignoreUnknown = true) @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) public static class TestResponseBuilder {

} } `

`package com.test;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.annotation.JsonNaming; import com.test.common.BaseResponse; import com.test.common.TestResponse; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; import lombok.extern.jackson.Jacksonized;

@SuperBuilder @Jacksonized @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) @Data @NoArgsConstructor @EqualsAndHashCode(callSuper = true) public class MyResponse extends BaseResponse { @JsonProperty TestResponse response; } `

`package com.test;

import io.vertx.core.json.JsonObject;

public class Test { public static void main(String args[]) { String resMacroStr = "{\n"