made dynamic swagger config example

This commit is contained in:
Tony Tam 2015-02-16 20:17:00 -08:00
parent 5853326f6a
commit 678bbd6676
4 changed files with 83 additions and 32 deletions

View File

@ -6,6 +6,7 @@ import com.wordnik.swagger.models.properties.*;
import java.util.*;
public interface CodegenConfig {
CodegenType getTag();
String getName();
String getHelp();
Map<String, Object> additionalProperties();

View File

@ -25,20 +25,19 @@ import javax.servlet.ServletException;
public class Bootstrap extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
Info info = new Info()
.title("Swagger Generator")
.description("This is an online swagger codegen server. You can find out more " +
"at <a href=\"https://github.com/wordnik/swagger-generator\">https://github.com/wordnik/swagger-generator</a> or on irc.freenode.net, #swagger." +
"http://helloreverb.com/terms/")
.termsOfService("http://helloreverb.com/terms/")
.contact(new Contact()
.email("apiteam@swagger.io"))
.license(new License()
.name("Apache 2.0")
.url("http://www.apache.org/licenses/LICENSE-2.0.html"));
ServletContext context = config.getServletContext();
Swagger swagger = new Swagger().info(info);
context.setAttribute("swagger", swagger);
DynamicSwaggerConfig bc = new DynamicSwaggerConfig();
bc.setBasePath("/api");
bc.setTitle("Swagger Generator");
bc.setDescription("This is an online swagger codegen server. You can find out more " +
"at <a href=\"https://github.com/wordnik/swagger-generator\">https://github.com/wordnik/swagger-generator</a> or on irc.freenode.net, #swagger." +
"http://helloreverb.com/terms/");
bc.setTermsOfServiceUrl("http://helloreverb.com/terms/");
bc.setContact("apiteam@swagger.io");
bc.setLicense("Apache 2.0");
bc.setLicenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html");
bc.setResourcePackage("com.wordnik.swagger.generator.resource");
bc.setScan(true);
}
}

View File

@ -0,0 +1,69 @@
package com.wordnik.swagger.generator;
import com.wordnik.swagger.models.*;
import com.wordnik.swagger.models.parameters.*;
import com.wordnik.swagger.models.properties.*;
import com.wordnik.swagger.codegen.*;
import com.wordnik.swagger.util.Json;
import com.wordnik.swagger.jaxrs.config.BeanConfig;
import java.util.*;
public class DynamicSwaggerConfig extends BeanConfig {
static List<String> clients = new ArrayList<String>();
static List<String> servers = new ArrayList<String>();
static {
List<CodegenConfig> extensions = Codegen.getExtensions();
for(CodegenConfig config : extensions) {
if(config.getTag().equals(CodegenType.CLIENT) || config.getTag().equals(CodegenType.DOCUMENTATION)) {
clients.add(config.getName());
}
else if(config.getTag().equals(CodegenType.SERVER)) {
servers.add(config.getName());
}
}
}
@Override
public Swagger configure(Swagger swagger) {
Path clientPath = swagger.getPaths().get("/gen/clients/{language}");
// update the path description based on what clients are available via SPI
if(clientPath != null) {
Operation post = clientPath.getPost();
Parameter framework = post.getParameters().get(0);
if(framework instanceof PathParameter) {
PathParameter param = (PathParameter) framework;
StringBuilder b = new StringBuilder();
for(String client : clients) {
if(b.toString().length() > 0)
b.append(", ");
b.append(client);
}
param.setDescription("available clients: " + b.toString());
}
}
Path serverPath = swagger.getPaths().get("/gen/servers/{framework}");
// update the path description based on what servers are available via SPI
if(serverPath != null) {
Operation post = serverPath.getPost();
Parameter framework = post.getParameters().get(0);
if(framework instanceof PathParameter) {
PathParameter param = (PathParameter) framework;
StringBuilder b = new StringBuilder();
for(String server : servers) {
if(b.toString().length() > 0)
b.append(", ");
b.append(server);
}
param.setDescription("available clients: " + b.toString());
}
}
return swagger.info(getInfo())
.host(getHost())
.basePath("/api");
}
}

View File

@ -34,24 +34,6 @@
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Jersey2Config</servlet-name>
<servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://generator.wordnik.com/online/api</param-value>
</init-param>
<init-param>
<param-name>swagger.filter</param-name>
<param-value>com.wordnik.swagger.generator.util.ApiAuthorizationFilterImpl</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<filter>
<filter-name>ApiOriginFilter</filter-name>
<filter-class>com.wordnik.swagger.generator.util.ApiOriginFilter</filter-class>