Skip to content

Omit annotations from the repository and domaintype in CrudMethodMetadata. #1170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
currentInvocation.set(invocation);

try {

CrudMethodMetadata metadata = (CrudMethodMetadata) TransactionSynchronizationManager.getResource(method);

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

if (methodMetadata == null) {

methodMetadata = new DefaultCrudMethodMetadata(method, repositoryInformation);
methodMetadata = new DefaultCrudMethodMetadata(method);
CrudMethodMetadata tmp = metadataCache.putIfAbsent(method, methodMetadata);

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

private final Method method;
private final ScanConsistency scanConsistency;
private final RepositoryInformation repositoryInformation;
private final String scope;
private final String collection;

Expand All @@ -198,10 +196,9 @@ private static class DefaultCrudMethodMetadata implements CrudMethodMetadata {
*
* @param method must not be {@literal null}.
*/
DefaultCrudMethodMetadata(Method method, RepositoryInformation repositoryInformation) {
DefaultCrudMethodMetadata(Method method) {
Assert.notNull(method, "Method must not be null!");
this.method = method;
this.repositoryInformation = repositoryInformation;
String n = method.getName();
// internal methods
if (n.equals("getEntityInformation") || n.equals("getOperations") || n.equals("withOptions")
Expand All @@ -212,8 +209,7 @@ private static class DefaultCrudMethodMetadata implements CrudMethodMetadata {
return;
}

AnnotatedElement[] annotated = new AnnotatedElement[] { method, method.getDeclaringClass(),
repositoryInformation.getRepositoryInterface(), repositoryInformation.getDomainType() };
AnnotatedElement[] annotated = new AnnotatedElement[] { method, method.getDeclaringClass()};
this.scanConsistency = OptionsBuilder.annotation(ScanConsistency.class, "query", QueryScanConsistency.NOT_BOUNDED,
annotated);
this.scope = OptionsBuilder.annotationString(Scope.class, CollectionIdentifier.DEFAULT_SCOPE, annotated);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
public interface UserColRepository extends CouchbaseRepository<UserCol, String>, DynamicProxyable<UserColRepository> {

<S extends UserCol> S save(S var1);
// CouchbaseRepositoryQueryCollectionIntegrationTests.testScopeCollectionAnnotationSwap() relies on this
// being commented out.
//<S extends UserCol> S save(S var1);

List<UserCol> findByFirstname(String firstname);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.couchbase.repository.query;

import static com.couchbase.client.core.io.CollectionIdentifier.DEFAULT_SCOPE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down Expand Up @@ -178,7 +179,7 @@ public void testScopeCollectionAnnotation() {
// scope
List<UserCol> found = userColRepository.withCollection(otherCollection).findByFirstname(user.getFirstname());
assertEquals(saved, found.get(0), "should have found what was saved");
List<UserCol> notfound = userColRepository.withScope(CollectionIdentifier.DEFAULT_SCOPE)
List<UserCol> notfound = userColRepository.withScope(DEFAULT_SCOPE)
.withCollection(CollectionIdentifier.DEFAULT_COLLECTION).findByFirstname(user.getFirstname());
assertEquals(0, notfound.size(), "should not have found what was saved");
} finally {
Expand All @@ -188,6 +189,20 @@ public void testScopeCollectionAnnotation() {
}
}

@Test
public void testScopeCollectionAnnotationSwap() {
// UserCol annotation scope is other_scope, collection is other_collection
// airportRepository relies on Config.setScopeName(scopeName) ("my_scope") from CollectionAwareIntegrationTests.
// using airportRepository without specified a collection should fail.
// This test ensures that airportRepository.save(airport) doesn't get the
// collection from CrudMethodMetadata of UserCol.save()
UserCol userCol = new UserCol("1", "Dave", "Wilson");
Airport airport = new Airport("3", "myIata", "myIcao");
UserCol savedCol = userColRepository.save(userCol); // uses UserCol annotation scope, populates CrudMethodMetadata
userColRepository.delete(userCol); // uses UserCol annotation scope, populates CrudMethodMetadata
assertThrows(IllegalStateException.class, () -> airportRepository.save(airport));
}

// template default scope is my_scope
// UserCol annotation scope is other_scope
@Test
Expand All @@ -198,7 +213,7 @@ public void testScopeCollectionRepoWith() {
List<UserCol> found = userColRepository.withScope(scopeName).withCollection(collectionName)
.findByFirstname(user.getFirstname());
assertEquals(saved, found.get(0), "should have found what was saved");
List<UserCol> notfound = userColRepository.withScope(CollectionIdentifier.DEFAULT_SCOPE)
List<UserCol> notfound = userColRepository.withScope(DEFAULT_SCOPE)
.withCollection(CollectionIdentifier.DEFAULT_COLLECTION).findByFirstname(user.getFirstname());
assertEquals(0, notfound.size(), "should not have found what was saved");
userColRepository.withScope(scopeName).withCollection(collectionName).delete(user);
Expand Down