Update dependency emicklei/go-restful

This commit is contained in:
Paul Morie 2015-05-05 12:15:07 -04:00
parent 74c07f1094
commit 41e9e53c64
3 changed files with 169 additions and 14 deletions

4
Godeps/Godeps.json generated
View File

@ -250,8 +250,8 @@
},
{
"ImportPath": "github.com/emicklei/go-restful",
"Comment": "v1.1.3-40-g4f30cbd",
"Rev": "4f30cbd5bd858a523d8fe9bd484f44513f50eeec"
"Comment": "v1.1.3-45-gd487287",
"Rev": "d4872876992d385f0e69b007f154e5633bdb40af"
},
{
"ImportPath": "github.com/evanphx/json-patch",

View File

@ -197,9 +197,13 @@ func (b modelBuilder) buildStructTypeProperty(field reflect.StructField, jsonNam
if required {
model.Required = append(model.Required, k)
}
// Add the model type to the global model list
if v.Ref != nil {
b.Models[*v.Ref] = sub.Models[*v.Ref]
}
// add all new referenced models
for key, sub := range sub.Models {
if key != subKey {
if _, ok := b.Models[key]; !ok {
b.Models[key] = sub
}
}
}
// empty name signals skip property
@ -245,7 +249,12 @@ func (b modelBuilder) buildPointerTypeProperty(field reflect.StructField, jsonNa
b.addModel(fieldType.Elem().Elem(), elemName)
} else {
// non-array, pointer type
var pType = fieldType.String()[1:] // no star, include pkg path
var pType = b.jsonSchemaType(fieldType.String()[1:]) // no star, include pkg path
if b.isPrimitiveType(fieldType.String()[1:]) {
prop.Type = &pType
prop.Format = b.jsonSchemaFormat(fieldType.String()[1:])
return jsonName, prop
}
prop.Ref = &pType
elemName := ""
if fieldType.Elem().Name() == "" {
@ -328,14 +337,15 @@ func (b modelBuilder) jsonSchemaType(modelName string) string {
func (b modelBuilder) jsonSchemaFormat(modelName string) string {
schemaMap := map[string]string{
"int": "int32",
"int32": "int32",
"int64": "int64",
"byte": "byte",
"uint8": "byte",
"float64": "double",
"float32": "float",
"time.Time": "date-time",
"int": "int32",
"int32": "int32",
"int64": "int64",
"byte": "byte",
"uint8": "byte",
"float64": "double",
"float32": "float",
"time.Time": "date-time",
"*time.Time": "date-time",
}
mapped, ok := schemaMap[modelName]
if !ok {

View File

@ -84,6 +84,49 @@ func TestPrimitiveTypes(t *testing.T) {
}`)
}
// clear && go test -v -test.run TestPrimitivePtrTypes ...swagger
func TestPrimitivePtrTypes(t *testing.T) {
type Prims struct {
f *float64
t *time.Time
b *bool
s *string
i *int
}
testJsonFromStruct(t, Prims{}, `{
"swagger.Prims": {
"id": "swagger.Prims",
"required": [
"f",
"t",
"b",
"s",
"i"
],
"properties": {
"b": {
"type": "boolean"
},
"f": {
"type": "number",
"format": "double"
},
"i": {
"type": "integer",
"format": "int32"
},
"s": {
"type": "string"
},
"t": {
"type": "string",
"format": "date-time"
}
}
}
}`)
}
// clear && go test -v -test.run TestS1 ...swagger
func TestS1(t *testing.T) {
type S1 struct {
@ -702,6 +745,52 @@ func TestEmbeddedStructA5(t *testing.T) {
}`)
}
type D2 struct {
id int
D []D
}
type A6 struct {
D2 "json:,inline"
}
// clear && go test -v -test.run TestStructA4 ...swagger
func TestEmbeddedStructA6(t *testing.T) {
testJsonFromStruct(t, A6{}, `{
"swagger.A6": {
"id": "swagger.A6",
"required": [
"id",
"D"
],
"properties": {
"D": {
"type": "array",
"items": {
"$ref": "swagger.D"
}
},
"id": {
"type": "integer",
"format": "int32"
}
}
},
"swagger.D": {
"id": "swagger.D",
"required": [
"Id"
],
"properties": {
"Id": {
"type": "integer",
"format": "int32"
}
}
}
}`)
}
type ObjectId []byte
type Region struct {
@ -833,3 +922,59 @@ func TestSlices(t *testing.T) {
}
}
type Name struct {
Value string
}
func (n Name) PostBuildModel(m *Model) *Model {
m.Description = "titles must be upcase"
return m
}
type TOC struct {
Titles []Name
}
type Discography struct {
Title Name
TOC
}
// clear && go test -v -test.run TestEmbeddedStructPull204 ...swagger
func TestEmbeddedStructPull204(t *testing.T) {
b := Discography{}
testJsonFromStruct(t, b, `
{
"swagger.Discography": {
"id": "swagger.Discography",
"required": [
"Title",
"Titles"
],
"properties": {
"Title": {
"$ref": "swagger.Name"
},
"Titles": {
"type": "array",
"items": {
"$ref": "swagger.Name"
}
}
}
},
"swagger.Name": {
"id": "swagger.Name",
"required": [
"Value"
],
"properties": {
"Value": {
"type": "string"
}
}
}
}
`)
}