Skip to content

Resolve ReadPreference on actual repository interface. #4545

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4542-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4542-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4542-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4542-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,12 @@ static class CrudMethodMetadataPopulatingMethodInterceptor implements MethodInte

private final ConcurrentMap<Method, CrudMethodMetadata> metadataCache = new ConcurrentHashMap<>();
private final Set<Method> implementations = new HashSet<>();
private final RepositoryInformation repositoryInformation;

CrudMethodMetadataPopulatingMethodInterceptor(RepositoryInformation repositoryInformation) {

this.repositoryInformation = repositoryInformation;

ReflectionUtils.doWithMethods(repositoryInformation.getRepositoryInterface(), implementations::add,
method -> !repositoryInformation.isQueryMethod(method));
}
Expand Down Expand Up @@ -140,7 +143,7 @@ public Object invoke(MethodInvocation invocation) throws Throwable {

if (methodMetadata == null) {

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

if (tmp != null) {
Expand Down Expand Up @@ -171,23 +174,24 @@ static class DefaultCrudMethodMetadata implements CrudMethodMetadata {
/**
* Creates a new {@link DefaultCrudMethodMetadata} for the given {@link Method}.
*
* @param repositoryInterface the target repository interface.
* @param method must not be {@literal null}.
*/
DefaultCrudMethodMetadata(Method method) {
DefaultCrudMethodMetadata(Class<?> repositoryInterface, Method method) {

Assert.notNull(method, "Method must not be null");

this.readPreference = findReadPreference(method);
this.readPreference = findReadPreference(repositoryInterface, method);
}

private Optional<ReadPreference> findReadPreference(Method method) {
private Optional<ReadPreference> findReadPreference(Class<?> repositoryInterface, Method method) {

org.springframework.data.mongodb.repository.ReadPreference preference = AnnotatedElementUtils
.findMergedAnnotation(method, org.springframework.data.mongodb.repository.ReadPreference.class);

if (preference == null) {

preference = AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(),
preference = AnnotatedElementUtils.findMergedAnnotation(repositoryInterface,
org.springframework.data.mongodb.repository.ReadPreference.class);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.repository.support;

import static org.assertj.core.api.Assertions.*;

import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.springframework.data.mongodb.repository.Person;
import org.springframework.data.mongodb.repository.ReadPreference;
import org.springframework.data.mongodb.repository.support.CrudMethodMetadataPostProcessor.DefaultCrudMethodMetadata;
import org.springframework.data.repository.CrudRepository;
import org.springframework.util.ReflectionUtils;

/**
* @author Christoph Strobl
*/
class DefaultCrudMethodMetadataUnitTests {

@Test // GH-4542
void detectsReadPreferenceOnRepositoryInterface() {

DefaultCrudMethodMetadata metadata = new DefaultCrudMethodMetadata(ReadPreferenceAnnotated.class,
ReflectionUtils.findMethod(ReadPreferenceAnnotated.class, "findAll"));

assertThat(metadata.getReadPreference()).hasValue(com.mongodb.ReadPreference.primary());
}

@Test // GH-4542
void favorsReadPreferenceOfAnnotatedMethod() {

DefaultCrudMethodMetadata metadata = new DefaultCrudMethodMetadata(ReadPreferenceAnnotated.class,
ReflectionUtils.findMethod(ReadPreferenceAnnotated.class, "findById", Object.class));

assertThat(metadata.getReadPreference()).hasValue(com.mongodb.ReadPreference.secondary());
}

@ReadPreference("primary")
interface ReadPreferenceAnnotated extends CrudRepository<Person, String> {

@Override
@ReadPreference("secondary")
Optional<Person> findById(String s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void shouldAddReadPreferenceToFindAllMethods(Consumer<SimpleMongoRepository<Obje

repository = new SimpleMongoRepository<>(entityInformation, mongoOperations);
repository.setRepositoryMethodMetadata(
new DefaultCrudMethodMetadata(TestRepositoryWithReadPreference.class.getMethod("dummy")));
new DefaultCrudMethodMetadata(TestRepositoryWithReadPreference.class, TestRepositoryWithReadPreference.class.getMethod("dummy")));

findCall.accept(repository);

Expand All @@ -167,7 +167,7 @@ void shouldAddReadPreferenceToFindOne() throws NoSuchMethodException {

repository = new SimpleMongoRepository<>(entityInformation, mongoOperations);
repository.setRepositoryMethodMetadata(
new DefaultCrudMethodMetadata(TestRepositoryWithReadPreference.class.getMethod("dummy")));
new DefaultCrudMethodMetadata(TestRepositoryWithReadPreference.class, TestRepositoryWithReadPreference.class.getMethod("dummy")));

repository.findOne(Example.of(new TestDummy()));

Expand All @@ -188,7 +188,7 @@ void shouldAddReadPreferenceToFluentFetchable() throws NoSuchMethodException {

repository = new SimpleMongoRepository<>(entityInformation, mongoOperations);
repository.setRepositoryMethodMetadata(
new DefaultCrudMethodMetadata(TestRepositoryWithReadPreferenceMethod.class.getMethod("dummy")));
new DefaultCrudMethodMetadata(TestRepositoryWithReadPreferenceMethod.class, TestRepositoryWithReadPreferenceMethod.class.getMethod("dummy")));

repository.findBy(Example.of(new TestDummy()), FetchableFluentQuery::all);

Expand Down