Skip to content

Commit 3a75479

Browse files
committed
- Added support for specifying both the body and the response via reflect.Type rather than an instance of the type. Supports cases where one wants to generate a swagger interface via reflection.
1 parent d9b3074 commit 3a75479

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

endpoint/builder.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package endpoint
1616

1717
import (
1818
"net/http"
19+
"reflect"
1920
"strconv"
2021
"strings"
2122

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

114115
// Body defines a body parameter for the swagger endpoint as would commonly be used for the POST, PUT, and PATCH methods
115116
// prototype should be a struct or a pointer to struct that swag can use to reflect upon the return type
116-
func Body(prototype interface{}, description string, required bool) Option {
117+
// t represents the Type of the body
118+
func BodyType(t reflect.Type, description string, required bool) Option {
117119
p := swagger.Parameter{
118120
In: "body",
119121
Name: "body",
120122
Description: description,
121-
Schema: swagger.MakeSchema(prototype),
123+
Schema: swagger.MakeSchema(t),
122124
Required: required,
123125
}
124126
return parameter(p)
125127
}
126128

129+
// Body defines a body parameter for the swagger endpoint as would commonly be used for the POST, PUT, and PATCH methods
130+
// prototype should be a struct or a pointer to struct that swag can use to reflect upon the return type
131+
func Body(prototype interface{}, description string, required bool) Option {
132+
return BodyType(reflect.TypeOf(prototype), description, required)
133+
}
134+
127135
// Tags allows one or more tags to be associated with the endpoint
128136
func Tags(tags ...string) Option {
129137
return func(b *Builder) {
@@ -180,16 +188,17 @@ func Header(name, typ, format, description string) ResponseOption {
180188
}
181189
}
182190

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

190199
r := swagger.Response{
191200
Description: description,
192-
Schema: swagger.MakeSchema(prototype),
201+
Schema: swagger.MakeSchema(t),
193202
}
194203

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

212+
// Response sets the endpoint response for the specified code; may be used multiple times with different status codes
213+
func Response(code int, prototype interface{}, description string, opts ...ResponseOption) Option {
214+
return ResponseType(code, reflect.TypeOf(prototype), description, opts...)
215+
}
216+
203217
// New constructs a new swagger endpoint using the fields and functional options provided
204218
func New(method, path, summary string, options ...Option) *swagger.Endpoint {
205219
method = strings.ToUpper(method)

endpoint/builder_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"net/http"
2020
"testing"
2121

22+
"reflect"
23+
2224
"github.com/savaki/swag"
2325
"github.com/savaki/swag/endpoint"
2426
"github.com/savaki/swag/swagger"
@@ -132,7 +134,7 @@ func TestBody(t *testing.T) {
132134
Required: true,
133135
Schema: &swagger.Schema{
134136
Ref: "#/definitions/endpoint_testModel",
135-
Prototype: Model{},
137+
Prototype: reflect.TypeOf(Model{}),
136138
},
137139
}
138140

@@ -149,7 +151,7 @@ func TestResponse(t *testing.T) {
149151
Description: "successful",
150152
Schema: &swagger.Schema{
151153
Ref: "#/definitions/endpoint_testModel",
152-
Prototype: Model{},
154+
Prototype: reflect.TypeOf(Model{}),
153155
},
154156
}
155157

@@ -166,7 +168,7 @@ func TestResponseHeader(t *testing.T) {
166168
Description: "successful",
167169
Schema: &swagger.Schema{
168170
Ref: "#/definitions/endpoint_testModel",
169-
Prototype: Model{},
171+
Prototype: reflect.TypeOf(Model{}),
170172
},
171173
Headers: map[string]swagger.Header{
172174
"X-Rate-Limit": {

0 commit comments

Comments
 (0)