diff --git a/bin/java-inflector-petstore-server.sh b/bin/java-inflector-petstore-server.sh new file mode 100755 index 00000000000..5200ab26bb1 --- /dev/null +++ b/bin/java-inflector-petstore-server.sh @@ -0,0 +1,31 @@ +#!/bin/sh + +SCRIPT="$0" + +while [ -h "$SCRIPT" ] ; do + ls=`ls -ld "$SCRIPT"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + SCRIPT="$link" + else + SCRIPT=`dirname "$SCRIPT"`/"$link" + fi +done + +if [ ! -d "${APP_DIR}" ]; then + APP_DIR=`dirname "$SCRIPT"`/.. + APP_DIR=`cd "${APP_DIR}"; pwd` +fi + +executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar" + +if [ ! -f "$executable" ] +then + mvn clean package +fi + +# if you've executed sbt assembly previously it will use that instead. +export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" +ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaInflector -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l inflector -o samples/server/petstore/java-inflector" + +java $JAVA_OPTS -jar $executable $ags diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaInflectorServerCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaInflectorServerCodegen.java new file mode 100644 index 00000000000..168e214aef1 --- /dev/null +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaInflectorServerCodegen.java @@ -0,0 +1,188 @@ +package io.swagger.codegen.languages; + +import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.CodegenOperation; +import io.swagger.codegen.CodegenType; +import io.swagger.codegen.SupportingFile; +import io.swagger.models.Operation; +import io.swagger.models.Swagger; +import io.swagger.models.properties.ArrayProperty; +import io.swagger.models.properties.MapProperty; +import io.swagger.models.properties.Property; +import io.swagger.util.Json; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Map; + +public class JavaInflectorServerCodegen extends JavaClientCodegen implements CodegenConfig { + protected String invokerPackage = "io.swagger.handler"; + protected String groupId = "io.swagger"; + protected String artifactId = "swagger-inflector-server"; + protected String artifactVersion = "1.0.0"; + protected String title = "Swagger Inflector"; + + public JavaInflectorServerCodegen() { + super(); + + sourceFolder = "src/main/java"; + modelTemplateFiles.put("model.mustache", ".java"); + apiTemplateFiles.put("api.mustache", ".java"); + templateDir = "JavaInflector"; + + apiPackage = System.getProperty("swagger.codegen.inflector.apipackage", "io.swagger.handler"); + modelPackage = System.getProperty("swagger.codegen.inflector.modelpackage", "io.swagger.model"); + + additionalProperties.put("invokerPackage", invokerPackage); + additionalProperties.put("groupId", groupId); + additionalProperties.put("artifactId", artifactId); + additionalProperties.put("artifactVersion", artifactVersion); + additionalProperties.put("title", title); + + languageSpecificPrimitives = new HashSet( + Arrays.asList( + "String", + "boolean", + "Boolean", + "Double", + "Integer", + "Long", + "Float") + ); + } + + public CodegenType getTag() { + return CodegenType.SERVER; + } + + public String getName() { + return "inflector"; + } + + public String getHelp() { + return "Generates a Java Inflector Server application."; + } + + @Override + public void processOpts() { + super.processOpts(); + + supportingFiles.clear(); + supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); + supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); + supportingFiles.add(new SupportingFile("web.mustache", "src/main/webapp/WEB-INF", "web.xml")); + supportingFiles.add(new SupportingFile("inflector.mustache", "", "inflector.yaml")); + } + + @Override + public String getTypeDeclaration(Property p) { + if (p instanceof ArrayProperty) { + ArrayProperty ap = (ArrayProperty) p; + Property inner = ap.getItems(); + return getSwaggerType(p) + "<" + getTypeDeclaration(inner) + ">"; + } else if (p instanceof MapProperty) { + MapProperty mp = (MapProperty) p; + Property inner = mp.getAdditionalProperties(); + + return getTypeDeclaration(inner); + } + return super.getTypeDeclaration(p); + } + + @Override + public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map> operations) { + String basePath = resourcePath; + if (basePath.startsWith("/")) { + basePath = basePath.substring(1); + } + int pos = basePath.indexOf("/"); + if (pos > 0) { + basePath = basePath.substring(0, pos); + } + + if (basePath == "") { + basePath = "default"; + } else { + if (co.path.startsWith("/" + basePath)) { + co.path = co.path.substring(("/" + basePath).length()); + } + co.subresourceOperation = !co.path.isEmpty(); + } + List opList = operations.get(basePath); + if (opList == null) { + opList = new ArrayList(); + operations.put(basePath, opList); + } + opList.add(co); + co.baseName = basePath; + } + + public Map postProcessOperations(Map objs) { + Map operations = (Map) objs.get("operations"); + if (operations != null) { + List ops = (List) operations.get("operation"); + for (CodegenOperation operation : ops) { + if (operation.returnType == null) { + operation.returnType = "Void"; + } else if (operation.returnType.startsWith("List")) { + String rt = operation.returnType; + int end = rt.lastIndexOf(">"); + if (end > 0) { + operation.returnType = rt.substring("List<".length(), end); + operation.returnContainer = "List"; + } + } else if (operation.returnType.startsWith("Map")) { + String rt = operation.returnType; + int end = rt.lastIndexOf(">"); + if (end > 0) { + operation.returnType = rt.substring("Map<".length(), end); + operation.returnContainer = "Map"; + } + } else if (operation.returnType.startsWith("Set")) { + String rt = operation.returnType; + int end = rt.lastIndexOf(">"); + if (end > 0) { + operation.returnType = rt.substring("Set<".length(), end); + operation.returnContainer = "Set"; + } + } + } + } + return objs; + } + + @Override + public void processSwagger(Swagger swagger) { + super.processSwagger(swagger); + + try { + File file = new File( outputFolder + "/src/main/swagger/swagger.json" ); + file.getParentFile().mkdirs(); + + FileWriter swaggerFile = new FileWriter(file); + swaggerFile.write( Json.pretty( swagger )); + swaggerFile.flush(); + swaggerFile.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public String toApiName(String name) { + if (name.length() == 0) { + return "DefaultController"; + } + name = name.replaceAll("[^a-zA-Z0-9]+", "_"); + return camelize(name)+ "Controller"; + } + + public boolean shouldOverwrite(String filename) { + return super.shouldOverwrite(filename); + } +} diff --git a/modules/swagger-codegen/src/main/resources/JavaInflector/README.mustache b/modules/swagger-codegen/src/main/resources/JavaInflector/README.mustache new file mode 100644 index 00000000000..c1309670c5a --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaInflector/README.mustache @@ -0,0 +1,8 @@ +# Swagger Inflector + +Run with + +``` +mvn package jetty:run +`` + diff --git a/modules/swagger-codegen/src/main/resources/JavaInflector/api.mustache b/modules/swagger-codegen/src/main/resources/JavaInflector/api.mustache new file mode 100644 index 00000000000..5f5b5b1df9a --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaInflector/api.mustache @@ -0,0 +1,27 @@ +package {{invokerPackage}}; + +import io.swagger.inflector.models.RequestContext; +import io.swagger.inflector.models.ResponseContext; +import javax.ws.rs.core.Response.Status; + +import org.glassfish.jersey.media.multipart.FormDataContentDisposition; +import java.io.File; +import java.util.List; + +import {{modelPackage}}.*; + +{{#imports}}import {{import}}; +{{/imports}} + +{{#operations}} +public class {{classname}} { + +{{#operation}} + public ResponseContext {{nickname}}(RequestContext request {{#allParams}},{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{/allParams}}) + { + return new ResponseContext().status(Status.INTERNAL_SERVER_ERROR).entity( "Not implemented" ); + } +{{/operation}} +} +{{/operations}} + diff --git a/modules/swagger-codegen/src/main/resources/JavaInflector/bodyParams.mustache b/modules/swagger-codegen/src/main/resources/JavaInflector/bodyParams.mustache new file mode 100644 index 00000000000..c7d1abfe527 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaInflector/bodyParams.mustache @@ -0,0 +1 @@ +{{#isBodyParam}}{{{dataType}}} {{paramName}}{{/isBodyParam}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaInflector/formParams.mustache b/modules/swagger-codegen/src/main/resources/JavaInflector/formParams.mustache new file mode 100644 index 00000000000..e44ab167e8f --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaInflector/formParams.mustache @@ -0,0 +1 @@ +{{#isFormParam}}{{#notFile}}{{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}}FormDataContentDisposition fileDetail{{/isFile}}{{/isFormParam}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaInflector/headerParams.mustache b/modules/swagger-codegen/src/main/resources/JavaInflector/headerParams.mustache new file mode 100644 index 00000000000..bd03573d196 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaInflector/headerParams.mustache @@ -0,0 +1 @@ +{{#isHeaderParam}}{{{dataType}}} {{paramName}}{{/isHeaderParam}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaInflector/inflector.mustache b/modules/swagger-codegen/src/main/resources/JavaInflector/inflector.mustache new file mode 100644 index 00000000000..d9d02ea93a8 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaInflector/inflector.mustache @@ -0,0 +1,6 @@ +controllerPackage: {{invokerPackage}} +modelPackage: {{modelPackage}} +swaggerUrl: ./src/main/swagger/swagger.json +modelMappings: + {{#models}}{{#model}}{{classname}} : {{modelPackage}}.{{classname}}{{/model}} + {{/models}} diff --git a/modules/swagger-codegen/src/main/resources/JavaInflector/model.mustache b/modules/swagger-codegen/src/main/resources/JavaInflector/model.mustache new file mode 100644 index 00000000000..300d5e61dd9 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaInflector/model.mustache @@ -0,0 +1,51 @@ +package {{package}}; + +{{#imports}}import {{import}}; +{{/imports}} + +import io.swagger.annotations.*; +import com.fasterxml.jackson.annotation.JsonProperty; +{{#models}} + +{{#model}}{{#description}} +/** + * {{description}} + **/{{/description}} +@ApiModel(description = "{{{description}}}") +public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} { + {{#vars}}{{#isEnum}} + public enum {{datatypeWithEnum}} { + {{#allowableValues}}{{#values}} {{.}}, {{/values}}{{/allowableValues}} + }; + private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};{{/isEnum}}{{^isEnum}} + private {{{datatype}}} {{name}} = {{{defaultValue}}};{{/isEnum}}{{/vars}} + + {{#vars}} + /**{{#description}} + * {{{description}}}{{/description}}{{#minimum}} + * minimum: {{minimum}}{{/minimum}}{{#maximum}} + * maximum: {{maximum}}{{/maximum}} + **/ + @ApiModelProperty({{#required}}required = {{required}}, {{/required}}value = "{{{description}}}") + @JsonProperty("{{name}}") + public {{{datatypeWithEnum}}} {{getter}}() { + return {{name}}; + } + public void {{setter}}({{{datatypeWithEnum}}} {{name}}) { + this.{{name}} = {{name}}; + } + + {{/vars}} + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class {{classname}} {\n"); + {{#parent}}sb.append(" " + super.toString()).append("\n");{{/parent}} + {{#vars}}sb.append(" {{name}}: ").append({{name}}).append("\n"); + {{/vars}}sb.append("}\n"); + return sb.toString(); + } +} +{{/model}} +{{/models}} diff --git a/modules/swagger-codegen/src/main/resources/JavaInflector/pathParams.mustache b/modules/swagger-codegen/src/main/resources/JavaInflector/pathParams.mustache new file mode 100644 index 00000000000..6829cf8c7a6 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaInflector/pathParams.mustache @@ -0,0 +1 @@ +{{#isPathParam}}{{{dataType}}} {{paramName}}{{/isPathParam}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaInflector/pom.mustache b/modules/swagger-codegen/src/main/resources/JavaInflector/pom.mustache new file mode 100644 index 00000000000..09477f95052 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaInflector/pom.mustache @@ -0,0 +1,174 @@ + + + org.sonatype.oss + oss-parent + 5 + + 4.0.0 + {{groupId}} + {{artifactId}} + jar + {{artifactId}} + {{artifactVersion}} + + 2.2.0 + + + + install + target + ${project.artifactId}-${project.version} + + + maven-dependency-plugin + + + package + + copy-dependencies + + + ${project.build.directory}/lib + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + + **/logback.xml + + + + development + ${project.url} + ${project.version} + io.swagger + + + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty-version} + + . + + inflector.yaml + src/main/swagger/swagger.yaml + + 1 + + / + + + 8080 + 60000 + + + + + + + + + com.fasterxml.jackson.core + jackson-core + ${jackson-version} + + + com.fasterxml.jackson.core + jackson-annotations + 2.4.0 + + + com.fasterxml.jackson.core + jackson-databind + ${jackson-version} + + + com.fasterxml.jackson.datatype + jackson-datatype-joda + ${jackson-version} + + + + org.glassfish.jersey.containers + jersey-container-servlet-core + ${jersey2-version} + + + org.glassfish.jersey.media + jersey-media-multipart + ${jersey2-version} + + + org.glassfish.jersey.core + jersey-client + ${jersey2-version} + + + javax.servlet + servlet-api + ${servlet-api-version} + provided + + + + ch.qos.logback + logback-classic + ${logback-version} + + + ch.qos.logback + logback-core + ${logback-version} + + + org.slf4j + slf4j-ext + ${slf4j-version} + + + org.slf4j + slf4j-api + ${slf4j-version} + + + + + commons-lang + commons-lang + ${commons-lang-version} + + + + + io.swagger + swagger-inflector + 1.0.0-SNAPSHOT + + + + + 1.0.0 + 1.5.0 + 1.0.8 + 2.4.2 + 2.2 + 1.2 + 9.2.9.v20150224 + 2.6 + 2.5 + 2.4 + 2.4 + 1.1 + 1.0.1 + 4.8.2 + 1.6.3 + + \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaInflector/queryParams.mustache b/modules/swagger-codegen/src/main/resources/JavaInflector/queryParams.mustache new file mode 100644 index 00000000000..ff79730471d --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaInflector/queryParams.mustache @@ -0,0 +1 @@ +{{#isQueryParam}}{{{dataType}}} {{paramName}}{{/isQueryParam}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaInflector/web.mustache b/modules/swagger-codegen/src/main/resources/JavaInflector/web.mustache new file mode 100644 index 00000000000..34a2eea6bcf --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaInflector/web.mustache @@ -0,0 +1,24 @@ + + + + swagger-inflector + org.glassfish.jersey.servlet.ServletContainer + + javax.ws.rs.Application + io.swagger.inflector.SwaggerInflector + + 1 + + + swagger-inflector + /* + + + CORSFilter + io.swagger.inflector.utils.CORSFilter + + + CORSFilter + /* + + \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig b/modules/swagger-codegen/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig index 29c7ed0f729..dbc1422497b 100644 --- a/modules/swagger-codegen/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig +++ b/modules/swagger-codegen/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig @@ -3,6 +3,7 @@ io.swagger.codegen.languages.AsyncScalaClientCodegen io.swagger.codegen.languages.CSharpClientCodegen io.swagger.codegen.languages.JavaClientCodegen io.swagger.codegen.languages.JaxRSServerCodegen +io.swagger.codegen.languages.JavaInflectorServerCodegen io.swagger.codegen.languages.NodeJSServerCodegen io.swagger.codegen.languages.ObjcClientCodegen io.swagger.codegen.languages.PerlClientCodegen