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

feat: support json array in request body #4444

Merged
merged 1 commit into from
Nov 5, 2024
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
20 changes: 12 additions & 8 deletions rest/httpx/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httpx
import (
"io"
"net/http"
"reflect"
"strings"
"sync/atomic"

Expand Down Expand Up @@ -43,16 +44,19 @@ type Validator interface {

// Parse parses the request.
func Parse(r *http.Request, v any) error {
if err := ParsePath(r, v); err != nil {
return err
}
kind := mapping.Deref(reflect.TypeOf(v)).Kind()
if kind != reflect.Array && kind != reflect.Slice {
if err := ParsePath(r, v); err != nil {
return err
}

if err := ParseForm(r, v); err != nil {
return err
}
if err := ParseForm(r, v); err != nil {
return err
}

if err := ParseHeaders(r, v); err != nil {
return err
if err := ParseHeaders(r, v); err != nil {
return err
}
}

if err := ParseJsonBody(r, v); err != nil {
Expand Down
22 changes: 21 additions & 1 deletion rest/httpx/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,31 @@ func TestParseJsonBody(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body))
r.Header.Set(ContentType, header.JsonContentType)

assert.NoError(t, ParseJsonBody(r, &v))
assert.NoError(t, Parse(r, &v))
assert.Equal(t, 1, len(v))
assert.Equal(t, "kevin", v[0].Name)
assert.Equal(t, 18, v[0].Age)
})

t.Run("form and array body", func(t *testing.T) {
var v []struct {
// we can only ignore the form tag,
// because if the value is a slice, it should be in the body,
// but it's hard to detect when we treat it as a json body.
Product string `form:"product"`
Name string `json:"name"`
Age int `json:"age"`
}

body := `[{"name":"apple", "age": 18}]`
r := httptest.NewRequest(http.MethodPost, "/a?product=tree", strings.NewReader(body))
r.Header.Set(ContentType, header.JsonContentType)

assert.NoError(t, Parse(r, &v))
assert.Equal(t, 1, len(v))
assert.Equal(t, "apple", v[0].Name)
assert.Equal(t, 18, v[0].Age)
})
}

func TestParseRequired(t *testing.T) {
Expand Down