DATAMONGO-1373 - Allow usage of @AliasFor for composed @Document annotation.
We now resolve aliased attribute values when reading @Document on entity types. This allows creation of composed annotations like:
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
@Document
static @interface ComposedDocumentAnnotation {
@AliasFor(annotation = Document.class, attribute = "collection")
String name() default "custom-collection-name";
}
Original pull request: #347.
Related issue: DATACMNS-825.
This commit is contained in:
committed by
Oliver Gierke
parent
9a078b743f
commit
0b634f8340
@@ -26,7 +26,6 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.expression.BeanFactoryAccessor;
|
||||
import org.springframework.context.expression.BeanFactoryResolver;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mapping.Association;
|
||||
import org.springframework.data.mapping.AssociationHandler;
|
||||
@@ -78,7 +77,7 @@ public class BasicMongoPersistentEntity<T> extends BasicPersistentEntity<T, Mong
|
||||
Class<?> rawType = typeInformation.getType();
|
||||
String fallback = MongoCollectionUtils.getPreferredCollectionName(rawType);
|
||||
|
||||
Document document = AnnotationUtils.findAnnotation(rawType, Document.class);
|
||||
Document document = this.findAnnotation(Document.class);
|
||||
|
||||
this.expression = detectExpression(document);
|
||||
this.context = new StandardEvaluationContext();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2014 by the original author(s).
|
||||
* Copyright 2011-2016 by the original author(s).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -29,6 +29,7 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
|
||||
@@ -243,6 +244,18 @@ public class BasicMongoPersistentEntityUnitTests {
|
||||
assertThat(entity.getCollection(), is("collection-1"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-1373
|
||||
*/
|
||||
@Test
|
||||
public void metaInformationShouldBeReadCorrectlyFromComposedDocumentAnnotation() {
|
||||
|
||||
BasicMongoPersistentEntity<DocumentWithComposedAnnotation> entity = new BasicMongoPersistentEntity<DocumentWithComposedAnnotation>(
|
||||
ClassTypeInformation.from(DocumentWithComposedAnnotation.class));
|
||||
|
||||
assertThat(entity.getCollection(), is("custom-collection"));
|
||||
}
|
||||
|
||||
@Document(collection = "contacts")
|
||||
class Contact {
|
||||
|
||||
@@ -284,9 +297,23 @@ public class BasicMongoPersistentEntityUnitTests {
|
||||
|
||||
}
|
||||
|
||||
@ComposedDocumentAnnotation
|
||||
static class DocumentWithComposedAnnotation {
|
||||
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.TYPE })
|
||||
@Document(collection = "collection-1")
|
||||
static @interface CustomDocumentAnnotation {
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.TYPE })
|
||||
@Document
|
||||
static @interface ComposedDocumentAnnotation {
|
||||
|
||||
@AliasFor(annotation = Document.class, attribute = "collection")
|
||||
String name() default "custom-collection";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user