Skip to content

Commit

Permalink
Merge pull request savaki#2 from exaring/master
Browse files Browse the repository at this point in the history
Added support for non struct response types.
  • Loading branch information
savaki authored Apr 4, 2017
2 parents d8e708a + 9acb23c commit 1ecab7a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions swagger/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Object struct {
GoType reflect.Type `json:"-"`
Name string `json:"-"`
Type string `json:"type"`
Format string `json:"format,omitempty"`
Required []string `json:"required,omitempty"`
Properties map[string]Property `json:"properties,omitempty"`
}
Expand Down
12 changes: 12 additions & 0 deletions swagger/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ func defineObject(v interface{}) Object {
t = t.Elem()
}

if t.Kind() != reflect.Struct {
p := inspect(t, "")
return Object{
IsArray: isArray,
GoType: t,
Type: p.Type,
Format: p.Format,
Name: t.Kind().String(),
Required: required,
}
}

for i := 0; i < t.NumField(); i++ {
field := t.Field(i)

Expand Down
42 changes: 42 additions & 0 deletions swagger/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"fmt"
)

type Person struct {
Expand Down Expand Up @@ -65,6 +66,47 @@ func TestDefine(t *testing.T) {
}
}

func TestNotStructDefine(t *testing.T) {
v := define(int32(1))
obj, ok := v["int32"]
assert.True(t, ok)
assert.False(t, obj.IsArray)
assert.Equal(t, "integer", obj.Type )
assert.Equal(t, "int32", obj.Format)

v = define(uint64(1))
obj, ok = v["uint64"]
assert.True(t, ok)
assert.False(t, obj.IsArray)
assert.Equal(t, "integer", obj.Type )
assert.Equal(t, "int64", obj.Format)

v = define("")
obj, ok = v["string"]
assert.True(t, ok)
assert.False(t, obj.IsArray)
assert.Equal(t, "string", obj.Type )
assert.Equal(t, "", obj.Format)

v = define(byte(1))
obj, ok = v["uint8"]
if !assert.True(t, ok) {
fmt.Printf("%v", v)
}
assert.False(t, obj.IsArray)
assert.Equal(t, "integer", obj.Type )
assert.Equal(t, "int32", obj.Format)

v = define([]byte{1,2})
obj, ok = v["uint8"]
if !assert.True(t, ok) {
fmt.Printf("%v", v)
}
assert.True(t, obj.IsArray)
assert.Equal(t, "integer", obj.Type )
assert.Equal(t, "int32", obj.Format)
}

func TestHonorJsonIgnore(t *testing.T) {
v := define(Empty{})
obj, ok := v["swaggerEmpty"]
Expand Down

0 comments on commit 1ecab7a

Please sign in to comment.