Skip to content

DATACMNS-1596 - Modules not exposing identifying annotations do not claim repository interfaces anymore. #411

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-commons</artifactId>
<version>2.3.0.BUILD-SNAPSHOT</version>
<version>2.3.0.DATACMNS-1596-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -53,7 +54,9 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit

private static final Logger LOGGER = LoggerFactory.getLogger(RepositoryConfigurationExtensionSupport.class);
private static final String CLASS_LOADING_ERROR = "%s - Could not load type %s using class loader %s.";
private static final String MULTI_STORE_DROPPED = "Spring Data {} - Could not safely identify store assignment for repository candidate {}.";
private static final String MULTI_STORE_DROPPED = "Spring Data %s - Could not safely identify store assignment for repository candidate %s. If you want this repository to be a %s repository,";

private boolean noMultiStoreSupport = false;

/*
* (non-Javadoc)
Expand Down Expand Up @@ -294,7 +297,22 @@ protected <T extends RepositoryConfigurationSource> RepositoryConfiguration<T> g
*/
protected boolean isStrictRepositoryCandidate(RepositoryMetadata metadata) {

if (noMultiStoreSupport) {
return false;
}

Collection<Class<?>> types = getIdentifyingTypes();
Collection<Class<? extends Annotation>> annotations = getIdentifyingAnnotations();
String moduleName = getModuleName();

if (types.isEmpty() && annotations.isEmpty()) {
if (!noMultiStoreSupport) {
LOGGER.warn("Spring Data {} does not support multi-store setups!", moduleName);
noMultiStoreSupport = true;
return false;
}
}

Class<?> repositoryInterface = metadata.getRepositoryInterface();

for (Class<?> type : types) {
Expand All @@ -304,19 +322,30 @@ protected boolean isStrictRepositoryCandidate(RepositoryMetadata metadata) {
}

Class<?> domainType = metadata.getDomainType();
Collection<Class<? extends Annotation>> annotations = getIdentifyingAnnotations();

if (annotations.isEmpty()) {
return true;
}

for (Class<? extends Annotation> annotationType : annotations) {
if (AnnotationUtils.findAnnotation(domainType, annotationType) != null) {
return true;
}
}

LOGGER.info(MULTI_STORE_DROPPED, getModuleName(), repositoryInterface);
String message = String.format(MULTI_STORE_DROPPED, moduleName, repositoryInterface, moduleName);

if (!annotations.isEmpty()) {
message = message.concat(" consider annotating your entities with one of these annotations: ") //
.concat(toString(annotations)) //
.concat(types.isEmpty() ? "." : " (preferred)");
}

if (!types.isEmpty()) {

message = message.concat(annotations.isEmpty() ? " consider" : ", or consider") //
.concat(" extending one of the following types with your repository: ") //
.concat(toString(types)) //
.concat(".");
}

LOGGER.info(message);

return false;
}
Expand Down Expand Up @@ -364,4 +393,11 @@ private Class<?> loadRepositoryInterface(RepositoryConfiguration<?> configuratio

return null;
}

private static String toString(Collection<? extends Class<?>> types) {

return types.stream() //
.map(Class::getName) //
.collect(Collectors.joining(", "));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.AbstractRepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;

Expand Down Expand Up @@ -86,6 +88,15 @@ public void rejectsReactiveRepositories() {
.hasMessageContaining("Reactive Repositories are not supported");
}

@Test // DATACMNS-1596
public void doesNotClaimEntityIfNoIdentifyingAnnotationsAreExposed() {

NonIdentifyingConfigurationExtension extension = new NonIdentifyingConfigurationExtension();
RepositoryMetadata metadata = AbstractRepositoryMetadata.getMetadata(AnnotatedTypeRepository.class);

assertThat(extension.isStrictRepositoryCandidate(metadata)).isFalse();
}

static class SampleRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {

@Override
Expand All @@ -109,6 +120,24 @@ protected Collection<Class<?>> getIdentifyingTypes() {
}
}

static class NonIdentifyingConfigurationExtension extends RepositoryConfigurationExtensionSupport {

@Override
protected String getModulePrefix() {
return "non-identifying";
}

@Override
public String getRepositoryFactoryBeanClassName() {
return RepositoryFactoryBeanSupport.class.getName();
}

@Override
protected Collection<Class<?>> getIdentifyingTypes() {
return Collections.singleton(CrudRepository.class);
}
}

@Primary
static class AnnotatedType {}

Expand Down