From 2bbe26b41a807df2eb7d150428a15ed4657fd28d Mon Sep 17 00:00:00 2001 From: Artur Dzmitryieu Date: Fri, 3 Jun 2016 15:52:46 -0400 Subject: [PATCH] Add new jax-rs language that uses only 2.0 spec API's --- .../languages/JavaJAXRSSpecServerCodegen.java | 145 ++++++++++++++++++ .../JavaJaxRS/spec/RestApplication.mustache | 9 ++ .../JavaJaxRS/spec/allowableValues.mustache | 1 + .../resources/JavaJaxRS/spec/api.mustache | 43 ++++++ .../JavaJaxRS/spec/bodyParams.mustache | 1 + .../JavaJaxRS/spec/enumClass.mustache | 16 ++ .../JavaJaxRS/spec/formParams.mustache | 2 + .../spec/generatedAnnotation.mustache | 1 + .../JavaJaxRS/spec/headerParams.mustache | 1 + .../resources/JavaJaxRS/spec/model.mustache | 14 ++ .../JavaJaxRS/spec/pathParams.mustache | 1 + .../resources/JavaJaxRS/spec/pojo.mustache | 74 +++++++++ .../resources/JavaJaxRS/spec/pom.mustache | 76 +++++++++ .../JavaJaxRS/spec/queryParams.mustache | 1 + .../services/io.swagger.codegen.CodegenConfig | 1 + 15 files changed, 386 insertions(+) create mode 100644 modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaJAXRSSpecServerCodegen.java create mode 100644 modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/RestApplication.mustache create mode 100644 modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/allowableValues.mustache create mode 100644 modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/api.mustache create mode 100644 modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/bodyParams.mustache create mode 100644 modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/enumClass.mustache create mode 100644 modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/formParams.mustache create mode 100644 modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/generatedAnnotation.mustache create mode 100644 modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/headerParams.mustache create mode 100644 modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/model.mustache create mode 100644 modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/pathParams.mustache create mode 100644 modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/pojo.mustache create mode 100644 modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/pom.mustache create mode 100644 modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/queryParams.mustache diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaJAXRSSpecServerCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaJAXRSSpecServerCodegen.java new file mode 100644 index 000000000000..667ff62958ab --- /dev/null +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaJAXRSSpecServerCodegen.java @@ -0,0 +1,145 @@ +package io.swagger.codegen.languages; + +import java.io.File; +import java.io.IOException; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import io.swagger.codegen.CliOption; +import io.swagger.codegen.CodegenConstants; +import io.swagger.codegen.CodegenModel; +import io.swagger.codegen.CodegenOperation; +import io.swagger.codegen.CodegenProperty; +import io.swagger.codegen.SupportingFile; +import io.swagger.models.Operation; +import io.swagger.models.Swagger; +import io.swagger.util.Json; +import org.apache.commons.io.FileUtils; +import com.fasterxml.jackson.core.JsonProcessingException; + +public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen +{ + public JavaJAXRSSpecServerCodegen() + { + super(); + supportsInheritance = true; + sourceFolder = "src/main/java"; + invokerPackage = "io.swagger.api"; + artifactId = "swagger-jaxrs-server"; + outputFolder = "generated-code/JavaJaxRS-Spec"; + + modelTemplateFiles.put("model.mustache", ".java"); + apiTemplateFiles.put("api.mustache", ".java"); + apiPackage = "io.swagger.api"; + modelPackage = "io.swagger.model"; + + additionalProperties.put("title", title); + + typeMapping.put("date", "LocalDate"); + typeMapping.put("DateTime", "javax.xml.datatype.XMLGregorianCalendar"); // Map DateTime fields to Java standart class 'XMLGregorianCalendar' + + importMapping.put("LocalDate", "org.joda.time.LocalDate"); + + super.embeddedTemplateDir = templateDir = JAXRS_TEMPLATE_DIRECTORY_NAME + File.separator + "spec"; + + for ( int i = 0; i < cliOptions.size(); i++ ) { + if ( CodegenConstants.LIBRARY.equals(cliOptions.get(i).getOpt()) ) { + cliOptions.remove(i); + break; + } + } + + CliOption library = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use"); + library.setDefault(DEFAULT_LIBRARY); + + Map supportedLibraries = new LinkedHashMap(); + + supportedLibraries.put(DEFAULT_LIBRARY, "JAXRS"); + library.setEnum(supportedLibraries); + + cliOptions.add(library); + cliOptions.add(new CliOption(CodegenConstants.IMPL_FOLDER, CodegenConstants.IMPL_FOLDER_DESC)); + cliOptions.add(new CliOption("title", "a title describing the application")); + } + + @Override + public void processOpts() + { + super.processOpts(); + + supportingFiles.clear(); // Don't need extra files provided by AbstractJAX-RS & Java Codegen + writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml")); + + writeOptional(outputFolder, new SupportingFile("RestApplication.mustache", + (sourceFolder + '/' + invokerPackage).replace(".", "/"), "RestApplication.java")); + + } + + @Override + public String getName() + { + return "jaxrs-spec"; + } + + @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; + } + + @Override + public void postProcessModelProperty(CodegenModel model, CodegenProperty property) { + super.postProcessModelProperty(model, property); + model.imports.remove("ApiModelProperty"); + model.imports.remove("ApiModel"); + model.imports.remove("JsonSerialize"); + model.imports.remove("ToStringSerializer"); + model.imports.remove("JsonValue"); + model.imports.remove("JsonProperty"); + } + + @Override + public void preprocessSwagger(Swagger swagger) { + //copy input swagger to output folder + try { + String swaggerJson = Json.pretty(swagger); + FileUtils.writeStringToFile(new File(outputFolder + File.separator + "swagger.json"), swaggerJson); + } catch (JsonProcessingException e) { + throw new RuntimeException(e.getMessage(), e.getCause()); + } catch (IOException e) { + throw new RuntimeException(e.getMessage(), e.getCause()); + } + super.preprocessSwagger(swagger); + + } + @Override + public String getHelp() + { + return "Generates a Java JAXRS Server according to JAXRS specification."; + } +} diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/RestApplication.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/RestApplication.mustache new file mode 100644 index 000000000000..b6ba3b369b1e --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/RestApplication.mustache @@ -0,0 +1,9 @@ +package {{invokerPackage}}; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@ApplicationPath("/") +public class RestApplication extends Application { + +} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/allowableValues.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/allowableValues.mustache new file mode 100644 index 000000000000..a48256d027a1 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/allowableValues.mustache @@ -0,0 +1 @@ +{{#allowableValues}}allowableValues="{{#values}}{{{.}}}{{^-last}}, {{/-last}}{{/values}}{{^values}}range=[{{#min}}{{.}}{{/min}}{{^min}}-infinity{{/min}}, {{#max}}{{.}}{{/max}}{{^max}}infinity{{/max}}]{{/values}}"{{/allowableValues}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/api.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/api.mustache new file mode 100644 index 000000000000..5ffd9c2a6d3b --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/api.mustache @@ -0,0 +1,43 @@ +package {{package}}; + +{{#imports}}import {{import}}; +{{/imports}} + +import javax.ws.rs.*; +import javax.ws.rs.core.Response; + +import io.swagger.annotations.*; + +import java.util.List; + +@Path("/{{baseName}}") + +@Api(description = "the {{baseName}} API") +{{#hasConsumes}}@Consumes({ {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}} +{{#hasProduces}}@Produces({ {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}} +{{>generatedAnnotation}} + +public class {{classname}} { +{{#operations}} +{{#operation}} + + @{{httpMethod}} + {{#subresourceOperation}}@Path("{{path}}"){{/subresourceOperation}} + {{#hasConsumes}}@Consumes({ {{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}} + {{#hasProduces}}@Produces({ {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}} + @ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}{{#hasAuthMethods}}, authorizations = { + {{#authMethods}}@Authorization(value = "{{name}}"{{#isOAuth}}, scopes = { + {{#scopes}}@AuthorizationScope(scope = "{{scope}}", description = "{{description}}"){{#hasMore}}, + {{/hasMore}}{{/scopes}} + }{{/isOAuth}}){{#hasMore}}, + {{/hasMore}}{{/authMethods}} + }{{/hasAuthMethods}}, tags={ {{#vendorExtensions.x-tags}}"{{tag}}"{{#hasMore}}, {{/hasMore}}{{/vendorExtensions.x-tags}} }) + @ApiResponses(value = { {{#responses}} + @ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}){{#hasMore}},{{/hasMore}}{{/responses}} }) + public Response {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}) { + return Response.ok().entity("magic!").build(); + } +{{/operation}} +} +{{/operations}} + diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/bodyParams.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/bodyParams.mustache new file mode 100644 index 000000000000..c7d1abfe527e --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/bodyParams.mustache @@ -0,0 +1 @@ +{{#isBodyParam}}{{{dataType}}} {{paramName}}{{/isBodyParam}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/enumClass.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/enumClass.mustache new file mode 100644 index 000000000000..1ef4a61efbed --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/enumClass.mustache @@ -0,0 +1,16 @@ +import javax.xml.bind.annotation.XmlEnum; +import javax.xml.bind.annotation.XmlType; + +@XmlType(name="{{classname}}") +@XmlEnum +public enum {{classname}} { + {{#allowableValues}}{{.}}{{^-last}}, {{/-last}}{{#-last}};{{/-last}}{{/allowableValues}} + + public String value() { + return name(); + } + + public static {{classname}} fromValue(String v) { + return valueOf(v); + } +} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/formParams.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/formParams.mustache new file mode 100644 index 000000000000..b1036ceeb54d --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/formParams.mustache @@ -0,0 +1,2 @@ +{{#isFormParam}}{{#notFile}}@FormParam(value = "{{paramName}}"{{^required}}, required = false{{/required}}) {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}} @FormParam(value = "{{paramName}}"{{^required}}, required = false{{/required}}) InputStream {{paramName}}InputStream, + @FormParam(value = "{{paramName}}" {{^required}}, required = false{{/required}}) Attachment {{paramName}}Detail{{/isFile}}{{/isFormParam}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/generatedAnnotation.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/generatedAnnotation.mustache new file mode 100644 index 000000000000..49110fc1ad93 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/generatedAnnotation.mustache @@ -0,0 +1 @@ +@javax.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}") \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/headerParams.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/headerParams.mustache new file mode 100644 index 000000000000..25d690c90ed8 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/headerParams.mustache @@ -0,0 +1 @@ +{{#isHeaderParam}}@HeaderParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/isHeaderParam}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/model.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/model.mustache new file mode 100644 index 000000000000..2c383a41a28e --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/model.mustache @@ -0,0 +1,14 @@ +package {{package}}; + +{{#imports}}import {{import}}; +{{/imports}} + +{{#models}} +{{#model}}{{#description}} +/** + * {{description}} + **/{{/description}} +{{#isEnum}}{{>enumClass}}{{/isEnum}} +{{^isEnum}}{{>pojo}}{{/isEnum}} +{{/model}} +{{/models}} diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/pathParams.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/pathParams.mustache new file mode 100644 index 000000000000..ba153467a65a --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/pathParams.mustache @@ -0,0 +1 @@ +{{#isPathParam}}@PathParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/isPathParam}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/pojo.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/pojo.mustache new file mode 100644 index 000000000000..f5657c2d33f2 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/pojo.mustache @@ -0,0 +1,74 @@ +import io.swagger.annotations.*; +import java.util.Objects; +{{#description}}@ApiModel(description = "{{{description}}}"){{/description}} + +public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} { + {{#vars}}{{#isEnum}} + +{{>enumClass}}{{/isEnum}}{{#items.isEnum}}{{#items}} + +{{>enumClass}}{{/items}}{{/items.isEnum}} + private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};{{/vars}} + + {{#vars}} + /**{{#description}} + * {{{description}}}{{/description}}{{#minimum}} + * minimum: {{minimum}}{{/minimum}}{{#maximum}} + * maximum: {{maximum}}{{/maximum}} + **/ + public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) { + this.{{name}} = {{name}}; + return this; + } + + {{#vendorExtensions.extraAnnotation}}{{vendorExtensions.extraAnnotation}}{{/vendorExtensions.extraAnnotation}} + @ApiModelProperty({{#example}}example = "{{example}}", {{/example}}{{#required}}required = {{required}}, {{/required}}value = "{{{description}}}") + public {{{datatypeWithEnum}}} {{getter}}() { + return {{name}}; + } + public void {{setter}}({{{datatypeWithEnum}}} {{name}}) { + this.{{name}} = {{name}}; + } + + {{/vars}} + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + {{classname}} {{classVarName}} = ({{classname}}) o;{{#hasVars}} + return {{#vars}}Objects.equals({{name}}, {{classVarName}}.{{name}}){{#hasMore}} && + {{/hasMore}}{{^hasMore}};{{/hasMore}}{{/vars}}{{/hasVars}}{{^hasVars}} + return true;{{/hasVars}} + } + + @Override + public int hashCode() { + return Objects.hash({{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}}); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class {{classname}} {\n"); + {{#parent}}sb.append(" ").append(toIndentedString(super.toString())).append("\n");{{/parent}} + {{#vars}}sb.append(" {{name}}: ").append(toIndentedString({{name}})).append("\n"); + {{/vars}}sb.append("}"); + return sb.toString(); + } + + /** + * Convert the given object to string with each line indented by 4 spaces + * (except the first line). + */ + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } +} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/pom.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/pom.mustache new file mode 100644 index 000000000000..e65d27f8db4f --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/pom.mustache @@ -0,0 +1,76 @@ + + 4.0.0 + {{groupId}} + {{artifactId}} + war + {{artifactId}} + {{artifactVersion}} + + src/main/java + + + org.apache.maven.plugins + maven-war-plugin + 2.6 + + false + + + + maven-failsafe-plugin + 2.6 + + + + integration-test + verify + + + + + + + + + javax.ws.rs + javax.ws.rs-api + 2.0 + provided + + + io.swagger + swagger-annotations + provided + 1.5.3 + + + junit + junit + ${junit-version} + test + + + org.testng + testng + 6.8.8 + test + + + junit + junit + + + snakeyaml + org.yaml + + + bsh + org.beanshell + + + + + + 4.8.1 + + \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/queryParams.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/queryParams.mustache new file mode 100644 index 000000000000..be8cee8dfe1f --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/queryParams.mustache @@ -0,0 +1 @@ +{{#isQueryParam}}@QueryParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/isQueryParam}} \ 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 f0d0fde5e576..c150f27b5626 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 @@ -11,6 +11,7 @@ io.swagger.codegen.languages.JavaClientCodegen io.swagger.codegen.languages.JavaJerseyServerCodegen io.swagger.codegen.languages.JavaCXFServerCodegen io.swagger.codegen.languages.JavaResteasyServerCodegen +io.swagger.codegen.languages.JavaJAXRSSpecServerCodegen io.swagger.codegen.languages.JavaInflectorServerCodegen io.swagger.codegen.languages.JavascriptClientCodegen io.swagger.codegen.languages.JavascriptClosureAngularClientCodegen