Skip to content

Commit 3bfa9e6

Browse files
committed
added handlebars templates to scala generators
1 parent 36db73f commit 3bfa9e6

21 files changed

+1475
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package {{package}}
2+
3+
import akka.http.scaladsl.server.Directives._
4+
import akka.http.scaladsl.server.Route
5+
{{^operations.complexRequestTypes.isEmpty}}import akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller
6+
import akka.http.scaladsl.marshalling.ToEntityMarshaller
7+
{{/operations.complexRequestTypes.isEmpty}}
8+
{{#hasCookieParams}}import akka.http.scaladsl.model.headers.HttpCookiePair
9+
{{/hasCookieParams}}
10+
import {{invokerPackage}}.AkkaHttpHelper._
11+
{{#imports}}import {{import}}
12+
{{/imports}}
13+
14+
{{#operations}}
15+
class {{classname}}(
16+
{{classVarName}}Service: {{classname}}Service{{#hasComplexTypes}},
17+
{{classVarName}}Marshaller: {{classname}}Marshaller{{/hasComplexTypes}}
18+
) {
19+
{{#hasComplexTypes}}import {{classVarName}}Marshaller._
20+
{{/hasComplexTypes}}
21+
22+
lazy val route: Route =
23+
{{#operation}}
24+
path({{#vendorExtensions.paths}}{{#isText}}"{{/isText}}{{value}}{{#isText}}"{{/isText}}{{#hasMore}} / {{/hasMore}}{{/vendorExtensions.paths}}) { {{^pathParams.isEmpty}}({{#pathParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/pathParams}}) => {{/pathParams.isEmpty}}
25+
{{vendorExtensions.lowercaseHttpMethod}} {
26+
{{^queryParams.isEmpty}}parameters({{#vendorExtensions.queryParamsWithSupportedType}}"{{baseName}}".as[{{dataType}}]{{^required}}.?{{/required}}{{#hasMore}}, {{/hasMore}}{{/vendorExtensions.queryParamsWithSupportedType}}) { ({{#queryParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/queryParams}}) =>{{/queryParams.isEmpty}}
27+
{{#headerParams}}{{#required}}headerValueByName{{/required}}{{^required}}optionalHeaderValueByName{{/required}}("{{baseName}}") { {{paramName}} => {{/headerParams}}
28+
{{^formParams.isEmpty}}formFields({{#formParams}}"{{baseName}}".as[{{#isPrimitiveType}}{{dataType}}{{/isPrimitiveType}}{{^isPrimitiveType}}String{{/isPrimitiveType}}]{{^required}}.?{{/required}}{{#hasMore}}, {{/hasMore}}{{/formParams}}) { ({{#formParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/formParams}}) =>{{/formParams.isEmpty}}
29+
{{#allParams}}{{#isCookieParam}}{{#required}}cookie({{/required}}{{^required}}optionalCookie({{/required}}"{{baseName}}"){ {{paramName}} => {{/isCookieParam}}{{/allParams}}
30+
{{#bodyParam}}{{^isPrimitiveType}}entity(as[{{dataType}}]){ body =>{{/isPrimitiveType}}{{/bodyParam}}
31+
{{classVarName}}Service.{{operationId}}({{#allParams}}{{paramName}} = {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
32+
{{#bodyParam}}{{^isPrimitiveType}} }{{/isPrimitiveType}}{{/bodyParam}}
33+
{{#allParams}}{{#isCookieParam}} }{{/isCookieParam}}{{/allParams}}
34+
{{^formParams.isEmpty}} }{{/formParams.isEmpty}}
35+
{{#headerParams}} }{{/headerParams}}
36+
{{^queryParams.isEmpty}} }{{/queryParams.isEmpty}}
37+
}
38+
}{{^@last}} ~{{/@last}}
39+
{{/operation}}
40+
}
41+
42+
trait {{classname}}Service {
43+
44+
{{#operation}}
45+
{{#responses}} def {{operationId}}{{code}}{{#baseType}}(response{{baseType}}{{containerType}}: {{dataType}}){{^isPrimitiveType}}(implicit toEntityMarshaller{{baseType}}{{containerType}}: ToEntityMarshaller[{{dataType}}]){{/isPrimitiveType}}{{/baseType}}: Route =
46+
complete(({{code}}, {{#baseType}}response{{baseType}}{{containerType}}{{/baseType}}{{^baseType}}"{{message}}"{{/baseType}}))
47+
{{/responses}}
48+
/**
49+
{{#responses}} * {{#code}}Code: {{.}}{{/code}}{{#message}}, Message: {{.}}{{/message}}{{#dataType}}, DataType: {{.}}{{/dataType}}
50+
{{/responses}}
51+
*/
52+
def {{operationId}}({{#vendorExtensions.paramsWithSupportedType}}{{paramName}}: {{^required}}{{^isBodyParam}}Option[{{/isBodyParam}}{{/required}}{{dataType}}{{^required}}{{^isBodyParam}}]{{/isBodyParam}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/vendorExtensions.paramsWithSupportedType}}){{^vendorExtensions.complexReturnTypes.isEmpty}}
53+
(implicit {{#vendorExtensions.complexReturnTypes}}toEntityMarshaller{{baseType}}{{containerType}}: ToEntityMarshaller[{{dataType}}]{{^@last}}, {{/@last}}{{/vendorExtensions.complexReturnTypes}}){{/vendorExtensions.complexReturnTypes.isEmpty}}: Route
54+
55+
{{/operation}}
56+
}
57+
58+
{{#hasComplexTypes}}
59+
trait {{classname}}Marshaller {
60+
{{#complexRequestTypes}} implicit def fromRequestUnmarshaller{{.}}: FromRequestUnmarshaller[{{.}}]
61+
62+
{{/complexRequestTypes}}
63+
64+
{{#complexReturnTypes}} implicit def toEntityMarshaller{{baseType}}{{containerType}}: ToEntityMarshaller[{{dataType}}]
65+
66+
{{/complexReturnTypes}}
67+
}
68+
{{/hasComplexTypes}}
69+
70+
{{/operations}}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version := "{{artifactVersion}}"
2+
name := "{{artifactId}}"
3+
organization := "{{groupId}}"
4+
scalaVersion := "2.12.6"
5+
6+
libraryDependencies ++= Seq(
7+
"com.typesafe.akka" %% "akka-http" % "10.1.5",
8+
"com.typesafe.akka" %% "akka-stream" % "2.5.16",
9+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package {{invokerPackage}}
2+
3+
import akka.http.scaladsl.Http
4+
import akka.http.scaladsl.server.Route
5+
{{#apiInfo}}{{#apis}}{{#operations}}import {{package}}.{{classname}}
6+
{{/operations}}{{/apis}}{{/apiInfo}}
7+
import akka.http.scaladsl.server.Directives._
8+
import akka.actor.ActorSystem
9+
import akka.stream.ActorMaterializer
10+
11+
class Controller({{#apiInfo}}{{#apis}}{{#operations}}{{classVarName}}: {{classname}}{{#hasMore}}, {{/hasMore}}{{/operations}}{{/apis}}{{/apiInfo}})(implicit system: ActorSystem, materializer: ActorMaterializer) {
12+
13+
lazy val routes: Route = {{#apiInfo}}{{#apis}}{{#operations}}{{classVarName}}.route {{#hasMore}}~ {{/hasMore}}{{/operations}}{{/apis}}{{/apiInfo}}
14+
15+
Http().bindAndHandle(routes, "0.0.0.0", 9000)
16+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package {{invokerPackage}}
2+
3+
import akka.http.scaladsl.server.Directives._
4+
import akka.http.scaladsl.server.{PathMatcher, PathMatcher1}
5+
6+
7+
object AkkaHttpHelper {
8+
/**
9+
* A PathMatcher that matches and extracts a Float value. The matched string representation is the pure decimal,
10+
* optionally signed form of a float value, i.e. without exponent.
11+
*
12+
* @group pathmatcher
13+
*/
14+
val FloatNumber: PathMatcher1[Float] =
15+
PathMatcher("""[+-]?\d*\.?\d*""".r) flatMap { string ⇒
16+
try Some(java.lang.Float.parseFloat(string))
17+
catch { case _: NumberFormatException ⇒ None }
18+
}
19+
20+
/**
21+
* A PathMatcher that matches and extracts a Boolean value.
22+
*
23+
* @group pathmatcher
24+
*/
25+
val Boolean: PathMatcher1[Boolean] =
26+
Segment.flatMap { string =>
27+
try Some(string.toBoolean)
28+
catch { case _: IllegalArgumentException => None }
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package {{package}}
2+
3+
{{#imports}}
4+
import {{import}}
5+
{{/imports}}
6+
7+
{{#models}}
8+
{{#model}}
9+
/**
10+
{{#title}} * = {{{title}}} =
11+
*
12+
{{/title}}
13+
{{#description}} * {{{description}}}
14+
*
15+
{{/description}}
16+
{{#vars}}
17+
* @param {{{name}}} {{#description}}{{{description}}}{{/description}}{{#example}} for example: ''{{{example}}}''{{/example}}
18+
{{/vars}}
19+
*/
20+
case class {{classname}} (
21+
{{#vars}}
22+
{{{name}}}: {{^required}}Option[{{/required}}{{datatype}}{{^required}}]{{/required}}{{#hasMore}},{{/hasMore}}
23+
{{/vars}}
24+
)
25+
26+
{{/model}}
27+
{{/models}}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# NAME
2+
3+
{{appName}}
4+
5+
{{#appDescription}}{{{appDescription}}}{{/appDescription}}
6+
7+
# VERSION
8+
9+
Automatically generated by the [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) project:
10+
11+
- API version: {{appVersion}}
12+
- Package version: {{moduleVersion}}
13+
{{^hideGenerationTimestamp}}
14+
- Build date: {{generatedDate}}
15+
{{/hideGenerationTimestamp}}
16+
- Build package: {{generatorClass}}
17+
{{#infoUrl}}
18+
For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
19+
{{/infoUrl}}
20+
21+
# Requirements
22+
23+
Building the API client library requires [Maven](https://maven.apache.org/) to be installed.
24+
25+
## Installation
26+
27+
To install the API client library to your local Maven repository, simply execute:
28+
29+
```shell
30+
mvn install
31+
```
32+
33+
To deploy it to a remote Maven repository instead, configure the settings of the repository and execute:
34+
35+
```shell
36+
mvn deploy
37+
```
38+
39+
Refer to the [official documentation](https://maven.apache.org/plugins/maven-deploy-plugin/usage.html) for more information.
40+
41+
### Maven users
42+
43+
Add this dependency to your project's POM:
44+
45+
```xml
46+
<dependency>
47+
<groupId>{{{groupId}}}</groupId>
48+
<artifactId>{{{artifactId}}}</artifactId>
49+
<version>{{{artifactVersion}}}</version>
50+
<scope>compile</scope>
51+
</dependency>
52+
```
53+
54+
### Gradle users
55+
56+
Add this dependency to your project's build file:
57+
58+
```groovy
59+
compile "{{{groupId}}}:{{{artifactId}}}:{{{artifactVersion}}}"
60+
```
61+
62+
### SBT users
63+
64+
```scala
65+
libraryDependencies += "{{{groupId}}}" % "{{{artifactId}}}" % "{{{artifactVersion}}}"
66+
```
67+
68+
## Documentation for API Endpoints
69+
70+
All URIs are relative to *{{basePath}}*
71+
72+
Class | Method | HTTP request | Description
73+
------------ | ------------- | ------------- | -------------
74+
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}*{{classname}}* | [**{{operationId}}**]({{apiDocPath}}{{classname}}.md#{{operationId}}) | **{{httpMethod}}** {{path}} | {{#summary}}{{summary}}{{/summary}}
75+
{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
76+
77+
## Documentation for Models
78+
79+
{{#models}}{{#model}} - [{{classname}}]({{modelDocPath}}{{classname}}.md)
80+
{{/model}}{{/models}}
81+
82+
## Documentation for Authorization
83+
84+
{{^authMethods}}All endpoints do not require authorization.
85+
{{/authMethods}}Authentication schemes defined for the API:
86+
{{#authMethods}}### {{name}}
87+
88+
{{#isApiKey}}- **Type**: API key
89+
- **API key parameter name**: {{keyParamName}}
90+
- **Location**: {{#isKeyInQuery}}URL query string{{/isKeyInQuery}}{{#isKeyInHeader}}HTTP header{{/isKeyInHeader}}
91+
{{/isApiKey}}
92+
{{#isBasic}}- **Type**: HTTP basic authentication
93+
{{/isBasic}}
94+
{{#isOAuth}}- **Type**: OAuth
95+
- **Flow**: {{flow}}
96+
- **Authorization URL**: {{authorizationUrl}}
97+
- **Scopes**: {{^scopes}}N/A{{/scopes}}
98+
{{#scopes}} - {{scope}}: {{description}}
99+
{{/scopes}}
100+
{{/isOAuth}}
101+
102+
{{/authMethods}}
103+
104+
105+
# BUILDING YOUR LIBRARY
106+
107+
See the homepage `https://github.com/swagger-api/swagger-codegen` for full details.
108+
But briefly, clone the git repository, build the codegen codebase, set up your build
109+
config file, then run the API build script. You will need git, Java 7 or 8 and Apache
110+
maven 3.0.3 or better already installed.
111+
112+
Your library files will be built under `WWW::MyProjectName`.
113+
114+
$ git clone https://github.com/swagger-api/swagger-codegen.git
115+
$ cd swagger-codegen
116+
$ mvn package
117+
$ java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
118+
-i [URL or file path to JSON swagger API spec] \
119+
-l akka-scala \
120+
-c /path/to/config/file.json \
121+
-o /path/to/output/folder
122+
123+
Bang, all done. Run the `autodoc` script in the `bin` directory to see the API
124+
you just built.
125+
126+
## Author
127+
128+
{{#apiInfo}}{{#apis}}{{^hasMore}}{{infoEmail}}
129+
{{/hasMore}}{{/apis}}{{/apiInfo}}

0 commit comments

Comments
 (0)