Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bodyR to simplify body usage #39

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading