Skip to content

Commit

Permalink
integration servlet initializer and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Nov 2, 2017
1 parent c3a1ad4 commit 1eafcdc
Show file tree
Hide file tree
Showing 15 changed files with 345 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.swagger.v3.oas.integration.api.OpenAPIConfiguration;
import io.swagger.v3.oas.integration.api.OpenApiScanner;
import org.apache.commons.lang3.StringUtils;
import org.reflections.scanners.ResourcesScanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
Expand All @@ -17,6 +18,12 @@

public class GenericOpenApiScanner implements OpenApiScanner {

static final Set<String> ignored = new HashSet();

static {
ignored.addAll(IgnoredPackages.ignored);
}

private static Logger LOGGER = LoggerFactory.getLogger(GenericOpenApiScanner.class);

OpenAPIConfiguration openApiConfiguration;
Expand All @@ -39,7 +46,7 @@ public Set<Class<?>> classes() {
// if classes are passed, use them
if (openApiConfiguration.getResourceClasses() != null && !openApiConfiguration.getResourceClasses().isEmpty()) {
for (String className : openApiConfiguration.getResourceClasses()) {
if (!"".equals(className)) {
if (!isIgnored(className)) {
try {
output.add(Class.forName(className));
} catch (ClassNotFoundException e) {
Expand All @@ -53,7 +60,7 @@ public Set<Class<?>> classes() {

if (openApiConfiguration.getResourcePackages() != null && !openApiConfiguration.getResourcePackages().isEmpty()) {
for (String pkg : openApiConfiguration.getResourcePackages()) {
if (!"".equals(pkg)) {
if (!isIgnored(pkg)) {
acceptablePackages.add(pkg);
config.addUrls(ClasspathHelper.forPackage(pkg));
}
Expand Down Expand Up @@ -91,4 +98,12 @@ public Set<Class<?>> classes() {
public Map<String, Object> resources() {
return new HashMap<>();
}

protected boolean isIgnored(String classOrPackageName) {
if (StringUtils.isBlank(classOrPackageName)) {
return true;
}
return ignored.stream().anyMatch(i -> classOrPackageName.startsWith(i));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.swagger.v3.oas.integration;

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

public final class IgnoredPackages {

public static final Set<String> ignored = new HashSet();

static {
ignored.add("io.swagger.v3.jaxrs2.integration.resources");
ignored.add("org.glassfish.jersey");
ignored.add("org.jboss.resteasy");
ignored.add("com.sun.jersey");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public class SwaggerConfiguration implements OpenAPIConfiguration {
private String scannerClass;

private Boolean prettyPrint;
private Boolean readAllResources;

// read all operations also with no @Operation; set to false to read only methods annotated with @Operation
private Boolean readAllResources = Boolean.TRUE;

private Collection<String> ignoredRoutes;
private Long cacheTTL = -1L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface OpenApiScanner {
Set<Class<?>> classes();

Map<String, Object> resources();


}
153 changes: 153 additions & 0 deletions modules/swagger-jaxrs2-servlet-initializer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>swagger-project</artifactId>
<groupId>io.swagger.core.v3</groupId>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>swagger-jaxrs2-servlet-initializer</artifactId>
<packaging>bundle</packaging>
<name>swagger-jaxrs2-servlet-initializer</name>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<defaultGoal>install</defaultGoal>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>${felix-version}</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>
io.swagger.v3.jaxrs2.integration
</Export-Package>
<Import-Package>
javax.ws.rs*;version="2.0",
*
</Import-Package>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe-plugin-version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>a</stopKey>
<stopPort>9999</stopPort>
<useTestScope>true</useTestScope>
<webAppSourceDirectory>${project.basedir}/src/test/webapp</webAppSourceDirectory>
</configuration>
<dependencies>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>${swagger.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey2-version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<properties>
<swagger.version>2.0.0-SNAPSHOT</swagger.version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.swagger.v3.jaxrs2.integration;

import io.swagger.v3.oas.integration.IgnoredPackages;
import io.swagger.v3.oas.integration.OpenApiConfigurationException;
import io.swagger.v3.oas.integration.SwaggerConfiguration;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import javax.ws.rs.Path;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

@HandlesTypes({Path.class})
public class SwaggerServletInitializer implements ServletContainerInitializer {

static final Set<String> ignored = new HashSet();

static {
ignored.addAll(IgnoredPackages.ignored);
}

public SwaggerServletInitializer() {
}

public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
if (classes != null && classes.size() != 0) {
Set<Class<?>> resources = new LinkedHashSet();
classes.stream()
.filter(c -> ignored.stream().noneMatch(i -> c.getName().startsWith(i)))
.forEach(resources::add);
if (!resources.isEmpty()) {
// init context
try {
SwaggerConfiguration oasConfig = new SwaggerConfiguration()
.resourceClasses(resources.stream().map(c -> c.getName()).collect(Collectors.toSet()));

new JaxrsOpenApiContextBuilder()
.openApiConfiguration(oasConfig)
.buildContext(true);
} catch (OpenApiConfigurationException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.swagger.v3.jaxrs2.integration.SwaggerServletInitializer
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,24 @@ protected String resolveApplicationPath() {
}
// look for inner application, e.g. ResourceConfig
try {
Application innerApp = application;
Method m = application.getClass().getMethod("getApplication", null);
Application innerApplication = (Application) m.invoke(application, null);
applicationPath = innerApplication.getClass().getAnnotation(ApplicationPath.class);
if (applicationPath != null) {
if (StringUtils.isNotBlank(applicationPath.value())) {
return applicationPath.value();
while (m != null) {
Application retrievedApp = (Application) m.invoke(innerApp, null);
if (retrievedApp == null) {
break;
}
if (retrievedApp.getClass().equals(innerApp.getClass())) {
break;
}
innerApp = retrievedApp;
applicationPath = innerApp.getClass().getAnnotation(ApplicationPath.class);
if (applicationPath != null) {
if (StringUtils.isNotBlank(applicationPath.value())) {
return applicationPath.value();
}
}
m = innerApp.getClass().getMethod("getApplication", null);
}
} catch (NoSuchMethodException e) {
// no inner application found
Expand Down Expand Up @@ -816,6 +827,9 @@ protected boolean isOperationHidden(Method method) {
if (apiOperation != null && apiOperation.hidden()) {
return true;
}
if (!Boolean.TRUE.equals(config.isReadAllResources()) && apiOperation == null) {
return true;
}
return false;
}

Expand Down
Loading

0 comments on commit 1eafcdc

Please sign in to comment.