Skip to content

Commit

Permalink
Merge pull request #39 from zc2638/feat/body
Browse files Browse the repository at this point in the history
add bodyR to simplify body usage
  • Loading branch information
zc2638 authored Dec 22, 2023
2 parents 958a34b + 66839db commit a206f70
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ func FormData(name string, typ types.ParameterType, description string, required
}
}

// BodyR 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 BodyR(prototype interface{}) Option {
return bodyType(reflect.TypeOf(prototype), "", true)
}

// 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 {
Expand Down
21 changes: 21 additions & 0 deletions endpoint/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,27 @@ type Model struct {
String string `json:"s"`
}

func TestBodyR(t *testing.T) {
expected := swag.Parameter{
In: "body",
Name: "body",
Required: true,
Schema: &swag.Schema{
Ref: "#/definitions/endpoint.Model",
Prototype: reflect.TypeOf(Model{}),
},
}

e := New(
"get", "/",
Summary("get thing"),
BodyR(Model{}),
)

assert.Equal(t, 1, len(e.Parameters))
assert.Equal(t, expected, e.Parameters[0])
}

func TestBody(t *testing.T) {
expected := swag.Parameter{
In: "body",
Expand Down

0 comments on commit a206f70

Please sign in to comment.