Added functionality to handle optional parameters for Scala (#3683)

* 1. Modified api.mushtache to handle optional paramters for scala (#3665)
2. Updated petstore sample for scala

* Generated scala-petstore.sh (#3665)

* 1. Updated api.mustache to handle optional thing with headers and file parameters
2. Generated petstore sample for the above changes
This commit is contained in:
Jyotsna Karan 2016-09-01 15:41:23 +05:30 committed by wing328
parent 8e7d3d1a72
commit 062e6fc3ac
4 changed files with 86 additions and 57 deletions

View File

@ -31,7 +31,7 @@ class {{classname}}(val defBasePath: String = "{{basePath}}",
{{#allParams}} * @param {{paramName}} {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}} * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
*/
def {{operationId}} ({{#allParams}}{{paramName}}: {{dataType}}{{#defaultValue}} /* = {{{defaultValue}}} */{{/defaultValue}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {{#returnType}}: Option[{{returnType}}]{{/returnType}} = {
def {{operationId}}({{#allParams}}{{paramName}}: {{#required}}{{dataType}}{{#defaultValue}} /* = {{{defaultValue}}}*/{{/defaultValue}}{{/required}}{{^required}}Option[{{dataType}}]{{#defaultValue}} /* = {{{defaultValue}}}*/{{/defaultValue}}{{^defaultValue}} = None{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}){{#returnType}}: Option[{{returnType}}]{{/returnType}} = {
// create path and map variables
val path = "{{path}}".replaceAll("\\{format\\}","json"){{#pathParams}}.replaceAll("\\{" + "{{baseName}}" + "\\}",apiInvoker.escape({{paramName}}))
@ -53,27 +53,57 @@ class {{classname}}(val defBasePath: String = "{{basePath}}",
{{/isPrimitiveType}}
{{/required}}
{{/allParams}}
{{#queryParams}}if(String.valueOf({{paramName}}) != "null") queryParams += "{{baseName}}" -> {{paramName}}.toString
{{#queryParams}}
{{#required}}
if(String.valueOf({{paramName}}) != "null") queryParams += "{{baseName}}" -> {{paramName}}.toString
{{/required}}
{{^required}}
{{paramName}}.map(paramVal => queryParams += "{{baseName}}" -> paramVal.toString)
{{/required}}
{{/queryParams}}
{{#headerParams}}headerParams += "{{baseName}}" -> {{paramName}}
{{#headerParams}}
{{#required}}
headerParams += "{{baseName}}" -> {{paramName}}
{{/required}}
{{^required}}
{{paramName}}.map(paramVal => headerParams += "{{baseName}}" -> paramVal)
{{/required}}
{{/headerParams}}
var postBody: AnyRef = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}
var postBody: AnyRef = {{#bodyParam}}{{#required}}{{paramName}}{{/required}}{{^required}}{{paramName}}.map(paramVal => paramVal){{/required}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}
if(contentType.startsWith("multipart/form-data")) {
val mp = new FormDataMultiPart()
{{#formParams}}{{#notFile}}
{{#required}}
mp.field("{{baseName}}", {{paramName}}.toString(), MediaType.MULTIPART_FORM_DATA_TYPE)
{{/required}}
{{^required}}
{{paramName}}.map(paramVal => mp.field("{{baseName}}", paramVal.toString, MediaType.MULTIPART_FORM_DATA_TYPE))
{{/required}}
{{/notFile}}{{#isFile}}
{{#required}}
mp.field("{{baseName}}", file.getName)
mp.bodyPart(new FileDataBodyPart("{{baseName}}", {{paramName}}, MediaType.MULTIPART_FORM_DATA_TYPE))
{{/required}}
{{^required}}
file.map(fileVal => mp.field("{{baseName}}", fileVal.getName))
{{paramName}}.map(paramVal => mp.bodyPart(new FileDataBodyPart("{{baseName}}", paramVal, MediaType.MULTIPART_FORM_DATA_TYPE)))
{{/required}}
{{/isFile}}{{/formParams}}
postBody = mp
}
else {
{{#formParams}}
{{#notFile}}formParams += "{{baseName}}" -> {{paramName}}.toString(){{/notFile}}
{{#notFile}}
{{#required}}
formParams += "{{baseName}}" -> {{paramName}}.toString()
{{/required}}
{{^required}}
{{paramName}}.map(paramVal => formParams += "{{baseName}}" -> paramVal.toString)
{{/required}}
{{/notFile}}
{{/formParams}}
}

View File

@ -25,8 +25,8 @@
package io.swagger.client.api
import io.swagger.client.model.Pet
import io.swagger.client.model.ApiResponse
import java.io.File
import io.swagger.client.model.ApiResponse
import io.swagger.client.ApiInvoker
import io.swagger.client.ApiException
@ -53,7 +53,7 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param body Pet object that needs to be added to the store
* @return void
*/
def addPet (body: Pet) = {
def addPet(body: Pet) = {
// create path and map variables
val path = "/pet".replaceAll("\\{format\\}","json")
val contentTypes = List("application/json", "application/xml", "application/json")
@ -66,8 +66,8 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
if (body == null) throw new Exception("Missing required parameter 'body' when calling PetApi->addPet")
var postBody: AnyRef = body
if(contentType.startsWith("multipart/form-data")) {
@ -96,7 +96,7 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param apiKey (optional)
* @return void
*/
def deletePet (petId: Long, apiKey: String) = {
def deletePet(petId: Long, apiKey: Option[String] = None) = {
// create path and map variables
val path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId))
@ -109,8 +109,8 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
val headerParams = new HashMap[String, String]
val formParams = new HashMap[String, String]
headerParams += "api_key" -> apiKey
apiKey.map(paramVal => headerParams += "api_key" -> paramVal)
var postBody: AnyRef = null
@ -139,7 +139,7 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param status Status values that need to be considered for filter
* @return List[Pet]
*/
def findPetsByStatus (status: List[String]) : Option[List[Pet]] = {
def findPetsByStatus(status: List[String]): Option[List[Pet]] = {
// create path and map variables
val path = "/pet/findByStatus".replaceAll("\\{format\\}","json")
val contentTypes = List("application/json")
@ -154,7 +154,7 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
if(String.valueOf(status) != "null") queryParams += "status" -> status.toString
var postBody: AnyRef = null
if(contentType.startsWith("multipart/form-data")) {
@ -183,7 +183,7 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param tags Tags to filter by
* @return List[Pet]
*/
def findPetsByTags (tags: List[String]) : Option[List[Pet]] = {
def findPetsByTags(tags: List[String]): Option[List[Pet]] = {
// create path and map variables
val path = "/pet/findByTags".replaceAll("\\{format\\}","json")
val contentTypes = List("application/json")
@ -198,7 +198,7 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
if(String.valueOf(tags) != "null") queryParams += "tags" -> tags.toString
var postBody: AnyRef = null
if(contentType.startsWith("multipart/form-data")) {
@ -227,7 +227,7 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param petId ID of pet to return
* @return Pet
*/
def getPetById (petId: Long) : Option[Pet] = {
def getPetById(petId: Long): Option[Pet] = {
// create path and map variables
val path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId))
@ -240,8 +240,8 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
val headerParams = new HashMap[String, String]
val formParams = new HashMap[String, String]
var postBody: AnyRef = null
if(contentType.startsWith("multipart/form-data")) {
@ -270,7 +270,7 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param body Pet object that needs to be added to the store
* @return void
*/
def updatePet (body: Pet) = {
def updatePet(body: Pet) = {
// create path and map variables
val path = "/pet".replaceAll("\\{format\\}","json")
val contentTypes = List("application/json", "application/xml", "application/json")
@ -283,8 +283,8 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
if (body == null) throw new Exception("Missing required parameter 'body' when calling PetApi->updatePet")
var postBody: AnyRef = body
if(contentType.startsWith("multipart/form-data")) {
@ -314,7 +314,7 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param status Updated status of the pet (optional)
* @return void
*/
def updatePetWithForm (petId: Long, name: String, status: String) = {
def updatePetWithForm(petId: Long, name: Option[String] = None, status: Option[String] = None) = {
// create path and map variables
val path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId))
@ -327,22 +327,22 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
val headerParams = new HashMap[String, String]
val formParams = new HashMap[String, String]
var postBody: AnyRef = null
if(contentType.startsWith("multipart/form-data")) {
val mp = new FormDataMultiPart()
mp.field("name", name.toString(), MediaType.MULTIPART_FORM_DATA_TYPE)
name.map(paramVal => mp.field("name", paramVal.toString, MediaType.MULTIPART_FORM_DATA_TYPE))
mp.field("status", status.toString(), MediaType.MULTIPART_FORM_DATA_TYPE)
status.map(paramVal => mp.field("status", paramVal.toString, MediaType.MULTIPART_FORM_DATA_TYPE))
postBody = mp
}
else {
formParams += "name" -> name.toString()
formParams += "status" -> status.toString()
name.map(paramVal => formParams += "name" -> paramVal.toString)
status.map(paramVal => formParams += "status" -> paramVal.toString)
}
try {
@ -364,7 +364,7 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param file file to upload (optional)
* @return ApiResponse
*/
def uploadFile (petId: Long, additionalMetadata: String, file: File) : Option[ApiResponse] = {
def uploadFile(petId: Long, additionalMetadata: Option[String] = None, file: Option[File] = None): Option[ApiResponse] = {
// create path and map variables
val path = "/pet/{petId}/uploadImage".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId))
@ -377,23 +377,22 @@ class PetApi(val defBasePath: String = "http://petstore.swagger.io/v2",
val headerParams = new HashMap[String, String]
val formParams = new HashMap[String, String]
var postBody: AnyRef = null
if(contentType.startsWith("multipart/form-data")) {
val mp = new FormDataMultiPart()
mp.field("additionalMetadata", additionalMetadata.toString(), MediaType.MULTIPART_FORM_DATA_TYPE)
additionalMetadata.map(paramVal => mp.field("additionalMetadata", paramVal.toString, MediaType.MULTIPART_FORM_DATA_TYPE))
mp.field("file", file.getName)
mp.bodyPart(new FileDataBodyPart("file", file, MediaType.MULTIPART_FORM_DATA_TYPE))
file.map(fileVal => mp.field("file", fileVal.getName))
file.map(paramVal => mp.bodyPart(new FileDataBodyPart("file", paramVal, MediaType.MULTIPART_FORM_DATA_TYPE)))
postBody = mp
}
else {
formParams += "additionalMetadata" -> additionalMetadata.toString()
additionalMetadata.map(paramVal => formParams += "additionalMetadata" -> paramVal.toString)
}
try {

View File

@ -51,7 +51,7 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param orderId ID of the order that needs to be deleted
* @return void
*/
def deleteOrder (orderId: String) = {
def deleteOrder(orderId: String) = {
// create path and map variables
val path = "/store/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}",apiInvoker.escape(orderId))
@ -64,8 +64,8 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2",
val headerParams = new HashMap[String, String]
val formParams = new HashMap[String, String]
var postBody: AnyRef = null
if(contentType.startsWith("multipart/form-data")) {
@ -92,7 +92,7 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* Returns a map of status codes to quantities
* @return Map[String, Integer]
*/
def getInventory () : Option[Map[String, Integer]] = {
def getInventory(): Option[Map[String, Integer]] = {
// create path and map variables
val path = "/store/inventory".replaceAll("\\{format\\}","json")
val contentTypes = List("application/json")
@ -103,8 +103,8 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2",
val headerParams = new HashMap[String, String]
val formParams = new HashMap[String, String]
var postBody: AnyRef = null
if(contentType.startsWith("multipart/form-data")) {
@ -133,7 +133,7 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param orderId ID of pet that needs to be fetched
* @return Order
*/
def getOrderById (orderId: Long) : Option[Order] = {
def getOrderById(orderId: Long): Option[Order] = {
// create path and map variables
val path = "/store/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}",apiInvoker.escape(orderId))
@ -146,8 +146,8 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2",
val headerParams = new HashMap[String, String]
val formParams = new HashMap[String, String]
var postBody: AnyRef = null
if(contentType.startsWith("multipart/form-data")) {
@ -176,7 +176,7 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param body order placed for purchasing the pet
* @return Order
*/
def placeOrder (body: Order) : Option[Order] = {
def placeOrder(body: Order): Option[Order] = {
// create path and map variables
val path = "/store/order".replaceAll("\\{format\\}","json")
val contentTypes = List("application/json")
@ -189,8 +189,8 @@ class StoreApi(val defBasePath: String = "http://petstore.swagger.io/v2",
if (body == null) throw new Exception("Missing required parameter 'body' when calling StoreApi->placeOrder")
var postBody: AnyRef = body
if(contentType.startsWith("multipart/form-data")) {

View File

@ -51,7 +51,7 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param body Created user object
* @return void
*/
def createUser (body: User) = {
def createUser(body: User) = {
// create path and map variables
val path = "/user".replaceAll("\\{format\\}","json")
val contentTypes = List("application/json")
@ -64,8 +64,8 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2",
if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUser")
var postBody: AnyRef = body
if(contentType.startsWith("multipart/form-data")) {
@ -93,7 +93,7 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param body List of user object
* @return void
*/
def createUsersWithArrayInput (body: List[User]) = {
def createUsersWithArrayInput(body: List[User]) = {
// create path and map variables
val path = "/user/createWithArray".replaceAll("\\{format\\}","json")
val contentTypes = List("application/json")
@ -106,8 +106,8 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2",
if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUsersWithArrayInput")
var postBody: AnyRef = body
if(contentType.startsWith("multipart/form-data")) {
@ -135,7 +135,7 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param body List of user object
* @return void
*/
def createUsersWithListInput (body: List[User]) = {
def createUsersWithListInput(body: List[User]) = {
// create path and map variables
val path = "/user/createWithList".replaceAll("\\{format\\}","json")
val contentTypes = List("application/json")
@ -148,8 +148,8 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2",
if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUsersWithListInput")
var postBody: AnyRef = body
if(contentType.startsWith("multipart/form-data")) {
@ -177,7 +177,7 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param username The name that needs to be deleted
* @return void
*/
def deleteUser (username: String) = {
def deleteUser(username: String) = {
// create path and map variables
val path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escape(username))
@ -190,8 +190,8 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2",
val headerParams = new HashMap[String, String]
val formParams = new HashMap[String, String]
var postBody: AnyRef = null
if(contentType.startsWith("multipart/form-data")) {
@ -219,7 +219,7 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param username The name that needs to be fetched. Use user1 for testing.
* @return User
*/
def getUserByName (username: String) : Option[User] = {
def getUserByName(username: String): Option[User] = {
// create path and map variables
val path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escape(username))
@ -232,8 +232,8 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2",
val headerParams = new HashMap[String, String]
val formParams = new HashMap[String, String]
var postBody: AnyRef = null
if(contentType.startsWith("multipart/form-data")) {
@ -263,7 +263,7 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2",
* @param password The password for login in clear text
* @return String
*/
def loginUser (username: String, password: String) : Option[String] = {
def loginUser(username: String, password: String): Option[String] = {
// create path and map variables
val path = "/user/login".replaceAll("\\{format\\}","json")
val contentTypes = List("application/json")
@ -275,9 +275,9 @@ class UserApi(val defBasePath: String = "http://petstore.swagger.io/v2",
val formParams = new HashMap[String, String]
if(String.valueOf(username) != "null") queryParams += "username" -> username.toString
if(String.valueOf(password) != "null") queryParams += "password" -> password.toString
if(String.valueOf(password) != "null") queryParams += "password" -> password.toString
var postBody: AnyRef = null
if(contentType.startsWith("multipart/form-data")) {
@ -305,7 +305,7 @@ if(String.valueOf(password) != "null") queryParams += "password" -> password.toS
*
* @return void
*/
def logoutUser () = {
def logoutUser() = {
// create path and map variables
val path = "/user/logout".replaceAll("\\{format\\}","json")
val contentTypes = List("application/json")
@ -316,8 +316,8 @@ if(String.valueOf(password) != "null") queryParams += "password" -> password.toS
val headerParams = new HashMap[String, String]
val formParams = new HashMap[String, String]
var postBody: AnyRef = null
if(contentType.startsWith("multipart/form-data")) {
@ -346,7 +346,7 @@ if(String.valueOf(password) != "null") queryParams += "password" -> password.toS
* @param body Updated user object
* @return void
*/
def updateUser (username: String, body: User) = {
def updateUser(username: String, body: User) = {
// create path and map variables
val path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escape(username))
@ -361,8 +361,8 @@ if(String.valueOf(password) != "null") queryParams += "password" -> password.toS
if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->updateUser")
var postBody: AnyRef = body
if(contentType.startsWith("multipart/form-data")) {