Updating go-restful to include fix for emicklei/go-restful#185

pull/6/head
nikhiljindal 2015-03-16 23:30:58 -07:00
parent c04ceec27f
commit 09224901d0
6 changed files with 104 additions and 40 deletions

4
Godeps/Godeps.json generated
View File

@ -151,8 +151,8 @@
},
{
"ImportPath": "github.com/emicklei/go-restful",
"Comment": "v1.1.3-9-g49e08a0",
"Rev": "49e08a07d31e2dae8005f28b5360a96746cd6365"
"Comment": "v1.1.3-10-g62dc65d",
"Rev": "62dc65d6e51525418cad2bb6f292d3cf7c5e9d0a"
},
{
"ImportPath": "github.com/evanphx/json-patch",

View File

@ -0,0 +1,32 @@
package swagger
import "github.com/emicklei/go-restful"
type orderedRouteMap struct {
elements map[string][]restful.Route
keys []string
}
func newOrderedRouteMap() *orderedRouteMap {
return &orderedRouteMap{
elements: map[string][]restful.Route{},
keys: []string{},
}
}
func (o *orderedRouteMap) Add(key string, route restful.Route) {
routes, ok := o.elements[key]
if ok {
routes = append(routes, route)
o.elements[key] = routes
return
}
o.elements[key] = []restful.Route{route}
o.keys = append(o.keys, key)
}
func (o *orderedRouteMap) Do(block func(key string, routes []restful.Route)) {
for _, k := range o.keys {
block(k, o.elements[k])
}
}

View File

@ -0,0 +1,29 @@
package swagger
import (
"testing"
"github.com/emicklei/go-restful"
)
// go test -v -test.run TestOrderedRouteMap ...swagger
func TestOrderedRouteMap(t *testing.T) {
m := newOrderedRouteMap()
r1 := restful.Route{Path: "/r1"}
r2 := restful.Route{Path: "/r2"}
m.Add("a", r1)
m.Add("b", r2)
m.Add("b", r1)
m.Add("d", r2)
m.Add("c", r2)
order := ""
m.Do(func(k string, routes []restful.Route) {
order += k
if len(routes) == 0 {
t.Fail()
}
})
if order != "abdc" {
t.Fail()
}
}

View File

@ -1,26 +1,26 @@
package swagger
import (
"encoding/json"
"fmt"
"testing"
"github.com/emicklei/go-restful"
)
// go test -v -test.run TestApi ...swagger
func TestApi(t *testing.T) {
value := Api{Path: "/", Description: "Some Path", Operations: []Operation{}}
compareJson(t, true, value, `{"path":"/","description":"Some Path"}`)
}
// go test -v -test.run TestServiceToApi ...swagger
func TestServiceToApi(t *testing.T) {
ws := new(restful.WebService)
ws.Path("/tests")
ws.Consumes(restful.MIME_JSON)
ws.Produces(restful.MIME_XML)
ws.Route(ws.GET("/all").To(dummy).Writes(sample{}))
ws.Route(ws.GET("/a").To(dummy).Writes(sample{}))
ws.Route(ws.PUT("/b").To(dummy).Writes(sample{}))
ws.Route(ws.POST("/c").To(dummy).Writes(sample{}))
ws.Route(ws.DELETE("/d").To(dummy).Writes(sample{}))
ws.Route(ws.GET("/d").To(dummy).Writes(sample{}))
ws.Route(ws.PUT("/c").To(dummy).Writes(sample{}))
ws.Route(ws.POST("/b").To(dummy).Writes(sample{}))
ws.Route(ws.DELETE("/a").To(dummy).Writes(sample{}))
ws.ApiVersion("1.2.3")
cfg := Config{
WebServicesUrl: "http://here.com",
@ -28,12 +28,26 @@ func TestServiceToApi(t *testing.T) {
WebServices: []*restful.WebService{ws}}
sws := newSwaggerService(cfg)
decl := sws.composeDeclaration(ws, "/tests")
data, err := json.MarshalIndent(decl, " ", " ")
if err != nil {
t.Fatal(err.Error())
// checks
if decl.ApiVersion != "1.2.3" {
t.Errorf("got %v want %v", decl.ApiVersion, "1.2.3")
}
if decl.BasePath != "http://here.com" {
t.Errorf("got %v want %v", decl.BasePath, "http://here.com")
}
if len(decl.Apis) != 4 {
t.Errorf("got %v want %v", len(decl.Apis), 4)
}
pathOrder := ""
for _, each := range decl.Apis {
pathOrder += each.Path
for _, other := range each.Operations {
pathOrder += other.Method
}
}
if pathOrder != "/tests/aGETDELETE/tests/bPUTPOST/tests/cPOSTPUT/tests/dDELETEGET" {
t.Errorf("got %v want %v", pathOrder, "see test source")
}
// for visual inspection only
fmt.Println(string(data))
}
func dummy(i *restful.Request, o *restful.Response) {}

View File

@ -180,14 +180,13 @@ func (sws SwaggerService) composeDeclaration(ws *restful.WebService, pathPrefix
rootParams = append(rootParams, asSwaggerParameter(param.Data()))
}
// aggregate by path
pathToRoutes := map[string][]restful.Route{}
pathToRoutes := newOrderedRouteMap()
for _, other := range ws.Routes() {
if strings.HasPrefix(other.Path, pathPrefix) {
routes := pathToRoutes[other.Path]
pathToRoutes[other.Path] = append(routes, other)
pathToRoutes.Add(other.Path, other)
}
}
for path, routes := range pathToRoutes {
pathToRoutes.Do(func(path string, routes []restful.Route) {
api := Api{Path: strings.TrimSuffix(path, "/"), Description: ws.Documentation()}
for _, route := range routes {
operation := Operation{
@ -217,7 +216,7 @@ func (sws SwaggerService) composeDeclaration(ws *restful.WebService, pathPrefix
api.Operations = append(api.Operations, operation)
}
decl.Apis = append(decl.Apis, api)
}
})
return decl
}

View File

@ -9,8 +9,10 @@ import (
"testing"
)
func testJsonFromStruct(t *testing.T, sample interface{}, expectedJson string) {
compareJson(t, false, modelsFromStruct(sample), expectedJson)
func testJsonFromStruct(t *testing.T, sample interface{}, expectedJson string) bool {
m := modelsFromStruct(sample)
data, _ := json.MarshalIndent(m, " ", " ")
return compareJson(t, string(data), expectedJson)
}
func modelsFromStruct(sample interface{}) map[string]Model {
@ -20,32 +22,20 @@ func modelsFromStruct(sample interface{}) map[string]Model {
return models
}
func compareJson(t *testing.T, flatCompare bool, value interface{}, expectedJsonAsString string) {
var output []byte
var err error
if flatCompare {
output, err = json.Marshal(value)
} else {
output, err = json.MarshalIndent(value, " ", " ")
}
if err != nil {
t.Error(err.Error())
return
}
actual := string(output)
func compareJson(t *testing.T, actualJsonAsString string, expectedJsonAsString string) bool {
var actualMap map[string]interface{}
json.Unmarshal([]byte(actualJsonAsString), &actualMap)
var expectedMap map[string]interface{}
json.Unmarshal(output, &actualMap)
json.Unmarshal([]byte(expectedJsonAsString), &expectedMap)
if !reflect.DeepEqual(actualMap, expectedMap) {
fmt.Println("---- expected -----")
fmt.Println(withLineNumbers(expectedJsonAsString))
fmt.Println("---- actual -----")
fmt.Println(withLineNumbers(actual))
fmt.Println("---- raw -----")
fmt.Println(actual)
fmt.Println(withLineNumbers(actualJsonAsString))
t.Error("there are differences")
return false
}
return true
}
func indexOfNonMatchingLine(actual, expected string) int {