Skip to content

Commit

Permalink
- Added support for specifying both the body and the response via ref…
Browse files Browse the repository at this point in the history
…lect.Type rather than an instance of the type. Supports cases where one wants to generate a swagger interface via reflection.
  • Loading branch information
savaki committed Jul 22, 2017
1 parent d9b3074 commit 3a75479
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
24 changes: 19 additions & 5 deletions endpoint/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package endpoint

import (
"net/http"
"reflect"
"strconv"
"strings"

Expand Down Expand Up @@ -113,17 +114,24 @@ func Query(name, typ, description string, required bool) Option {

// Body defines a body parameter for the swagger endpoint as would commonly be used for the POST, PUT, and PATCH methods
// prototype should be a struct or a pointer to struct that swag can use to reflect upon the return type
func Body(prototype interface{}, description string, required bool) Option {
// t represents the Type of the body
func BodyType(t reflect.Type, description string, required bool) Option {
p := swagger.Parameter{
In: "body",
Name: "body",
Description: description,
Schema: swagger.MakeSchema(prototype),
Schema: swagger.MakeSchema(t),
Required: required,
}
return parameter(p)
}

// Body defines a body parameter for the swagger endpoint as would commonly be used for the POST, PUT, and PATCH methods
// prototype should be a struct or a pointer to struct that swag can use to reflect upon the return type
func Body(prototype interface{}, description string, required bool) Option {
return BodyType(reflect.TypeOf(prototype), description, required)
}

// Tags allows one or more tags to be associated with the endpoint
func Tags(tags ...string) Option {
return func(b *Builder) {
Expand Down Expand Up @@ -180,16 +188,17 @@ func Header(name, typ, format, description string) ResponseOption {
}
}

// Response sets the endpoint response for the specified code; may be used multiple times with different status codes
func Response(code int, prototype interface{}, description string, opts ...ResponseOption) Option {
// ResponseType sets the endpoint response for the specified code; may be used multiple times with different status codes
// t represents the Type of the response
func ResponseType(code int, t reflect.Type, description string, opts ...ResponseOption) Option {
return func(b *Builder) {
if b.Endpoint.Responses == nil {
b.Endpoint.Responses = map[string]swagger.Response{}
}

r := swagger.Response{
Description: description,
Schema: swagger.MakeSchema(prototype),
Schema: swagger.MakeSchema(t),
}

for _, opt := range opts {
Expand All @@ -200,6 +209,11 @@ func Response(code int, prototype interface{}, description string, opts ...Respo
}
}

// Response sets the endpoint response for the specified code; may be used multiple times with different status codes
func Response(code int, prototype interface{}, description string, opts ...ResponseOption) Option {
return ResponseType(code, reflect.TypeOf(prototype), description, opts...)
}

// New constructs a new swagger endpoint using the fields and functional options provided
func New(method, path, summary string, options ...Option) *swagger.Endpoint {
method = strings.ToUpper(method)
Expand Down
8 changes: 5 additions & 3 deletions endpoint/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"net/http"
"testing"

"reflect"

"github.com/savaki/swag"
"github.com/savaki/swag/endpoint"
"github.com/savaki/swag/swagger"
Expand Down Expand Up @@ -132,7 +134,7 @@ func TestBody(t *testing.T) {
Required: true,
Schema: &swagger.Schema{
Ref: "#/definitions/endpoint_testModel",
Prototype: Model{},
Prototype: reflect.TypeOf(Model{}),
},
}

Expand All @@ -149,7 +151,7 @@ func TestResponse(t *testing.T) {
Description: "successful",
Schema: &swagger.Schema{
Ref: "#/definitions/endpoint_testModel",
Prototype: Model{},
Prototype: reflect.TypeOf(Model{}),
},
}

Expand All @@ -166,7 +168,7 @@ func TestResponseHeader(t *testing.T) {
Description: "successful",
Schema: &swagger.Schema{
Ref: "#/definitions/endpoint_testModel",
Prototype: Model{},
Prototype: reflect.TypeOf(Model{}),
},
Headers: map[string]swagger.Header{
"X-Rate-Limit": {
Expand Down

0 comments on commit 3a75479

Please sign in to comment.