Skip to content

Commit

Permalink
Merge pull request #3284 from swagger-api/ticket-3283
Browse files Browse the repository at this point in the history
refs #3283 - add scanner limited to defined resourcePackages
  • Loading branch information
frantuma authored Sep 4, 2019
2 parents 4bbbc98 + 6103bf4 commit 3bfd7cc
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class JaxrsAnnotationScanner<T extends JaxrsAnnotationScanner<T>> impleme
protected OpenAPIConfiguration openApiConfiguration;
protected Application application;
protected static Logger LOGGER = LoggerFactory.getLogger(JaxrsAnnotationScanner.class);
protected boolean onlyConsiderResourcePackages = false;

public JaxrsAnnotationScanner application(Application application) {
this.application = application;
Expand Down Expand Up @@ -83,7 +84,9 @@ public Set<Class<?>> classes() {
}
}
} else {
allowAllPackages = true;
if (!onlyConsiderResourcePackages) {
allowAllPackages = true;
}
}
final Set<Class<?>> classes;
try (ScanResult scanResult = graph.scan()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.swagger.v3.jaxrs2.integration;

import java.util.HashSet;
import java.util.Set;

/**
* @since 2.0.10
*/
public class JaxrsApplicationAndResourcePackagesAnnotationScanner extends JaxrsAnnotationScanner<JaxrsApplicationAndResourcePackagesAnnotationScanner> {

public JaxrsApplicationAndResourcePackagesAnnotationScanner() {
onlyConsiderResourcePackages = true;
}

@Override
public Set<Class<?>> classes() {
Set<Class<?>> classes = super.classes();
Set<Class<?>> output = new HashSet<Class<?>>();
if (application != null) {
Set<Class<?>> clzs = application.getClasses();
if (clzs != null) {
for (Class<?> clz : clzs) {
if (!isIgnored(clz.getName())) {
output.add(clz);
}
}
}
Set<Object> singletons = application.getSingletons();
if (singletons != null) {
for (Object o : singletons) {
if (!isIgnored(o.getClass().getName())) {
output.add(o.getClass());
}
}
}
}
classes.addAll(output);
return classes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;

public class IntegrationTest {

Expand Down Expand Up @@ -52,4 +53,34 @@ public void shouldScanWithNewInitialization() throws Exception {
}
}

@Test
public void shouldScanOnlyResourcePackagesClasses() throws Exception {
SwaggerConfiguration config = new SwaggerConfiguration()
.openAPI(new OpenAPI().info(new Info().description("TEST INFO DESC")));
OpenApiContext ctx = new GenericOpenApiContext()
.openApiConfiguration(config)
.openApiReader(new Reader(config))
.openApiScanner(new JaxrsApplicationAndResourcePackagesAnnotationScanner().openApiConfiguration(config))
.init();

OpenAPI openApi = ctx.read();

assertNotNull(openApi);
assertNull(openApi.getPaths());

config = new SwaggerConfiguration()
.resourcePackages(Stream.of("com.my.project.resources", "org.my.project.resources").collect(Collectors.toSet()))
.openAPI(new OpenAPI().info(new Info().description("TEST INFO DESC")));

ctx = new GenericOpenApiContext()
.openApiConfiguration(config)
.openApiReader(new Reader(config))
.openApiScanner(new JaxrsApplicationAndResourcePackagesAnnotationScanner().openApiConfiguration(config))
.init();

openApi = ctx.read();
assertNotNull(openApi);
assertEquals(openApi.getPaths().keySet(), expectedKeys);
}

}

0 comments on commit 3bfd7cc

Please sign in to comment.