Skip to content
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

allow multiple base packages, comma-separated #1154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -12,6 +12,7 @@
import org.springframework.util.StringUtils;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Optional;
import java.util.Set;

Expand All @@ -28,8 +29,8 @@ public class AnnotationClassScanner<T extends Annotation> implements ClassScanne

@Override
public Set<Class<?>> scan() {
String basePackage = asyncApiDocketService.getAsyncApiDocket().getBasePackage();
if (!StringUtils.hasText(basePackage)) {
String basePackages = asyncApiDocketService.getAsyncApiDocket().getBasePackage();
if (!StringUtils.hasText(basePackages)) {
throw new IllegalArgumentException("Base package must not be blank");
}

Expand All @@ -38,12 +39,15 @@ public Set<Class<?>> scan() {

provider.addIncludeFilter(new AnnotationTypeFilter(annotation));

log.debug("Scanning for {} classes in {}", annotation.getSimpleName(), basePackage);
return provider.findCandidateComponents(basePackage).stream()
.map(BeanDefinition::getBeanClassName)
.map(this::getClass)
.filter(Optional::isPresent)
.map(Optional::get)
return Arrays.stream(basePackages.replaceAll("\\s", "").split(","))
.flatMap(basePackage -> {
log.debug("Scanning for {} classes in {}", annotation.getSimpleName(), basePackage);
return provider.findCandidateComponents(basePackage).stream()
.map(BeanDefinition::getBeanClassName)
.map(this::getClass)
.filter(Optional::isPresent)
.map(Optional::get);
})
.collect(toSet());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
public class AsyncApiDocket {

/**
* The base package containing the declarations of consumers and producer beans.
* The base package(s) containing the declarations of consumers and producer beans.
* Comma-separated for multiple base packages.
*/
private final String basePackage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public static class ConfigDocket {
public static final String DEFAULT_CONTENT_TYPE = "application/json";

/**
* The base package to scan for listeners which are declared inside a class annotated with @Component or @Service.
* The base package(s) to scan for listeners which are declared inside a class annotated with @Component or @Service.
* Comma-separated for multiple base packages.
*
* @see AsyncApiDocket.AsyncApiDocketBuilder#basePackage(String)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.github.springwolf.core.asyncapi.scanners.classes.TestComponent;
import io.github.springwolf.core.asyncapi.scanners.classes.TestConditionalComponent;
import io.github.springwolf.core.asyncapi.scanners.classes.TestOtherConditionalComponent;
import io.github.springwolf.core.asyncapi.scanners.classes2.TestComponent2;
import io.github.springwolf.core.configuration.docket.AsyncApiDocket;
import io.github.springwolf.core.configuration.docket.AsyncApiDocketService;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -49,7 +50,8 @@ void setUp() {
.title("ComponentClassScannerTest-title")
.version("ComponentClassScannerTest-version")
.build())
.basePackage("io.github.springwolf.core.asyncapi.scanners.classes")
.basePackage("io.github.springwolf.core.asyncapi.scanners.classes, "
+ "io.github.springwolf.core.asyncapi.scanners.classes2")
.build());
}

Expand All @@ -62,7 +64,7 @@ void getComponents() {

assertThat(components)
.doesNotContain(TestBeanConfiguration.TestBean.class)
.contains(TestComponent.class)
.contains(TestComponent.class, TestComponent2.class)
.doesNotContain(TestConditionalComponent.class)
.doesNotContain(TestOtherConditionalComponent.class);
}
Expand All @@ -76,7 +78,7 @@ void getComponentsIncludesConditional() {

assertThat(components)
.doesNotContain(TestBeanConfiguration.TestBean.class)
.contains(TestComponent.class)
.contains(TestComponent.class, TestComponent2.class)
.contains(TestConditionalComponent.class)
.doesNotContain(TestOtherConditionalComponent.class);
}
Expand Down
Loading