jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

It would be good if custom annotations from value class were used for Builder too

Open AndreasFagschlunger opened this issue 4 years ago • 5 comments

Hi! Maybe this is more a feature request but currently you have to add custom annotations (JacksonAnnotationsInside) to the builder class as well do make deserialization work, e.g.:

@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonSerialize(using = SpecialSnowflakeSerializer.class)
@JsonDeserialize(using = SpecialSnowflakeDeserializer.class)
public @interface CustomAnnotation {

}

If I do some special stuff in SpecialSnowflake(De)Serializer classes, deserialization works only if I add @CustomAnnotation to the property in Builder as well. Duplication of code and error-prone IMHO. Maybe merge annotations from the "source" properties into the builder as it happens?

@JsonDeserialize(builder = Person.Builder.class)
public final class Person {

	@JsonPOJOBuilder(withPrefix = "")
	public static final class Builder {

		private String firstName;
		private String lastName;

		// @CustomAnnotation
		private int age;

		public Builder firstName(String firstName) {
			this.firstName = firstName;
			return this;
		}

		public Builder lastName(String lastName) {
			this.lastName = lastName;
			return this;
		}

		public Builder age(int age) {
			this.age = age;
			return this;
		}

		public Person build() {
			return new Person(this);
		}
	}

	private String firstName;
	private String lastName;
	@CustomAnnotation
	private int age;

	private Person(Builder builder) {
		firstName = builder.firstName;
		lastName = builder.lastName;
		age = builder.age;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public int getAge() {
		return age;
	}

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

	@Override
	public String toString() {
		return "Person [firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + "]";
	}
}

AndreasFagschlunger avatar Jul 25 '19 20:07 AndreasFagschlunger