[BUG] Jackson annotation @JsonProperty not working with @Jacksonized - 2
#3,651 opened on Apr 9, 2024
Description
Describe the bug
Similarly to the issue reported in https://github.com/projectlombok/lombok/issues/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?