DATACOUCH-226 - Enable strict @Field mapping
If the MappingCouchbaseConverter strict mode is enabled and no annotation is present, the property will not be eligible for document mapping. This allows to specifically ignore some attributes in Couchbase only. For generally ignoring attributes in any Spring Data backing store, prefer the standard `@Transient`. Closes #112
This commit is contained in:
committed by
Simon Baslé
parent
811c024041
commit
7a57a235d8
@@ -33,6 +33,7 @@ import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import com.couchbase.client.java.repository.annotation.Field;
|
||||
import org.joda.time.LocalDateTime;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -51,6 +52,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Michael Nitschinger
|
||||
* @author Geoffrey Mina
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = UnitTestApplicationConfig.class)
|
||||
@@ -501,6 +503,41 @@ public class MappingCouchbaseConverterTests {
|
||||
assertEquals("springId", converted.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStrictFieldCheckingIgnoresUnannotated() throws Exception{
|
||||
try {
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
AnnotatedEntity entity = new AnnotatedEntity();
|
||||
|
||||
converter.setEnableStrictFieldChecking(true);
|
||||
converter.write(entity,converted);
|
||||
|
||||
assertTrue(converted.getId() != null);
|
||||
assertTrue(converted.getPayload().containsKey("annotatedField"));
|
||||
assertFalse(converted.getPayload().containsKey("nonAnnotatedField"));
|
||||
} finally {
|
||||
converter.setEnableStrictFieldChecking(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLenientFieldCheckingStoresUnannotated() throws Exception{
|
||||
try {
|
||||
CouchbaseDocument converted = new CouchbaseDocument();
|
||||
AnnotatedEntity entity = new AnnotatedEntity();
|
||||
|
||||
converter.setEnableStrictFieldChecking(false);
|
||||
converter.write(entity,converted);
|
||||
|
||||
assertTrue(converted.getId() != null);
|
||||
assertTrue(converted.getPayload().containsKey("annotatedField"));
|
||||
assertTrue(converted.getPayload().containsKey("nonAnnotatedField"));
|
||||
} finally {
|
||||
converter.setEnableStrictFieldChecking(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static class EntityWithoutID {
|
||||
private String attr0;
|
||||
@@ -648,6 +685,20 @@ public class MappingCouchbaseConverterTests {
|
||||
}
|
||||
}
|
||||
|
||||
static class AnnotatedEntity{
|
||||
@Id private String uuid;
|
||||
@Field private String annotatedField;
|
||||
private String nonAnnotatedField;
|
||||
|
||||
public AnnotatedEntity(){
|
||||
this.uuid = java.util.UUID.randomUUID().toString();
|
||||
this.annotatedField = "Annotated Property";
|
||||
this.nonAnnotatedField = "Non Annotated Property";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@WritingConverter
|
||||
public static enum BigDecimalToStringConverter implements Converter<BigDecimal, String> {
|
||||
INSTANCE;
|
||||
|
||||
Reference in New Issue
Block a user