Skip to content

Commit

Permalink
dave - support for response codes
Browse files Browse the repository at this point in the history
  • Loading branch information
david denton committed Mar 8, 2015
1 parent 0012e0e commit 9ba406a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 37 deletions.
Expand Up @@ -2,14 +2,14 @@ package io.github.daviddenton.fintrospect

import com.twitter.finagle.http.path.Path
import io.github.daviddenton.fintrospect.parameters.RequestParameter
import org.jboss.netty.handler.codec.http.HttpMethod
import org.jboss.netty.handler.codec.http.{HttpMethod, HttpResponseStatus}

case class Description private(value: String, method: HttpMethod, params: List[RequestParameter[_]], complete: (Path => Path)) {
case class Description private(value: String, method: HttpMethod, params: List[RequestParameter[_]], responses: Map[HttpResponseStatus, String], complete: (Path => Path)) {
def requiring(rp: RequestParameter[_]) = copy(params = rp :: params)

def returning(codeAndDescription: (HttpResponseStatus, String)) = copy(responses = responses + codeAndDescription)
def matches(method: HttpMethod, rootPath: Path, path: Path) = method == this.method && path == complete(rootPath)
}

object Description {
def apply(value: String, method: HttpMethod, complete: (Path => Path)): Description = Description(value, method, Nil, complete)
def apply(value: String, method: HttpMethod, complete: (Path => Path)): Description = Description(value, method, Nil, Map.empty, complete)
}

This file was deleted.

Expand Up @@ -4,7 +4,7 @@ import com.twitter.finagle.http.path.Path
import io.github.daviddenton.fintrospect.parameters.PathParameter

class ModuleRoute(val description: Description, rootPath: Path, pathParams: Seq[PathParameter[_]]) {
val params = pathParams.flatten ++ description.params

val allParams = pathParams.flatten ++ description.params
val allResponses = description.responses
override def toString: String = (description.complete(rootPath).toString :: pathParams.map(_.toString()).toList).mkString("/")
}
Expand Up @@ -19,13 +19,14 @@ object Swagger1dot1Json {
)

private def render(r: ModuleRoute): (String, JsonNode) = {

r.description.method.getName.toLowerCase -> obj(
"httpMethod" -> string(r.description.method.getName),
"nickname" -> string(r.description.value),
"summary" -> string(r.description.value),
"produces" -> array(string("application/json")),
"parameters" -> array(r.params.map(render): _*),
"errorResponses" -> array(Seq[PathResponse]().map(r => obj("code" -> number(r.code.getCode), "description" -> string(r.description))))
"parameters" -> array(r.allParams.map(render): _*),
"errorResponses" -> array(r.allResponses.filterKeys(_.getCode > 399).map { case (code, desc) => obj("code" -> number(code.getCode), "description" -> string(desc))}.toSeq)
)
}

Expand Down
Expand Up @@ -7,7 +7,6 @@ import io.github.daviddenton.fintrospect._
import io.github.daviddenton.fintrospect.parameters.Parameter
import io.github.daviddenton.fintrospect.util.ArgoUtil._
import org.jboss.netty.handler.codec.http.HttpMethod
import org.jboss.netty.handler.codec.http.HttpResponseStatus.OK

object Swagger2dot0Json {

Expand All @@ -24,8 +23,8 @@ object Swagger2dot0Json {
r.description.method.getName.toLowerCase -> obj(
"summary" -> string(r.description.value),
"produces" -> array(string("application/json")),
"parameters" -> array(r.params.map(render): _*),
"responses" -> obj(Seq(PathResponse(OK, "")).map(r => r.code.getCode.toString -> obj("description" -> string(r.description)))),
"parameters" -> array(r.allParams.map(render): _*),
"responses" -> obj(r.allResponses.map { case (code, desc) => code.getCode.toString -> obj("description" -> string(desc))}),
"security" -> array(obj(Seq[Security]().map(_.toPathSecurity)))
)
}
Expand Down
Expand Up @@ -83,7 +83,12 @@
"dataType": "string"
}
],
"errorResponses": []
"errorResponses": [
{
"code": 403,
"description": "no way jose"
}
]
}
]
}
Expand Down
Expand Up @@ -32,11 +32,7 @@
"type": "boolean"
}
],
"responses": {
"200": {
"description": ""
}
},
"responses": {},
"security": [
{}
]
Expand All @@ -62,11 +58,7 @@
"type": "int"
}
],
"responses": {
"200": {
"description": ""
}
},
"responses": {},
"security": [
{}
]
Expand All @@ -92,7 +84,10 @@
],
"responses": {
"200": {
"description": ""
"description": "peachy"
},
"403": {
"description": "no way jose"
}
},
"security": [
Expand All @@ -101,4 +96,4 @@
}
}
}
}
}
Expand Up @@ -9,6 +9,7 @@ import io.github.daviddenton.fintrospect.parameters._
import io.github.daviddenton.fintrospect.util.ArgoUtil._
import io.github.daviddenton.fintrospect.{Description, FintrospectModule}
import org.jboss.netty.handler.codec.http.HttpMethod._
import org.jboss.netty.handler.codec.http.HttpResponseStatus._
import org.scalatest.{FunSpec, ShouldMatchers}
import util.Echo

Expand All @@ -18,13 +19,18 @@ abstract class JsonRendererTest(name: String, renderer: FintrospectModule.Render
describe(name) {
it("renders as expected") {
val module = FintrospectModule(Root, renderer)
.withRoute(Description("a get endpoint", GET, _ / "echo").requiring(Header.string("header")), string("message"), (s: String) => Echo(s))
.withRoute(
Description("a get endpoint", GET, _ / "echo")
.requiring(Header.string("header"))
.returning(OK -> "peachy")
.returning(FORBIDDEN -> "no way jose"),
string("message"), (s: String) => Echo(s))
.withRoute(Description("a post endpoint", POST, _ / "echo").requiring(Query.int("query")), string("message"), (s: String) => Echo(s))
.withRoute(Description("a friendly endpoint", GET, _ / "welcome").requiring(Query.boolean("query")), string("firstName"), fixed("bertrand"), string("secondName"), (x: String, y: String, z: String) => Echo(x, y, z))

val expected = parse(Source.fromInputStream(renderer.getClass.getResourceAsStream(s"$name.json")).mkString)
val actual = Await.result(module.toService(Request("/"))).content.toString(Utf8)
// println(actual)
// println(actual)
parse(actual) should be === expected
}
}
Expand Down

0 comments on commit 9ba406a

Please sign in to comment.