Skip to content

Commit 0ffc6cb

Browse files
committed
added header passing for codegen
1 parent 6a2f53f commit 0ffc6cb

File tree

10 files changed

+81
-29
lines changed

10 files changed

+81
-29
lines changed

src/main/scala/com/wordnik/swagger/codegen/BasicGenerator.scala

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,24 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
4848
throw new RuntimeException("Need url to resource listing as argument. You can also specify VM Argument -DfileMap=/path/to/folder/containing.resources.json/")
4949
}
5050
val host = args(0)
51-
val apiKey = {
52-
if (args.length > 1) Some("?api_key=" + args(1))
53-
else None
51+
val authorization = {
52+
Option (System.getProperty("header")) match {
53+
case Some(e) => {
54+
// this is ugly and will be replaced with proper arg parsing like in ScalaAsyncClientGenerator soon
55+
val authInfo = e.split(":")
56+
Some(ApiKeyValue(authInfo(0), "header", authInfo(1)))
57+
}
58+
case _ => {
59+
if (args.length > 1) {
60+
Some(ApiKeyValue("api_key", "query", args(1)))
61+
}
62+
else None
63+
}
64+
}
5465
}
5566
val doc = {
5667
try {
57-
ResourceExtractor.fetchListing(getResourcePath(host), apiKey)
68+
ResourceExtractor.fetchListing(getResourcePath(host), authorization)
5869
} catch {
5970
case e: Exception => throw new Exception("unable to read from " + host, e)
6071
}
@@ -66,7 +77,7 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
6677
val apiReferences = doc.apis
6778
if (apiReferences == null)
6879
throw new Exception("No APIs specified by resource")
69-
val apis = ApiExtractor.fetchApiListings(doc.swaggerVersion, basePath, apiReferences, apiKey)
80+
val apis = ApiExtractor.fetchApiListings(doc.swaggerVersion, basePath, apiReferences, authorization)
7081

7182
SwaggerSerializers.validationMessages.filter(_.level == ValidationMessage.ERROR).size match {
7283
case i: Int if i > 0 => {

src/main/scala/com/wordnik/swagger/codegen/BasicObjcGenerator.scala

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class BasicObjcGenerator extends BasicGenerator {
110110
responseClass match {
111111
case "void" => None
112112
case e: String => {
113+
println(responseClass)
113114
if(responseClass.toLowerCase.startsWith("array") || responseClass.toLowerCase.startsWith("list"))
114115
Some("NSArray")
115116
else
@@ -161,9 +162,10 @@ class BasicObjcGenerator extends BasicGenerator {
161162
}
162163

163164
override def toDeclaration(obj: ModelProperty) = {
165+
println("getting declaration for " + obj)
164166
var declaredType = toDeclaredType(obj.`type`)
165167
declaredType.toLowerCase match {
166-
case "List" => {
168+
case "list" => {
167169
declaredType = "array"
168170
}
169171
case e: String => e
@@ -183,7 +185,15 @@ class BasicObjcGenerator extends BasicGenerator {
183185
case _ => throw new Exception("no inner type defined")
184186
}
185187
}
186-
declaredType += "<" + inner + ">"
188+
"NSArray"
189+
}
190+
case "set" => {
191+
val inner = {
192+
obj.items match {
193+
case Some(items) => items.ref.getOrElse(items.`type`)
194+
case _ => throw new Exception("no inner type defined")
195+
}
196+
}
187197
"NSArray"
188198
}
189199
case _ =>

src/main/scala/com/wordnik/swagger/codegen/BasicScalaGenerator.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class BasicScalaGenerator extends BasicGenerator {
166166
case _ => throw new Exception("no inner type defined")
167167
}
168168
}
169-
val e = "List[%s]" format toDeclaredType(inner)
169+
val e = "List[%s]".format(toDeclaredType(inner))
170170
(e, toDefaultValue(inner, obj))
171171
}
172172
case "List" => {
@@ -176,7 +176,7 @@ class BasicScalaGenerator extends BasicGenerator {
176176
case _ => throw new Exception("no inner type defined")
177177
}
178178
}
179-
val e = "List[%s]" format toDeclaredType(inner)
179+
val e = "List[%s]".format(toDeclaredType(inner))
180180
(e, toDefaultValue(inner, obj))
181181
}
182182
case "Set" => {
@@ -186,7 +186,7 @@ class BasicScalaGenerator extends BasicGenerator {
186186
case _ => throw new Exception("no inner type defined")
187187
}
188188
}
189-
val e = "Set[%s]" format toDeclaredType(inner)
189+
val e = "Set[%s]".format(toDeclaredType(inner))
190190
(e, toDefaultValue(inner, obj))
191191
}
192192
case e: String => (toDeclaredType(e), toDefaultValue(e, obj))

src/main/scala/com/wordnik/swagger/codegen/ScalaAsyncClientGenerator.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,17 @@ class ScalaAsyncClientGenerator(cfg: SwaggerGenConfig) extends BasicGenerator {
317317
override def generateClient(args: Array[String]) = {
318318

319319
val host = cfg.api.resourceUrl
320-
val apiKey = cfg.api.apiKey map ("?api_key=" + _)
320+
val authorization = {
321+
val apiKey = cfg.api.apiKey
322+
if(apiKey != None)
323+
Some(ApiKeyValue("api_key", "query", apiKey.get))
324+
else
325+
None
326+
}
327+
321328
val doc = {
322329
try {
323-
ResourceExtractor.fetchListing(getResourcePath(host), apiKey)
330+
ResourceExtractor.fetchListing(getResourcePath(host), authorization)
324331
} catch {
325332
case e: Exception => throw new Exception("unable to read from " + host, e)
326333
}
@@ -331,7 +338,7 @@ class ScalaAsyncClientGenerator(cfg: SwaggerGenConfig) extends BasicGenerator {
331338
val apiReferences = doc.apis
332339
if (apiReferences == null)
333340
throw new Exception("No APIs specified by resource")
334-
val apis = ApiExtractor.fetchApiListings(doc.swaggerVersion, basePath, apiReferences, apiKey)
341+
val apis = ApiExtractor.fetchApiListings(doc.swaggerVersion, basePath, apiReferences, authorization)
335342

336343
new SwaggerSpecValidator(doc, apis).validate()
337344

src/main/scala/com/wordnik/swagger/codegen/spec/Validator.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ object Validator extends PathUtil {
2727
throw new RuntimeException("Need url to Resource Listing as argument. You can also specify VM Argument -DfileMap=/path/to/resourceListing")
2828
}
2929
val host = args(0)
30-
val apiKey = {
31-
if (args.length > 1) Some("?api_key=" + args(1))
32-
else None
30+
val authorization = {
31+
// if (args.length > 1) Some("?api_key=" + args(1))
32+
// else
33+
None
3334
}
3435

3536
val outputFilename = {
@@ -38,14 +39,14 @@ object Validator extends PathUtil {
3839
}
3940
val doc = {
4041
try {
41-
ResourceExtractor.fetchListing(getResourcePath(host), apiKey)
42+
ResourceExtractor.fetchListing(getResourcePath(host), authorization)
4243
} catch {
4344
case e: Exception => throw new Exception("unable to read from " + host, e)
4445
}
4546
}
4647

4748
val basePath = getBasePath(host, doc.basePath)
48-
val apis = ApiExtractor.fetchApiListings(doc.swaggerVersion, basePath, doc.apis, apiKey)
49+
val apis = ApiExtractor.fetchApiListings(doc.swaggerVersion, basePath, doc.apis, authorization)
4950
val swaggerSpecValidator = new SwaggerSpecValidator(doc, apis, false)
5051
swaggerSpecValidator.validate()
5152
swaggerSpecValidator.generateReport(host, outputFilename)

src/main/scala/com/wordnik/swagger/codegen/util/ApiExtractor.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@ import scala.io._
2828
import scala.collection.mutable.{ ListBuffer, HashMap, HashSet }
2929

3030
object ApiExtractor extends RemoteUrl {
31-
def fetchApiListings(version: String, basePath: String, apis: List[ApiListingReference], apiKey: Option[String] = None): List[ApiListing] = {
31+
def fetchApiListings(version: String, basePath: String, apis: List[ApiListingReference], authorization: Option[AuthorizationValue] = None): List[ApiListing] = {
3232
implicit val formats = SwaggerSerializers.formats(version)
3333
(for (api <- apis) yield {
3434
try{
3535
val json = (basePath.startsWith("http")) match {
3636
case true => {
3737
val path = if(api.path.startsWith("http")) api.path
3838
else basePath + api.path
39-
println("calling: " + ((path + apiKey.getOrElse("")).replaceAll(".\\{format\\}", ".json")))
40-
urlToString((path + apiKey.getOrElse("")).replaceAll(".\\{format\\}", ".json"))
39+
urlToString(path.replaceAll(".\\{format\\}", ".json"), authorization)
4140
}
4241
case false => Source.fromFile((basePath + api.path).replaceAll(".\\{format\\}", ".json")).mkString
4342
}
@@ -57,13 +56,12 @@ object ApiExtractor extends RemoteUrl {
5756
}).flatten.toList
5857
}
5958

60-
def extractApiOperations(version: String, basePath: String, references: List[ApiListingReference], apiKey: Option[String] = None) = {
59+
def extractApiOperations(version: String, basePath: String, references: List[ApiListingReference], authorization: Option[AuthorizationValue] = None) = {
6160
implicit val formats = SwaggerSerializers.formats(version)
6261
for (api <- references) yield {
6362
val json = basePath.startsWith("http") match {
6463
case true => {
65-
println("calling: " + ((basePath + api.path + apiKey.getOrElse("")).replaceAll(".\\{format\\}", ".json")))
66-
urlToString((basePath + api.path + apiKey.getOrElse("")).replaceAll(".\\{format\\}", ".json"))
64+
urlToString((basePath + api.path).replaceAll(".\\{format\\}", ".json"), authorization)
6765
}
6866
case false => Source.fromFile((basePath + api.path).replaceAll(".\\{format\\}", ".json")).mkString
6967
}

src/main/scala/com/wordnik/swagger/codegen/util/RemoteUrl.scala

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
package com.wordnik.swagger.codegen.util
22

3-
import java.net.URL
3+
import com.wordnik.swagger.model._
4+
5+
import java.net._
46
import java.io.InputStream
57

68
import scala.io.Source
79

810
trait RemoteUrl {
9-
def urlToString(url: String): String = {
11+
def urlToString(url: String, authorization: Option [AuthorizationValue]): String = {
1012
var is: InputStream = null
1113
try{
12-
val conn = new URL(url).openConnection()
14+
val conn: URLConnection = authorization match {
15+
case Some(auth: ApiKeyValue) => {
16+
if(auth.passAs == "header") {
17+
val connection = new URL(url).openConnection()
18+
connection.setRequestProperty(auth.keyName, auth.value)
19+
connection
20+
}
21+
else if(auth.passAs == "query") {
22+
new URL(url + "?%s=%s".format(URLEncoder.encode(auth.keyName), URLEncoder.encode(auth.value))).openConnection()
23+
}
24+
else {
25+
new URL(url).openConnection()
26+
}
27+
}
28+
case None => new URL(url).openConnection()
29+
}
30+
println(conn)
1331
is = conn.getInputStream()
1432
Source.fromInputStream(is).mkString
1533
}

src/main/scala/com/wordnik/swagger/codegen/util/ResourceExtractor.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import org.json4s.jackson.Serialization.read
2525
import scala.io._
2626

2727
object ResourceExtractor extends RemoteUrl {
28-
def fetchListing(path: String, apiKey: Option[String] = None): ResourceListing = {
28+
def fetchListing(path: String, authorization: Option[AuthorizationValue] = None): ResourceListing = {
2929
val json = path.startsWith("http") match {
30-
case true => urlToString(path + apiKey.getOrElse(""))
30+
case true => urlToString(path, authorization)
3131
case false => Source.fromFile(path).mkString
3232
}
3333

src/main/scala/com/wordnik/swagger/model/AuthorizationModels.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ case class AuthorizationCodeGrant(
4141
tokenEndpoint: TokenEndpoint) extends GrantType {
4242
def `type` = "authorization_code"
4343
}
44+
45+
trait AuthorizationValue
46+
case class ApiKeyValue(keyName: String, passAs: String, value: String) extends AuthorizationValue

src/test/scala/BasicJavaGeneratorTest.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class BasicJavaGeneratorTest extends FlatSpec with ShouldMatchers {
5353
config.processResponseDeclaration("array[String]") should be (Some("List<String>"))
5454
}
5555

56+
it should "process an upper-case string array" in {
57+
config.processResponseDeclaration("Array[String]") should be (Some("List<String>"))
58+
}
59+
5660
/*
5761
* swagger int is turned into scala Int
5862
*/

0 commit comments

Comments
 (0)