-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
@JsonNaming
on value class not applied to use via Builder
#2712
Comments
Delombokified source: public class Foo {
private Long id;
private String title;
private String shortDescription;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public Foo() {
}
Foo(Long id, String title, String shortDescription) {
super();
this.id = id;
this.title = title;
this.shortDescription = shortDescription;
}
public static FooBuilder builder() {
return new FooBuilder();
}
public static class FooBuilder {
private Long id;
private String title;
private String shortDescription;
public FooBuilder id(Long id) {
this.id = id;
return this;
}
public FooBuilder title(String title) {
this.title = title;
return this;
}
public FooBuilder shortDescription(String shortDescription) {
this.shortDescription = shortDescription;
return this;
}
public Foo build() {
return new Foo(id, title, shortDescription);
}
}
} |
But I think it is reasonable to suggest that at least some annotations from value class should be sort of added as defaults to be used with builder. |
@JsonNaming
on value class not applied to use via Builder
There isn't a direct way to put annotations on generated builder classes, but you can write an empty builder class and lombok will fill it for you: @lombok.Value
@lombok.Builder
@JsonDeserialize(builder = Foo.FooBuilder.class)
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
public class Foo {
private Long id;
private String title;
private String shortDescription;
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
@JsonPOJOBuilder(withPrefix = "")
public static class FooBuilder {
}
} While this isn't perfect, and a solution like you mentioned (inheriting annotations from the value class) would be preferable, it is certainly acceptable as an alternative. Thanks! |
@ganondolf Ok good. And thank you for reporting this usability issue; related problems have come up occasionally but for different annotations. I hope to maybe address this in 2.12, depending on how things go. But at least it is more visible now. |
There will be a new annotation |
@janrieke Wow. That looks really really nice. And even if Jackson was to do this eventually on its own, it is good to have ability to customize handling (and obviously have this available now/soon, instead of "maybe in future"). |
Given an input json string like
"{\"Id\":1,\"Title\":\"title\",\"ShortDescription\":\"desc\"}"
, I'd like to keep java naming conventions on my class, and usePropertyNamingStrategy.UpperCamelCaseStrategy
for deserialization:While this works as intended, if I wanted to make the class (sort of) immutable I'd have to remove the no args constructor, and use the builder to deserialize:
but this results in an empty bean. The builder setters are never invoked, Jackson cannot match them to the input properties:
Currently on Jackson 2.10.3.
The text was updated successfully, but these errors were encountered: