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

fix: [#528] GraphQL request causing errors from request body parsing #108

Merged
merged 1 commit into from
Dec 2, 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
12 changes: 7 additions & 5 deletions context_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
request := contextRequestPool.Get().(*ContextRequest)
httpBody, err := getHttpBody(ctx)
if err != nil {
log.Error(fmt.Sprintf("%+v", errors.Unwrap(err)))
log.Error(fmt.Sprintf("%+v", err))

Check failure on line 46 in context_request.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

error: Potential nil panic detected. Observed nil flow from source to dereference point:

Check failure on line 46 in context_request.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

error: Potential nil panic detected. Observed nil flow from source to dereference point:
}
request.ctx = ctx
request.instance = ctx.instance
Expand Down Expand Up @@ -476,11 +476,13 @@
return nil, fmt.Errorf("retrieve json error: %v", err)
}

if err := json.Unmarshal(bodyBytes, &data); err != nil {
return nil, fmt.Errorf("decode json [%v] error: %v", string(bodyBytes), err)
}
if len(bodyBytes) > 0 {
if err := json.Unmarshal(bodyBytes, &data); err != nil {
return nil, fmt.Errorf("decode json [%v] error: %v", string(bodyBytes), err)
}

request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
}
}

if contentType == "multipart/form-data" {
Expand Down
20 changes: 20 additions & 0 deletions context_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,26 @@ func (s *ContextRequestSuite) TestInputBool() {
s.Equal(http.StatusOK, code)
}

// Test Issue: https://github.com/goravel/goravel/issues/528
func (s *ContextRequestSuite) TestPostWithEmpty() {
s.route.Post("/post-with-empty", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Success().Json(contractshttp.Json{
"all": ctx.Request().All(),
})
})

payload := strings.NewReader("")
req, err := http.NewRequest("POST", "/post-with-empty?a=1", payload)
req.ContentLength = 1
s.Require().Nil(err)

req.Header.Set("Content-Type", "application/json")
code, body, _, _ := s.request(req)

s.Equal("{\"all\":{\"a\":\"1\"}}", body)
s.Equal(http.StatusOK, code)
}

func (s *ContextRequestSuite) TestQuery() {
s.route.Get("/query", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Success().Json(contractshttp.Json{
Expand Down
Loading