Skip to content

Commit d3967f2

Browse files
committed
Polishing.
Filter Enable…Repositories(fragmentsContributor) attribute values that point to an interface to enable defaulting in Enable…Repositories annotations. See #3279
1 parent 3a61461 commit d3967f2

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/main/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSource.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Optional;
2323
import java.util.Set;
2424
import java.util.function.Function;
25+
import java.util.function.Predicate;
2526
import java.util.stream.Stream;
2627

2728
import org.jspecify.annotations.NonNull;
@@ -195,7 +196,11 @@ public Optional<String> getRepositoryFragmentsContributorClassName() {
195196
return Optional.empty();
196197
}
197198

198-
return Optional.of(attributes.getClass(REPOSITORY_FRAGMENTS_CONTRIBUTOR_CLASS).getName());
199+
Class<?> fragmentsContributorClass = attributes.getClass(REPOSITORY_FRAGMENTS_CONTRIBUTOR_CLASS);
200+
201+
return Optional.of(fragmentsContributorClass) //
202+
.filter(Predicate.not(Class::isInterface)) // avoid default values that are typically interfaces
203+
.map(Class::getName);
199204
}
200205

201206
/**

src/test/java/org/springframework/data/repository/config/AnnotationRepositoryConfigurationSourceUnitTests.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.data.repository.config.basepackage.repo.PersonRepository;
3939
import org.springframework.data.repository.core.support.DummyReactiveRepositoryFactory;
4040
import org.springframework.data.repository.core.support.DummyRepositoryFactory;
41+
import org.springframework.data.repository.core.support.RepositoryFragmentsContributor;
4142
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
4243

4344
/**
@@ -196,6 +197,19 @@ void considersDefaultFragmentsContributor() {
196197
.contains(SampleRepositoryFragmentsContributor.class.getName());
197198
}
198199

200+
@Test // GH-3279
201+
void skipsInterfaceFragmentsContributor() {
202+
203+
RootBeanDefinition bd = new RootBeanDefinition(DummyRepositoryFactory.class);
204+
bd.getConstructorArgumentValues().addGenericArgumentValue(PersonRepository.class);
205+
206+
AnnotationMetadata metadata = AnnotationMetadata.introspect(ConfigurationWithFragmentsContributorInterface.class);
207+
AnnotationRepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(metadata,
208+
EnableRepositoriesWithContributor.class, resourceLoader, environment, registry, null);
209+
210+
assertThat(configurationSource.getRepositoryFragmentsContributorClassName()).isEmpty();
211+
}
212+
199213
@Test // GH-3279
200214
void omitsUnspecifiedFragmentsContributor() {
201215

@@ -216,8 +230,9 @@ void considerBeanNameGeneratorForReactiveRepos() {
216230
bd.getConstructorArgumentValues().addGenericArgumentValue(ReactivePersonRepository.class);
217231

218232
assertThat(getConfigSource(ConfigurationWithBeanNameGenerator.class).generateBeanName(bd))
219-
.isEqualTo(ReactivePersonRepository.class.getName());
220-
assertThat(getConfigSource(DefaultConfiguration.class).generateBeanName(bd)).isEqualTo("annotationRepositoryConfigurationSourceUnitTests.ReactivePersonRepository");
233+
.isEqualTo(ReactivePersonRepository.class.getName());
234+
assertThat(getConfigSource(DefaultConfiguration.class).generateBeanName(bd))
235+
.isEqualTo("annotationRepositoryConfigurationSourceUnitTests.ReactivePersonRepository");
221236
}
222237

223238
private AnnotationRepositoryConfigurationSource getConfigSource(Class<?> type) {
@@ -247,6 +262,9 @@ static class ConfigurationWithBeanNameGenerator {}
247262
@EnableRepositoriesWithContributor()
248263
static class ConfigurationWithFragmentsContributor {}
249264

265+
@EnableRepositoriesWithContributor(fragmentsContributor = RepositoryFragmentsContributor.class)
266+
static class ConfigurationWithFragmentsContributorInterface {}
267+
250268
@EnableReactiveRepositories(nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class)
251269
static class ReactiveConfigurationWithBeanNameGenerator {}
252270

@@ -256,6 +274,7 @@ static class ReactiveConfigurationWithBeanNameGenerator {}
256274
Filter[] includeFilters() default {};
257275

258276
Filter[] excludeFilters() default {};
277+
259278
}
260279

261280
@SampleAnnotation

0 commit comments

Comments
 (0)