Skip to content

Commit 0b634f8

Browse files
christophstroblodrotbohm
authored andcommitted
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: spring-projects#347. Related issue: DATACMNS-825.
1 parent 9a078b7 commit 0b634f8

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntity.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.context.ApplicationContextAware;
2727
import org.springframework.context.expression.BeanFactoryAccessor;
2828
import org.springframework.context.expression.BeanFactoryResolver;
29-
import org.springframework.core.annotation.AnnotationUtils;
3029
import org.springframework.data.annotation.Id;
3130
import org.springframework.data.mapping.Association;
3231
import org.springframework.data.mapping.AssociationHandler;
@@ -78,7 +77,7 @@ public BasicMongoPersistentEntity(TypeInformation<T> typeInformation) {
7877
Class<?> rawType = typeInformation.getType();
7978
String fallback = MongoCollectionUtils.getPreferredCollectionName(rawType);
8079

81-
Document document = AnnotationUtils.findAnnotation(rawType, Document.class);
80+
Document document = this.findAnnotation(Document.class);
8281

8382
this.expression = detectExpression(document);
8483
this.context = new StandardEvaluationContext();

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntityUnitTests.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 by the original author(s).
2+
* Copyright 2011-2016 by the original author(s).
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
import org.mockito.Mock;
3030
import org.mockito.runners.MockitoJUnitRunner;
3131
import org.springframework.context.ApplicationContext;
32+
import org.springframework.core.annotation.AliasFor;
3233
import org.springframework.data.mapping.model.MappingException;
3334
import org.springframework.data.util.ClassTypeInformation;
3435

@@ -243,6 +244,18 @@ public void metaInformationShouldBeReadCorrectlyFromInheritedDocumentAnnotation(
243244
assertThat(entity.getCollection(), is("collection-1"));
244245
}
245246

247+
/**
248+
* @see DATAMONGO-1373
249+
*/
250+
@Test
251+
public void metaInformationShouldBeReadCorrectlyFromComposedDocumentAnnotation() {
252+
253+
BasicMongoPersistentEntity<DocumentWithComposedAnnotation> entity = new BasicMongoPersistentEntity<DocumentWithComposedAnnotation>(
254+
ClassTypeInformation.from(DocumentWithComposedAnnotation.class));
255+
256+
assertThat(entity.getCollection(), is("custom-collection"));
257+
}
258+
246259
@Document(collection = "contacts")
247260
class Contact {
248261

@@ -284,9 +297,23 @@ static class DocumentWithCustomAnnotation {
284297

285298
}
286299

300+
@ComposedDocumentAnnotation
301+
static class DocumentWithComposedAnnotation {
302+
303+
}
304+
287305
@Retention(RetentionPolicy.RUNTIME)
288306
@Target({ ElementType.TYPE })
289307
@Document(collection = "collection-1")
290308
static @interface CustomDocumentAnnotation {
291309
}
310+
311+
@Retention(RetentionPolicy.RUNTIME)
312+
@Target({ ElementType.TYPE })
313+
@Document
314+
static @interface ComposedDocumentAnnotation {
315+
316+
@AliasFor(annotation = Document.class, attribute = "collection")
317+
String name() default "custom-collection";
318+
}
292319
}

0 commit comments

Comments
 (0)