diff --git a/src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBEntityMetadataSupport.java b/src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBEntityMetadataSupport.java index ca08d26e..9bca270f 100644 --- a/src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBEntityMetadataSupport.java +++ b/src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBEntityMetadataSupport.java @@ -286,11 +286,23 @@ public String getOverriddenAttributeName(final String propertyName) { @Override public DynamoDBMarshaller getMarshallerForProperty(final String propertyName) { + DynamoDBMarshalling annotation = null; Method method = findMethod(propertyName); - if (method != null && method.getAnnotation(DynamoDBMarshalling.class) != null) { + if (method != null) { + annotation = method.getAnnotation(DynamoDBMarshalling.class); + } + + if(annotation == null) { + Field field = findField(propertyName); + if(field != null) { + annotation = field.getAnnotation(DynamoDBMarshalling.class); + } + } + + if(annotation != null) { try { - return method.getAnnotation(DynamoDBMarshalling.class).marshallerClass().newInstance(); + return annotation.marshallerClass().newInstance(); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { diff --git a/src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/User.java b/src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/User.java index 76a7a89e..a75edf37 100644 --- a/src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/User.java +++ b/src/test/java/org/socialsignin/spring/data/dynamodb/domain/sample/User.java @@ -35,7 +35,8 @@ public class User { private Integer numberOfPlaylists; private Date joinDate; - + + @DynamoDBMarshalling(marshallerClass=DynamoDBYearMarshaller.class) private Date joinYear; private Instant leaveDate; @@ -61,7 +62,6 @@ public void setJoinDate(Date joinDate) { this.joinDate = joinDate; } - @DynamoDBMarshalling(marshallerClass=DynamoDBYearMarshaller.class) public Date getJoinYear() { return joinYear; } diff --git a/src/test/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBEntityMetadataSupportUnitTest.java b/src/test/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBEntityMetadataSupportUnitTest.java new file mode 100644 index 00000000..493d772a --- /dev/null +++ b/src/test/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBEntityMetadataSupportUnitTest.java @@ -0,0 +1,34 @@ +package org.socialsignin.spring.data.dynamodb.repository.support; + +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMarshaller; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.socialsignin.spring.data.dynamodb.domain.sample.Playlist; +import org.socialsignin.spring.data.dynamodb.domain.sample.PlaylistId; +import org.socialsignin.spring.data.dynamodb.domain.sample.User; + +@RunWith(MockitoJUnitRunner.class) +@SuppressWarnings("unused") +public class DynamoDBEntityMetadataSupportUnitTest { + + @Test + public void testGetMarshallerForProperty_WhenAnnotationIsOnField_AndReturnsDynamoDBMarshaller() + { + DynamoDBEntityMetadataSupport support = new DynamoDBEntityMetadataSupport(User.class); + DynamoDBMarshaller fieldAnnotation = support.getMarshallerForProperty("joinYear"); + Assert.assertNotNull(fieldAnnotation); + } + + @Test + public void testGetMarshallerForProperty_WhenAnnotationIsOnMethod_AndReturnsDynamoDBMarshaller() + { + DynamoDBEntityMetadataSupport support = new DynamoDBEntityMetadataSupport(User.class); + DynamoDBMarshaller methodAnnotation = support.getMarshallerForProperty("leaveDate"); + Assert.assertNotNull(methodAnnotation); + } +}