Skip to content

Commit 99414aa

Browse files
committed
Omit annotations from the repository and domaintype in CrudMethodMetadata. (#1170)
Omit annotations from the repository and domaintype in CrudMethodMetadata as they may be different for the same method. The annotations from the repository and domaintype are obtained directly in CouchbaseRepositoryBase. Closes #1168. Co-authored-by: mikereiche <michael.reiche@couchbase.com>
1 parent 1581712 commit 99414aa

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

src/main/java/org/springframework/data/couchbase/repository/support/CrudMethodMetadataPostProcessor.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
145145
currentInvocation.set(invocation);
146146

147147
try {
148-
149148
CrudMethodMetadata metadata = (CrudMethodMetadata) TransactionSynchronizationManager.getResource(method);
150149

151150
if (metadata != null) {
@@ -156,7 +155,7 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
156155

157156
if (methodMetadata == null) {
158157

159-
methodMetadata = new DefaultCrudMethodMetadata(method, repositoryInformation);
158+
methodMetadata = new DefaultCrudMethodMetadata(method);
160159
CrudMethodMetadata tmp = metadataCache.putIfAbsent(method, methodMetadata);
161160

162161
if (tmp != null) {
@@ -187,7 +186,6 @@ private static class DefaultCrudMethodMetadata implements CrudMethodMetadata {
187186

188187
private final Method method;
189188
private final ScanConsistency scanConsistency;
190-
private final RepositoryInformation repositoryInformation;
191189
private final String scope;
192190
private final String collection;
193191

@@ -198,10 +196,9 @@ private static class DefaultCrudMethodMetadata implements CrudMethodMetadata {
198196
*
199197
* @param method must not be {@literal null}.
200198
*/
201-
DefaultCrudMethodMetadata(Method method, RepositoryInformation repositoryInformation) {
199+
DefaultCrudMethodMetadata(Method method) {
202200
Assert.notNull(method, "Method must not be null!");
203201
this.method = method;
204-
this.repositoryInformation = repositoryInformation;
205202
String n = method.getName();
206203
// internal methods
207204
if (n.equals("getEntityInformation") || n.equals("getOperations") || n.equals("withOptions")
@@ -212,8 +209,7 @@ private static class DefaultCrudMethodMetadata implements CrudMethodMetadata {
212209
return;
213210
}
214211

215-
AnnotatedElement[] annotated = new AnnotatedElement[] { method, method.getDeclaringClass(),
216-
repositoryInformation.getRepositoryInterface(), repositoryInformation.getDomainType() };
212+
AnnotatedElement[] annotated = new AnnotatedElement[] { method, method.getDeclaringClass()};
217213
this.scanConsistency = OptionsBuilder.annotation(ScanConsistency.class, "query", QueryScanConsistency.NOT_BOUNDED,
218214
annotated);
219215
this.scope = OptionsBuilder.annotationString(Scope.class, CollectionIdentifier.DEFAULT_SCOPE, annotated);

src/test/java/org/springframework/data/couchbase/domain/UserColRepository.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
3939
public interface UserColRepository extends CouchbaseRepository<UserCol, String>, DynamicProxyable<UserColRepository> {
4040

41-
<S extends UserCol> S save(S var1);
41+
// CouchbaseRepositoryQueryCollectionIntegrationTests.testScopeCollectionAnnotationSwap() relies on this
42+
// being commented out.
43+
//<S extends UserCol> S save(S var1);
4244

4345
List<UserCol> findByFirstname(String firstname);
4446

src/test/java/org/springframework/data/couchbase/repository/query/CouchbaseRepositoryQueryCollectionIntegrationTests.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.couchbase.repository.query;
1717

18+
import static com.couchbase.client.core.io.CollectionIdentifier.DEFAULT_SCOPE;
1819
import static org.junit.jupiter.api.Assertions.assertEquals;
1920
import static org.junit.jupiter.api.Assertions.assertThrows;
2021

@@ -178,7 +179,7 @@ public void testScopeCollectionAnnotation() {
178179
// scope
179180
List<UserCol> found = userColRepository.withCollection(otherCollection).findByFirstname(user.getFirstname());
180181
assertEquals(saved, found.get(0), "should have found what was saved");
181-
List<UserCol> notfound = userColRepository.withScope(CollectionIdentifier.DEFAULT_SCOPE)
182+
List<UserCol> notfound = userColRepository.withScope(DEFAULT_SCOPE)
182183
.withCollection(CollectionIdentifier.DEFAULT_COLLECTION).findByFirstname(user.getFirstname());
183184
assertEquals(0, notfound.size(), "should not have found what was saved");
184185
} finally {
@@ -188,6 +189,20 @@ public void testScopeCollectionAnnotation() {
188189
}
189190
}
190191

192+
@Test
193+
public void testScopeCollectionAnnotationSwap() {
194+
// UserCol annotation scope is other_scope, collection is other_collection
195+
// airportRepository relies on Config.setScopeName(scopeName) ("my_scope") from CollectionAwareIntegrationTests.
196+
// using airportRepository without specified a collection should fail.
197+
// This test ensures that airportRepository.save(airport) doesn't get the
198+
// collection from CrudMethodMetadata of UserCol.save()
199+
UserCol userCol = new UserCol("1", "Dave", "Wilson");
200+
Airport airport = new Airport("3", "myIata", "myIcao");
201+
UserCol savedCol = userColRepository.save(userCol); // uses UserCol annotation scope, populates CrudMethodMetadata
202+
userColRepository.delete(userCol); // uses UserCol annotation scope, populates CrudMethodMetadata
203+
assertThrows(IllegalStateException.class, () -> airportRepository.save(airport));
204+
}
205+
191206
// template default scope is my_scope
192207
// UserCol annotation scope is other_scope
193208
@Test
@@ -198,7 +213,7 @@ public void testScopeCollectionRepoWith() {
198213
List<UserCol> found = userColRepository.withScope(scopeName).withCollection(collectionName)
199214
.findByFirstname(user.getFirstname());
200215
assertEquals(saved, found.get(0), "should have found what was saved");
201-
List<UserCol> notfound = userColRepository.withScope(CollectionIdentifier.DEFAULT_SCOPE)
216+
List<UserCol> notfound = userColRepository.withScope(DEFAULT_SCOPE)
202217
.withCollection(CollectionIdentifier.DEFAULT_COLLECTION).findByFirstname(user.getFirstname());
203218
assertEquals(0, notfound.size(), "should not have found what was saved");
204219
userColRepository.withScope(scopeName).withCollection(collectionName).delete(user);

0 commit comments

Comments
 (0)