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 - 2 #3651

Open
markusjevringsesame opened this issue Apr 9, 2024 · 0 comments

Comments

@markusjevringsesame
Copy link

markusjevringsesame commented Apr 9, 2024

Describe the bug
Similarly to the issue reported in #2824,
I'm having issues when using @JsonProperty with @Jacksonized when using @JsonProperty on the getter.
"Why are you doing that?", you ask. Well, I'm not. But openapi-generator is.

To Reproduce

    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Jacksonized
    private static class JacksonExample {
        private String name;
        private int age;

        @JsonProperty("my_name")
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }

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

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

        assertEquals(jacksonExample, result);
    }

This gives me:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "my_name" (class com.whatever.Test$JacksonExample$JacksonExampleBuilder), not marked as ignorable (2 known properties: "name", "age"])
 at [Source: (String)"{"my_name": "Joe", "age": 27}"; line: 1, column: 14] (through reference chain: com.sesame.bison.pubsub.PhotonUserPrescriptionReceiverTest$JacksonExample$JacksonExampleBuilder["my_name"])

Expected behavior
I would expect the value to be deserialized correctly. Alternatively, I'd like an exception to be thrown, if this is indeed not supported.

Version info (please complete the following information):

  • Lombok version: 1.18.32
  • Platform: javac 21

Additional context
You could argue that this issue should be raised with the openapi-generator folks, but we are the ones adding @Jacksonized (for reasons that have been lost to time, but presumably it was needed).
To be clear, I don't expect you to support any and all wacky combinations of annotations. But there's clearly an assumption that having these annotations on the getter should be sufficient. Maybe the openapi-generator folks are wrong about this. That's a possibility.

Should I expect this to work when only the getter is annotated, or must the annotation be attached to the field?

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

No branches or pull requests

1 participant