Skip to content

Commit

Permalink
Register indirect fragment introduced by super interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
quaff committed Nov 25, 2024
1 parent 4718c45 commit c732728
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
package org.springframework.data.repository.config;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Stream;

import org.springframework.beans.factory.BeanDefinitionStoreException;
Expand All @@ -32,6 +33,7 @@
* @author Mark Paluch
* @author Oliver Gierke
* @author Johannes Englmeier
* @author Yanming Zhou
* @since 2.1
*/
public class FragmentMetadata {
Expand All @@ -52,10 +54,25 @@ public Stream<String> getFragmentInterfaces(String interfaceName) {

Assert.hasText(interfaceName, "Interface name must not be null or empty");

return Arrays.stream(getClassMetadata(interfaceName).getInterfaceNames()) //
return getAllSuperInterfaces(interfaceName).stream() //
.filter(this::isCandidate);
}

/**
* Returns all super interfaces of the given interface.
*
* @param interfaceName must not be {@literal null} or empty.
* @return
*/
private Set<String> getAllSuperInterfaces(String interfaceName) {
Set<String> interfaces = new LinkedHashSet<>();
for (String ifc : getClassMetadata(interfaceName).getInterfaceNames()) {
interfaces.add(ifc);
interfaces.addAll(getAllSuperInterfaces(ifc));
}
return interfaces;
}

/**
* Returns whether the given interface is a fragment candidate.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Yanming Zhou
*/
@ExtendWith(MockitoExtension.class)
class RepositoryBeanDefinitionRegistrarSupportUnitTests {
Expand Down Expand Up @@ -124,6 +125,18 @@ void shouldLimitImplementationBasePackages() {
assertBeanDefinitionRegisteredFor("spiContribution");
}


@Test // GH-3212
void shouldRegisterIndirectSpiFragment() {

AnnotationMetadata metadata = new StandardAnnotationMetadata(ExcludedWithSpiFragement.class, true);

registrar.registerBeanDefinitions(metadata, registry);

assertBeanDefinitionRegisteredFor("personRepository");
assertBeanDefinitionRegisteredFor("spiFragmentImplFragment");
}

@Test // DATACMNS-360
void registeredProfileRepositoriesIfProfileActivated() {

Expand Down Expand Up @@ -203,6 +216,9 @@ static class FragmentExclusionConfiguration {}
@EnableRepositories(basePackageClasses = FragmentImpl.class)
static class LimitsImplementationBasePackages {}

@EnableRepositories(basePackageClasses = org.springframework.data.repository.config.indirectspifragment.PersonRepository.class)
static class ExcludedWithSpiFragement {}

@EnableRepositories(basePackageClasses = MyNestedRepository.class, considerNestedRepositories = true,
excludeFilters = {
@Filter(type = FilterType.ASSIGNABLE_TYPE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2017-2024 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.repository.config.indirectspifragment;

import org.springframework.data.mapping.Person;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.config.spifragment.SpiFragment;

/**
* @author Yanming Zhou
*/
@NoRepositoryBean
public interface ExcludedRepository extends Repository<Person, String>, SpiFragment {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2017-2024 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.repository.config.indirectspifragment;

/**
* @author Yanming Zhou
*/
public interface PersonRepository extends ExcludedRepository {}

0 comments on commit c732728

Please sign in to comment.