aws-sdk-java-v2: Enhanced DynamoDB annotations are incompatible with Lombok

Describe the Feature

Lombok is a popular system for reducing boilerplate code, especially in data-centric objects such as database entries. By annotating the class and/or member variables, it autogenerates setters and getters. This creates problems with enhanced dynamodb, because the annotations such as DynamoDbAttribute and DynamoDbPartitionKey cannot be applied to member variables, only to setter/getter functions, and the setter/getter function is not present in the source code. In DynamoDbMapper (from AWS SDK v1), annotations could be applied to functions or member variables, so Lombok worked well. It would be great if the enhanced DynamoDb library could work the same way.

Is your Feature Request related to a problem?

I’m always frustrated when I cannot use the new SDK because it is incompatible with other software that my development team depends on.

Proposed Solution

Ideally the annotations can be applied to member variables in the same manner that the SDKv1 dynamoDbMapper annotations could. As a second-best alternative, it could allow the annotations to be applied to member variables, but ignore them; with a little work, Lombok can be set to copy annotations from member variables to the setters and getters.

Describe alternatives you’ve considered

The alternative is to write explicit setters and getters for all dynamo-annotation values. This would be a significant pain point, however; we will probably prefer to stay with SDKv1.

Additional Context

Lombok annotations are as follows:

@Setter
@Getter
public class Customer {
  private String id;

  private String email;

  private Instant registrationDate;
}

Functions “setId(String)”, “getId()”, etc. are all generated automatically, so there is no way to get the annotations onto them.

  • I may be able to implement this feature request

Your Environment

  • AWS Java SDK version used: 2.13.47
  • JDK version used: Java 11
  • Operating System and version: Ubuntu (on EC2), AWS Lambda (whatever version of Linux that is)

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 40
  • Comments: 25 (5 by maintainers)

Commits related to this issue

Most upvoted comments

The @Builder(toBuilder = true) Lombok builder feature is also incompatible with enhanced dynamodb annotations. This feature generates a method for creating a builder pre-populated with all the fields of the current object. This feature is extremely useful in the context of DynamoDb mapped immutable objects. For example, code for updating an attribute is very easy to read and more resilient to schema changes:

    Customer customer = table.getItem(customerKey);
    Customer updated = customer.toBulder().name(updatedName).build();
    table.putItem(updated);

However, the enhanced DynamoDb validation fails because it detects a ‘toBuilder’ getter method without an associated setter:

A method was found on the immutable class that does not appear to have a matching setter on the builder class. Use the @DynamoDbIgnore annotation on the method if you do not want it to be included in the TableSchema introspection.

Unfortunately, it is not possible to add an annotation to the ‘toBuilder()’ method generated by Lombok. The only work-around is to hand-code the copy builder method.

If compatibility with Lombok (a other field-based object frameworks) is being pursued, this issue should be considered as well.

Wish this would be resolved in 2023 as it introduced uneeded boilerplate

While upgrading from Java 16 to Java 17 I discovered a new issue related to Lombok and Enhanced DynamoDB. The following code reproduces the issue.

@Builder
@DynamoDbImmutable(builder = Example.ExampleBuilder.class)
public class Example {

    @Getter(onMethod = @__(@DynamoDbPartitionKey))
    private String field1;
    @Getter(onMethod = @__(@DynamoDbSecondaryPartitionKey(indexNames = { "field2-index" })))
    private String field2;

}

This code fails to compile while using these version: Java 17.0.1, Lombok 1.18.22, AWS SDK 2.17.56. The compiler error is:

[ERROR] Lombok annotation handler class lombok.javac.handlers.HandleGetter failed on …\src\main\java\com\temp\Example.java: java.lang.NullPointerException: Cannot assign field “type” because “tree” is null [ERROR] …/src/main/java/com/temp/Example.java:[15,25] cannot find symbol symbol: class __ location: class com.temp.Example

From the observed failures it appears they are caused by the combination of the DynamoDbSecondaryPartitionKey and Getter annotations. I was able to work around the issue by removing the annotation on field2 and replacing it with a full getter implementation as follows:

@Builder
@DynamoDbImmutable(builder = Example.ExampleBuilder.class)
public class Example {

    @Getter(onMethod = @__(@DynamoDbPartitionKey))
    private String field1;
    private String field2;

    @DynamoDbSecondaryPartitionKey(indexNames = { "field2-index" })
    public String getField2() {

        return field2;
    }
}

The previous work done by @bmaizels is very appreciated. I am adding this compile failure since it is related to the overall enhancement request of general DynamoDB/Lombok compatibility.

I also stumbled onto the issue with toBuilder support. Would be great if you could address it, as it does make a lot of sense when working with immutable classes. 🙏

The ‘onMethod’ is the current approved workaround for this (I had even put an example in the REAME when coding immutables) :

    @Value
    @Builder
    @DynamoDbImmutable(builder = Customer.CustomerBuilder.class)
    public static class Customer {
        @Getter(onMethod = @__({@DynamoDbPartitionKey}))
        private String accountId;

        @Getter(onMethod = @__({@DynamoDbSortKey}))
        private int subId;  
      
        @Getter(onMethod = @__({@DynamoDbSecondaryPartitionKey(indexNames = "customers_by_name")}))
        private String name;

        @Getter(onMethod = @__({@DynamoDbSecondarySortKey(indexNames = {"customers_by_date", "customers_by_name"})}))
        private Instant createdDate;
    }

Get your point about it being experimental (plus it’s ugly), so we’ll definitely keep an eye on this. The tricky part is there will undoubtedly also be customers that depend on the current behavior and have properties without getters and setters they would not want to suddenly start having show up in their TableSchema, and I don’t want to head down the slippery slope of adding lots of opt-in feature flags for the sake of backwards compatibility which really just leaves us with the option of creating a new introspector entirely… maybe we can figure out one that works both for Kotlin and more elegantly with Lombok at the same time.

Same for me, need to get over toBuilder

@bill-phast I’m not seeing how this is a major pain point, if I am understanding your problem statement correctly; please correct me if I have it wrong. Lombok will not overwrite an existing getter/setter if you define one, it will just ignore code generation for it.

I am currently using the enhanced DynamoDB client with this exact scenario. Example:

@DynamoDbBean
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ExampleModel {
    private String fieldOne;
    private String fieldTwo;

    @DynamoDbPartitionKey
    public String getFieldOne() {
        return fieldOne;
    }
}

With the Lombok feature mentioned by @ocind this is almost identical to applying the DDB annotation by itself:

@DynamoDbBean
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ExampleModel {
    @Getter(onMethod_={@DynamoDbPartitionKey})
    private String fieldOne;
    private String fieldTwo;
}

Lombok supports injecting Annotations to the generated Setter/Getter

Yes that could be helpful, but this feature copies annotations from the field to the setter/getter. Enhanced DynamoDB annotations cannot be put onto the field in the first place, so we still need changes before this can work.

As of SDK v2.17.121 (and still working in v2.17.226), the following test case works as expected:

import org.junit.jupiter.api.Test;

import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.jackson.Jacksonized;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbImmutable;

class DynamoEnhancedSchemaTest {

	@Jacksonized
	@Builder(toBuilder = true)
	@DynamoDbImmutable(builder = Foo.FooBuilder.class)
	@Getter
	@ToString
	public static class Foo {
		@Getter(onMethod_ = @DynamoDbPartitionKey)
		private final String s;
		@Getter(onMethod_ = @DynamoDbSortKey)
		private final int i;
		private final boolean b;
	}

	@Jacksonized
	@Builder(toBuilder = true)
	@DynamoDbBean
	@Data
	@ToString
	public static class Bar {
		@Getter(onMethod_ = @DynamoDbPartitionKey)
		private String s;
		@Getter(onMethod_ = @DynamoDbSortKey)
		private int i;
		private boolean b;

		public Bar() {
		}

		public Bar(String s, int i, boolean b) {
			this.s = s;
			this.i = i;
			this.b = b;
		}
	}

	@Test
	void testFoo() {
		TableSchema<Foo> schema = TableSchema.fromClass(Foo.class);
		System.out.println(schema.attributeNames());

		Foo foo = Foo.builder().s("s").i(0).b(true).build().toBuilder().s("ess").build();
		System.out.println(foo);
	}

	@Test
	void testBar() {
		TableSchema<Bar> schema = TableSchema.fromClass(Bar.class);
		System.out.println(schema.attributeNames());

		Bar bar = Bar.builder().s("s").i(0).b(true).build().toBuilder().s("ess").build();
		System.out.println(bar);
	}
}

Am I missing anything?

While upgrading from Java 16 to Java 17 I discovered a new issue related to Lombok and Enhanced DynamoDB. The following code reproduces the issue. …

@nadernader99 We’ve run into this same issue with java 11 and lombok 1.18.22 and aws sdk 2.17.86

Also waiting for this feature