Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added cookie support for scala akka http server generator #210

Merged
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
Expand Up @@ -70,6 +70,7 @@ public static Map<String, Object> setComplexTypes(Map<String, Object> objs) {
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
Boolean hasComplexTypes = Boolean.FALSE;
Boolean hasCookieParams = Boolean.FALSE;
Set<String> complexRequestTypes = new HashSet<>();
Set<String> complexReturnTypes = new HashSet<>();
for (CodegenOperation op : operationList) {
Expand All @@ -81,6 +82,9 @@ public static Map<String, Object> setComplexTypes(Map<String, Object> objs) {
complexRequestTypes.add(parameter.dataType);
}
}
if(parameter.getIsCookieParam()){
hasCookieParams = Boolean.TRUE;
}
}
for(CodegenResponse response : op.responses) {
if(!response.getIsPrimitiveType()){
Expand All @@ -92,6 +96,7 @@ public static Map<String, Object> setComplexTypes(Map<String, Object> objs) {
op.getVendorExtensions().put("complexReturnTypes", complexOperationReturnTypes);
}
objs.put("hasComplexTypes", hasComplexTypes);
objs.put("hasCookieParams", hasCookieParams);
objs.put("complexRequestTypes", complexRequestTypes);
objs.put("complexReturnTypes", complexReturnTypes);

Expand Down Expand Up @@ -156,6 +161,7 @@ protected static void addPathMatcher(CodegenOperation codegenOperation) {
}};

protected static String FALLBACK_DATA_TYPE = "String";
protected static String COOKIE_DATA_TYPE = "HttpCookiePair";

private static LinkedList<TextOrMatcher> replacePathsWithMatchers(LinkedList<String> paths, CodegenOperation codegenOperation) {
LinkedList<TextOrMatcher> result = new LinkedList<>();
Expand Down Expand Up @@ -238,6 +244,8 @@ public static void addAllParamsWithSupportedTypes(CodegenOperation codegenOperat
if(!primitiveParamTypes.contains(parameter.dataType)){
parameterCopy.dataType = FALLBACK_DATA_TYPE;
}
} else if(parameter.getIsCookieParam()){
parameterCopy.dataType = COOKIE_DATA_TYPE;
}
allParamsWithSupportedType.add(parameterCopy);
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/resources/mustache/scala/akka-http-server/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import akka.http.scaladsl.server.Route
{{^operations.complexRequestTypes.isEmpty}}import akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller
import akka.http.scaladsl.marshalling.ToEntityMarshaller
{{/operations.complexRequestTypes.isEmpty}}
{{#hasCookieParams}}import akka.http.scaladsl.model.headers.HttpCookiePair
{{/hasCookieParams}}
import {{invokerPackage}}.AkkaHttpHelper._
{{#imports}}import {{import}}
{{/imports}}
Expand All @@ -24,9 +26,11 @@ class {{classname}}(
{{^queryParams.isEmpty}}parameters({{#vendorExtensions.queryParamsWithSupportedType}}"{{paramName}}".as[{{dataType}}]{{^required}}.?{{/required}}{{#hasMore}}, {{/hasMore}}{{/vendorExtensions.queryParamsWithSupportedType}}) { ({{#queryParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/queryParams}}) =>{{/queryParams.isEmpty}}
{{#headerParams}}{{#required}}headerValueByName{{/required}}{{^required}}optionalHeaderValueByName{{/required}}("{{paramName}}") { {{paramName}} => {{/headerParams}}
{{^formParams.isEmpty}}formFields({{#formParams}}"{{paramName}}".as[{{#isPrimitiveType}}{{dataType}}{{/isPrimitiveType}}{{^isPrimitiveType}}String{{/isPrimitiveType}}]{{^required}}.?{{/required}}{{#hasMore}}, {{/hasMore}}{{/formParams}}) { ({{#formParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/formParams}}) =>{{/formParams.isEmpty}}
{{#bodyParam}}{{^isPrimitiveType}}entity(as[{{dataType}}]){ body =>{{/isPrimitiveType}}{{/bodyParam}}
{{classVarName}}Service.{{operationId}}({{#allParams}}{{paramName}} = {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{{#bodyParam}}{{^isPrimitiveType}} }{{/isPrimitiveType}}{{/bodyParam}}
{{#allParams}}{{#isCookieParam}}{{#required}}cookie({{/required}}{{^required}}optionalCookie({{/required}}"{{paramName}}"){ {{paramName}} => {{/isCookieParam}}{{/allParams}}
{{#bodyParam}}{{^isPrimitiveType}}entity(as[{{dataType}}]){ body =>{{/isPrimitiveType}}{{/bodyParam}}
{{classVarName}}Service.{{operationId}}({{#allParams}}{{paramName}} = {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{{#bodyParam}}{{^isPrimitiveType}} }{{/isPrimitiveType}}{{/bodyParam}}
{{#allParams}}{{#isCookieParam}} }{{/isCookieParam}}{{/allParams}}
{{^formParams.isEmpty}} }{{/formParams.isEmpty}}
{{#headerParams}} }{{/headerParams}}
{{^queryParams.isEmpty}} }{{/queryParams.isEmpty}}
Expand All @@ -45,7 +49,7 @@ trait {{classname}}Service {
{{#responses}} * {{#code}}Code: {{.}},{{/code}} {{#message}}Message: {{.}},{{/message}} {{#dataType}}DataType: {{.}}{{/dataType}}
{{/responses}}
*/
def {{operationId}}({{#vendorExtensions.paramsWithSupportedType}}{{paramName}}: {{^required}}Option[{{/required}}{{dataType}}{{^required}}]{{/required}}{{#hasMore}}, {{/hasMore}}{{/vendorExtensions.paramsWithSupportedType}}){{^vendorExtensions.complexReturnTypes.isEmpty}}
def {{operationId}}({{#vendorExtensions.paramsWithSupportedType}}{{paramName}}: {{^required}}{{^isBodyParam}}Option[{{/isBodyParam}}{{/required}}{{dataType}}{{^required}}{{^isBodyParam}}]{{/isBodyParam}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/vendorExtensions.paramsWithSupportedType}}){{^vendorExtensions.complexReturnTypes.isEmpty}}
(implicit {{#vendorExtensions.complexReturnTypes}}toEntityMarshaller{{.}}: ToEntityMarshaller[{{.}}]{{^-last}}, {{/-last}}{{/vendorExtensions.complexReturnTypes}}){{/vendorExtensions.complexReturnTypes.isEmpty}}: Route

{{/operation}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"openapi": "3.0.0",
"info": {
"title": "cookie-parameters",
"version": "1.0"
},
"paths": {
"/parameters": {
"get": {
"tags": [
"pet"
],
"summary": "Find some pet",
"description": "Returns a single pet",
"operationId": "getPet",
"parameters": [
{
"name": "integer",
"in": "cookie",
"description": "some integer value",
"required": true,
"schema": {
"type": "integer",
"format": "int32"
}
},
{
"name": "long",
"in": "cookie",
"description": "some long value",
"required": false,
"schema": {
"type": "integer",
"format": "int64"
}
},
{
"name": "float",
"in": "cookie",
"description": "some float value",
"required": true,
"schema": {
"type": "number",
"format": "float"
}
},
{
"name": "double",
"in": "cookie",
"description": "some double value",
"required": false,
"schema": {
"type": "number",
"format": "double"
}
},
{
"name": "arrayParam",
"in": "cookie",
"description": "some array value",
"required": true,
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
},
{
"name": "boolean",
"in": "cookie",
"description": "some boolean value",
"required": false,
"schema": {
"type": "boolean"
}
},
{
"name": "string",
"in": "cookie",
"description": "some string value",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "successful operation",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
}
}