Skip to content

Commit

Permalink
Fixing generation of string default parameters (#145)
Browse files Browse the repository at this point in the history
* fixing generation of string default parameters

Fixes #134

* writing more tests for default parameter value
  • Loading branch information
Moises Trovo authored and kailuowang committed Mar 14, 2017
1 parent e62433b commit 4962ace
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ object SwaggerParameterMapper {
case ci"Int" | ci"Long" JsNumber(value.toLong)
case ci"Double" | ci"Float" | ci"BigDecimal" JsNumber(value.toDouble)
case ci"Boolean" JsBoolean(value.toBoolean)
case _ JsString(value)
case ci"String" {
val noquotes = value match {
case c if c.startsWith("\"\"\"") && c.endsWith("\"\"\"") c.substring(3, c.length - 3)
case c if c.startsWith("\"") && c.endsWith("\"") c.substring(1, c.length - 1)
case c c
}
JsString(noquotes)
}
case _ JsString(value)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion core/src/test/resources/students.routes
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
###
GET /:name com.iheart.controllers.Students.get(name)

PUT /defaultValueParam com.iheart.controllers.DefaultValueParam.put(aFlag:Boolean ?= true)
PUT /defaultValueParam com.iheart.controllers.DefaultValueParam.put(aFlag:Boolean ?= true)
PUT /defaultValueParamString com.iheart.controllers.DefaultValueParam.put(strFlag:String ?= "defaultValue")
PUT /defaultValueParamString3 com.iheart.controllers.DefaultValueParam.put(strFlag:String ?= """defaultValue with triple quotes""")

Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,31 @@ class SwaggerParameterMapperSpec extends Specification {
parameter.asInstanceOf[GenSwaggerParameter].`type` must beSome("string")
parameter.asInstanceOf[GenSwaggerParameter].format must beNone
}

"map default value to content without quotes when provided with string without quotes" >> {
mapParam(Parameter("strField", "String", None, Some("defaultValue"))) === GenSwaggerParameter(
name = "strField",
`type` = Option("string"),
required = false,
default = Option(JsString("defaultValue"))
)
}
"map default value to content without quotes when provided with string with simple quotes" >> {
mapParam(Parameter("strField", "String", None, Some("\"defaultValue\""))) === GenSwaggerParameter(
name = "strField",
`type` = Option("string"),
required = false,
default = Option(JsString("defaultValue"))
)
}
"map default value to content without quotes when provided with string with triple quotes" >> {
mapParam(Parameter("strField", "String", None, Some("\"\"\"defaultValue\"\"\""))) === GenSwaggerParameter(
name = "strField",
`type` = Option("string"),
required = false,
default = Option(JsString("defaultValue"))
)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,49 @@ class SwaggerSpecGeneratorIntegrationSpec extends Specification {
}
}

"parse param with default triple quoted string value as optional field" >> {
val endPointJson = (pathJson \ "/api/students/defaultValueParamString3" \ "put").asOpt[JsObject]
endPointJson must beSome[JsObject]

val paramJson: JsValue = parametersOf(endPointJson.get).head

(paramJson \ "name").as[String] === "strFlag"

"set required as false" >> {
(paramJson \ "required").as[Boolean] === false
}

"set in as query" >> {
(paramJson \ "in").as[String] === "query"
}

"set default value" >> {
(paramJson \ "default").as[String] === """defaultValue with triple quotes"""
}
}

"parse param with default string value as optional field" >> {
val endPointJson = (pathJson \ "/api/students/defaultValueParamString" \ "put").asOpt[JsObject]
endPointJson must beSome[JsObject]

val paramJson: JsValue = parametersOf(endPointJson.get).head

(paramJson \ "name").as[String] === "strFlag"

"set required as false" >> {
(paramJson \ "required").as[Boolean] === false
}

"set in as query" >> {
(paramJson \ "in").as[String] === "query"
}

"set default value" >> {

(paramJson \ "default").as[String] === "defaultValue"
}
}

"should contain schemas in responses" >> {
(postBodyJson \ "responses" \ "200" \ "schema" \ "$ref").asOpt[String] === Some("#/definitions/com.iheart.playSwagger.FooWithSeq2")
}
Expand Down

0 comments on commit 4962ace

Please sign in to comment.