Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package io.swagger.codegen.languages;

import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenConstants;
import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.CodegenParameter;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.SupportingFile;
import com.google.common.base.Strings;
import io.swagger.codegen.*;
import io.swagger.models.Model;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;
Expand Down Expand Up @@ -93,17 +88,16 @@ public FinchServerCodegen() {
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
} else {
additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName);
};
}

supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("build.sbt", "", "build.sbt"));
//supportingFiles.add(new SupportingFile("web.xml", "/src/main/webapp/WEB-INF", "web.xml"));
//supportingFiles.add(new SupportingFile("JettyMain.mustache", sourceFolder, "JettyMain.scala"));
//supportingFiles.add(new SupportingFile("Bootstrap.mustache", sourceFolder, "FinchBootstrap.scala"));
//supportingFiles.add(new SupportingFile("ServletApp.mustache", sourceFolder, "ServletApp.scala"));
//supportingFiles.add(new SupportingFile("project/build.properties", "project", "build.properties"));
//supportingFiles.add(new SupportingFile("project/plugins.sbt", "project", "plugins.sbt"));
//supportingFiles.add(new SupportingFile("sbt", "", "sbt"));
supportingFiles.add(new SupportingFile("Server.mustache", sourceFolder, "Server.scala"));
supportingFiles.add(new SupportingFile("DataAccessor.mustache", sourceFolder, "DataAccessor.scala"));

supportingFiles.add(new SupportingFile("project/build.properties", "project", "build.properties"));
supportingFiles.add(new SupportingFile("project/plugins.sbt", "project", "plugins.sbt"));
supportingFiles.add(new SupportingFile("sbt", "", "sbt"));

supportingFiles.add(new SupportingFile("endpoint.mustache", sourceFolder, "endpoint.scala"));
supportingFiles.add(new SupportingFile("errors.mustache", sourceFolder, "errors.scala"));
Expand Down Expand Up @@ -174,6 +168,23 @@ public String modelFileFolder() {
return outputFolder + "/" + sourceFolder + "/" + modelPackage().replace('.', File.separatorChar);
}

/**
* Convert Swagger Model object to Codegen Model object
*
* @param name the name of the model
* @param model Swagger Model object
* @param allDefinitions a map of all Swagger models from the spec
* @return Codegen Model object
*/
@Override
public CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions) {
CodegenModel codegenModel = super.fromModel(name, model, allDefinitions);

codegenModel.vendorExtensions.put("x-varcount", codegenModel.vars.size());

return codegenModel;
}

@Override
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package {{packageName}}

trait DataAccessor {
// TODO: apiInfo -> apis -> operations = ???
// NOTE: ??? throws a not implemented exception

{{#apiInfo}}
{{#apis}}
{{#operations}}
{{#operation}}
/**
* {{{description}}}
* @return A {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}
*/
def {{baseName}}_{{operationId}}({{#allParams}}{{paramName}}: {{{dataType}}}{{^-last}}, {{/-last}}{{/allParams}}): {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}} = ???

{{/operation}}
{{/operations}}
{{/apis}}
{{/apiInfo}}
}

This file was deleted.

33 changes: 33 additions & 0 deletions modules/swagger-codegen/src/main/resources/finch/Server.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package {{packageName}}

import com.twitter.finagle.Http
import com.twitter.finagle.util.LoadService
import com.twitter.util.{Await, Future}

{{#imports}}import {{import}}
{{/imports}}

class Server {
// Loads implementation defined in resources/META-INF/services/{{packageName}}.DataAccessor
val db = LoadService[DataAccessor]() match {
case accessor :: _ => accessor
case _ => new DataAccessor { }
}

val service = endpoint.makeService(db)

val server = Http.serve(":8080", service) //creates service

Await.ready(server)

def close(): Future[Unit] = {
Await.ready(server.close())
}
}

/**
* Launches the PetstoreAPI service when the system is ready.
*/
object Server extends Server with App {
Await.ready(server)
}

This file was deleted.

87 changes: 40 additions & 47 deletions modules/swagger-codegen/src/main/resources/finch/api.mustache
Original file line number Diff line number Diff line change
@@ -1,53 +1,46 @@
{{>licenseInfo}}

package {{packageName}}

{{#imports}}//import {{import}}
{{/imports}}
//import java.io.File

import org.scalatra.{ TypedParamSupport, ScalatraServlet }
import org.scalatra.swagger._
import org.json4s._
import org.json4s.JsonDSL._
import org.scalatra.json.{ JValueResult, JacksonJsonSupport }
import org.scalatra.servlet.{FileUploadSupport, MultipartConfig, SizeConstraintExceededException}

import scala.collection.JavaConverters._

class {{classname}} (implicit val swagger: Swagger) extends ScalatraServlet
with FileUploadSupport
with JacksonJsonSupport
with SwaggerSupport {
protected implicit val jsonFormats: Formats = DefaultFormats

protected val applicationDescription: String = "{{classname}}"
override protected val applicationName: Option[String] = Some("{{basePathWithoutHost}}/{{baseName}}")

before() {
contentType = formats("json")
response.headers += ("Access-Control-Allow-Origin" -> "*")
}
{{#operations}}
{{#operation}}
{{newline}}

val {{nickname}}Operation = (apiOperation[{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Unit{{/returnType}}]("{{nickname}}")
summary "{{{summary}}}"
parameters({{#allParams}}{{>queryParam}}{{>pathParam}}{{>bodyParam}}{{>formParam}}{{>headerParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
)

{{httpMethod}}("{{path}}",operation({{nickname}}Operation)) {
{{#allParams}}
{{#isFile}}val {{paramName}} = fileParams("{{paramName}}"){{/isFile}}
{{^isFile}}{{#isPathParam}}
val {{paramName}} = params.getOrElse("{{paramName}}", halt(400)){{/isPathParam}}
{{>queryParamOperation}}{{>headerParamOperation}}{{>formParamMustache}}{{>bodyParamOperation}}
{{/isFile}}
println("{{paramName}}: " + {{paramName}})
{{/allParams}}
}

{{/operation}}
{{/operations}}
import _root_.argonaut._, Argonaut._
import com.twitter.finagle.Service
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.http.exp.Multipart.FileUpload
import com.twitter.util.Future
import argonaut.Argonaut._
import io.finch._, items._
import io.finch.argonaut._
import java.io.File

object {{classname}} {
/**
* Compiles all service endpoints.
* @return Bundled compilation of all service endpoints.
*/
public def endpoints(da: DataAccessor) =
{{#operations}}
{{#operation}}
{{{operationId}}}(da){{^-last}} :+:{{/-last}}
{{/operation}}
{{/operations}}

{{#operations}}
{{#operation}}
/**
* {{{description}}}
* @return And endpoint representing a {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}
*/
private def {{operationId}}(da: DataAccessor): Endpoint[{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}] =
post( {{{vendorExtensions.x-scala-path}}} {{#allParams}}{{^isPathParam}}{{{dataType}}}{{^-last}} ? {{/-last}}{{/isPathParam}}{{/allParams}}) { {{#hasParams}}({{#allParams}}{{paramName}}: {{{dataType}}}{{^-last}}, {{/-last}}{{/allParams}}) => {{/hasParams}}
{{#returnType}}
Ok(da.{{baseName}}_{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}))
{{/returnType}}
{{^returnType}}
NoContent(da.{{baseName}}_{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}))
{{/returnType}}
}

{{/operation}}
{{/operations}}
}
Loading