From 1e4f30eb16f1410b0c15066daf0d67da9e50866c Mon Sep 17 00:00:00 2001 From: geetikagupta16 Date: Thu, 15 Sep 2016 20:58:01 +0530 Subject: [PATCH] Fixed bug for handling optional header parameters (#3776) * Fixed #3774 Refactored code to handle optional header parameters. * Changed null check with pattern matching --- .../main/resources/asyncscala/api.mustache | 23 ++++++++++++++----- .../scala/io/swagger/client/api/PetApi.scala | 23 +++++-------------- .../io/swagger/client/api/StoreApi.scala | 5 +--- .../scala/io/swagger/client/api/UserApi.scala | 20 +++++++--------- .../async-scala/src/test/scala/Test.scala | 19 ++++++++------- 5 files changed, 41 insertions(+), 49 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/asyncscala/api.mustache b/modules/swagger-codegen/src/main/resources/asyncscala/api.mustache index 7f2edfd9aea..4595c877299 100644 --- a/modules/swagger-codegen/src/main/resources/asyncscala/api.mustache +++ b/modules/swagger-codegen/src/main/resources/asyncscala/api.mustache @@ -25,23 +25,34 @@ class {{classname}}(client: TransportClient, config: SwaggerConfig) extends ApiC {{#required}} {{^isPrimitiveType}} if ({{paramName}} == null) throw new Exception("Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}") - {{/isPrimitiveType}} + {{#isString}} + if ({{paramName}} == null) throw new Exception("Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}") + + {{/isString}} {{/required}} {{/allParams}} {{#queryParams}} {{^required}} - if ({{paramName}} != null) {{paramName}}.foreach { v => queryParams += "{{baseName}}" -> v.toString } - + {{paramName}} match { + case Some(param) => queryParams += "{{baseName}}" -> param.toString + case _ => queryParams + } {{/required}} {{#required}} - if ({{paramName}} != null) queryParams += "{{baseName}}" -> {{paramName}}.toString - + queryParams += "{{baseName}}" -> {{paramName}}.toString {{/required}} {{/queryParams}} - {{#headerParams}} + {{^required}} + {{paramName}} match { + case Some(param) => headerParams += "{{baseName}}" -> param.toString + case _ => headerParams + } + {{/required}} + {{#required}} headerParams += "{{baseName}}" -> {{paramName}}.toString + {{/required}} {{/headerParams}} val resFuture = client.submit("{{httpMethod}}", path, queryParams.toMap, headerParams.toMap, {{#bodyParam}}writer.write({{paramName}}){{/bodyParam}}{{^bodyParam}}"{{emptyBodyParam}}"{{/bodyParam}}) diff --git a/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/api/PetApi.scala b/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/api/PetApi.scala index 3f9576fa8a8..97f1bb2ba62 100644 --- a/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/api/PetApi.scala +++ b/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/api/PetApi.scala @@ -19,8 +19,6 @@ class PetApi(client: TransportClient, config: SwaggerConfig) extends ApiClient(c if (body == null) throw new Exception("Missing required parameter 'body' when calling PetApi->addPet") - - val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) resFuture flatMap { resp => process(reader.read(resp)) @@ -38,8 +36,10 @@ class PetApi(client: TransportClient, config: SwaggerConfig) extends ApiClient(c val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - - headerParams += "api_key" -> apiKey.toString + apiKey match { + case Some(param) => headerParams += "api_key" -> param.toString + case _ => headerParams + } val resFuture = client.submit("DELETE", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => @@ -56,10 +56,7 @@ class PetApi(client: TransportClient, config: SwaggerConfig) extends ApiClient(c val headerParams = new mutable.HashMap[String, String] if (status == null) throw new Exception("Missing required parameter 'status' when calling PetApi->findPetsByStatus") - - if (status != null) queryParams += "status" -> status.toString - - + queryParams += "status" -> status.toString val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => @@ -76,10 +73,7 @@ class PetApi(client: TransportClient, config: SwaggerConfig) extends ApiClient(c val headerParams = new mutable.HashMap[String, String] if (tags == null) throw new Exception("Missing required parameter 'tags' when calling PetApi->findPetsByTags") - - if (tags != null) queryParams += "tags" -> tags.toString - - + queryParams += "tags" -> tags.toString val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => @@ -97,7 +91,6 @@ class PetApi(client: TransportClient, config: SwaggerConfig) extends ApiClient(c val headerParams = new mutable.HashMap[String, String] - val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => process(reader.read(resp)) @@ -114,8 +107,6 @@ class PetApi(client: TransportClient, config: SwaggerConfig) extends ApiClient(c if (body == null) throw new Exception("Missing required parameter 'body' when calling PetApi->updatePet") - - val resFuture = client.submit("PUT", path, queryParams.toMap, headerParams.toMap, writer.write(body)) resFuture flatMap { resp => process(reader.read(resp)) @@ -135,7 +126,6 @@ class PetApi(client: TransportClient, config: SwaggerConfig) extends ApiClient(c val headerParams = new mutable.HashMap[String, String] - val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => process(reader.read(resp)) @@ -155,7 +145,6 @@ class PetApi(client: TransportClient, config: SwaggerConfig) extends ApiClient(c val headerParams = new mutable.HashMap[String, String] - val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => process(reader.read(resp)) diff --git a/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/api/StoreApi.scala b/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/api/StoreApi.scala index e17c641fcb2..22fb3706712 100644 --- a/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/api/StoreApi.scala +++ b/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/api/StoreApi.scala @@ -16,6 +16,7 @@ class StoreApi(client: TransportClient, config: SwaggerConfig) extends ApiClient val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] + if (orderId == null) throw new Exception("Missing required parameter 'orderId' when calling StoreApi->deleteOrder") val resFuture = client.submit("DELETE", path, queryParams.toMap, headerParams.toMap, "") @@ -33,7 +34,6 @@ class StoreApi(client: TransportClient, config: SwaggerConfig) extends ApiClient val headerParams = new mutable.HashMap[String, String] - val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => process(reader.read(resp)) @@ -50,7 +50,6 @@ class StoreApi(client: TransportClient, config: SwaggerConfig) extends ApiClient val headerParams = new mutable.HashMap[String, String] - val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => process(reader.read(resp)) @@ -67,8 +66,6 @@ class StoreApi(client: TransportClient, config: SwaggerConfig) extends ApiClient if (body == null) throw new Exception("Missing required parameter 'body' when calling StoreApi->placeOrder") - - val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) resFuture flatMap { resp => process(reader.read(resp)) diff --git a/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/api/UserApi.scala b/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/api/UserApi.scala index bf3294cb331..025c9f26925 100644 --- a/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/api/UserApi.scala +++ b/samples/client/petstore/async-scala/src/main/scala/io/swagger/client/api/UserApi.scala @@ -17,8 +17,6 @@ class UserApi(client: TransportClient, config: SwaggerConfig) extends ApiClient( if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUser") - - val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) resFuture flatMap { resp => process(reader.read(resp)) @@ -35,8 +33,6 @@ class UserApi(client: TransportClient, config: SwaggerConfig) extends ApiClient( if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUsersWithArrayInput") - - val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) resFuture flatMap { resp => process(reader.read(resp)) @@ -53,8 +49,6 @@ class UserApi(client: TransportClient, config: SwaggerConfig) extends ApiClient( if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->createUsersWithListInput") - - val resFuture = client.submit("POST", path, queryParams.toMap, headerParams.toMap, writer.write(body)) resFuture flatMap { resp => process(reader.read(resp)) @@ -70,6 +64,7 @@ class UserApi(client: TransportClient, config: SwaggerConfig) extends ApiClient( val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] + if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->deleteUser") val resFuture = client.submit("DELETE", path, queryParams.toMap, headerParams.toMap, "") @@ -87,6 +82,7 @@ class UserApi(client: TransportClient, config: SwaggerConfig) extends ApiClient( val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] + if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->getUserByName") val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") @@ -104,11 +100,12 @@ class UserApi(client: TransportClient, config: SwaggerConfig) extends ApiClient( val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] - if (username != null) queryParams += "username" -> username.toString - - if (password != null) queryParams += "password" -> password.toString + if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->loginUser") + if (password == null) throw new Exception("Missing required parameter 'password' when calling UserApi->loginUser") + queryParams += "username" -> username.toString + queryParams += "password" -> password.toString val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => @@ -125,7 +122,6 @@ class UserApi(client: TransportClient, config: SwaggerConfig) extends ApiClient( val headerParams = new mutable.HashMap[String, String] - val resFuture = client.submit("GET", path, queryParams.toMap, headerParams.toMap, "") resFuture flatMap { resp => process(reader.read(resp)) @@ -142,10 +138,10 @@ class UserApi(client: TransportClient, config: SwaggerConfig) extends ApiClient( val queryParams = new mutable.HashMap[String, String] val headerParams = new mutable.HashMap[String, String] + if (username == null) throw new Exception("Missing required parameter 'username' when calling UserApi->updateUser") + if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->updateUser") - - val resFuture = client.submit("PUT", path, queryParams.toMap, headerParams.toMap, writer.write(body)) resFuture flatMap { resp => process(reader.read(resp)) diff --git a/samples/client/petstore/async-scala/src/test/scala/Test.scala b/samples/client/petstore/async-scala/src/test/scala/Test.scala index 749772a2718..796c96ca822 100644 --- a/samples/client/petstore/async-scala/src/test/scala/Test.scala +++ b/samples/client/petstore/async-scala/src/test/scala/Test.scala @@ -1,18 +1,17 @@ -import io.swagger.client._ -import io.swagger.client.api._ -import io.swagger.client.model._ - import java.net.URI +import com.wordnik.swagger.client.ClientResponseReaders.Json4sFormatsReader._ +import com.wordnik.swagger.client.RequestWriters.Json4sFormatsWriter._ +import com.wordnik.swagger.client.SwaggerConfig +import io.swagger.client.SwaggerClient +import org.junit.runner.RunWith +import org.scalatest._ +import org.scalatest.junit.JUnitRunner + +import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent._ import scala.concurrent.duration._ import scala.util.{Failure, Success} -import org.scalatest._ -import org.junit.runner.RunWith -import org.scalatest.junit.JUnitRunner -import org.scalatest.FunSuite -import com.wordnik.swagger._ -com.wordnik.swagger.client @RunWith(classOf[JUnitRunner]) class SimpleTest extends FlatSpec with Matchers {