Merge dcc508aa44bf4c7b8f828285e045016d9bab7ce4 into 1bd0ecf2d3e08da37efa270bb7be20c1eddb0d68

This commit is contained in:
ibraah007 2026-09-25 09:51:15 +02:00 committed by GitHub
commit 8d7cf14952
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 4 deletions

View File

@ -648,12 +648,14 @@ func (c *Context) initFormCache() {
if c.formCache == nil {
c.formCache = make(url.Values)
req := c.Request
if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
if !errors.Is(err, http.ErrNotMultipart) {
debugPrint("error on parse multipart form array: %v", err)
if req != nil {
if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
if !errors.Is(err, http.ErrNotMultipart) {
debugPrint("error on parse multipart form array: %v", err)
}
}
c.formCache = req.PostForm
}
c.formCache = req.PostForm
}
}

View File

@ -4055,3 +4055,17 @@ func BenchmarkGetMapFromFormData(b *testing.B) {
})
}
}
func TestContextFormWithoutRequest(t *testing.T) {
// Regression test for https://github.com/gin-gonic/gin/issues/4772
// Form getters should not panic when c.Request is nil, and should
// behave like the query getters by returning empty/default values.
c, _ := CreateTestContext(httptest.NewRecorder())
assert.Equal(t, "", c.PostForm("key"))
assert.Equal(t, "fallback", c.DefaultPostForm("key", "fallback"))
value, ok := c.GetPostForm("key")
assert.Equal(t, "", value)
assert.False(t, ok)
}