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

[Scala] Fixed bug for handling optional header parameters #3776

Merged
merged 5 commits into from
Sep 15, 2016
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
23 changes: 17 additions & 6 deletions modules/swagger-codegen/src/main/resources/asyncscala/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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}})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 =>
Expand All @@ -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 =>
Expand All @@ -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 =>
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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, "")
Expand All @@ -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, "")
Expand All @@ -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 =>
Expand All @@ -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))
Expand All @@ -142,9 +138,9 @@ class UserApi(client: TransportClient, config: SwaggerConfig) extends ApiClient(
val queryParams = new mutable.HashMap[String, String]
val headerParams = new mutable.HashMap[String, String]

if (body == null) throw new Exception("Missing required parameter 'body' when calling UserApi->updateUser")

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 =>
Expand Down
19 changes: 9 additions & 10 deletions samples/client/petstore/async-scala/src/test/scala/Test.scala
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down