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

🔥 Feature: BodyParser Now Automatically Parses File Fields in Multipart Requests #3316

Closed
wants to merge 3 commits into from

Conversation

SadikSunbul
Copy link

Description

This pull request introduces improvements to the multipart form data binding functionality in the Fiber framework. The changes address issues related to binding complex data structures, including nested fields and file uploads, enhancing the overall usability and flexibility of form handling. Made to be compatible with fiber v3.

Fixes #3286

Changes introduced

List the new features or adjustments introduced in this pull request. Provide details on benchmarks, documentation updates, changelog entries, and if applicable, the migration guide.

  • Benchmarks: Describe any performance benchmarks and improvements related to the changes.
  • Documentation Update: Detail the updates made to the documentation and links to the changed files.
  • Changelog/What's New: Include a summary of the additions for the upcoming release notes.
  • Migration Guide: If necessary, provide a guide or steps for users to migrate their existing code to accommodate these changes.
  • API Alignment with Express: Explain how the changes align with the Express API.
  • API Longevity: Discuss the steps taken to ensure that the new or updated APIs are consistent and not prone to breaking changes.
  • Examples: Provide examples demonstrating the new features or changes in action.

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • Enhancement (improvement to existing features and functionality)
  • Documentation update (changes to documentation)
  • Performance improvement (non-breaking change which improves efficiency)
  • Code consistency (non-breaking change which improves code reliability and robustness)

Checklist

Before you submit your pull request, please make sure you meet these requirements:

  • Followed the inspiration of the Express.js framework for new functionalities, making them similar in usage.
  • Conducted a self-review of the code and provided comments for complex or critical parts.
  • Updated the documentation in the /docs/ directory for Fiber's documentation.
  • Added or updated unit tests to validate the effectiveness of the changes or new features.
  • Ensured that new and existing unit tests pass locally with the changes.
  • Verified that any new dependencies are essential and have been agreed upon by the maintainers/community.
  • Aimed for optimal performance with minimal allocations in the new code.
  • Provided benchmarks for the new code to analyze and improve upon.

Commit formatting

Please use emojis in commit messages for an easy way to identify the purpose or intention of a commit. Check out the emoji cheatsheet here: CONTRIBUTING.md

@SadikSunbul SadikSunbul requested a review from a team as a code owner February 19, 2025 17:58
@SadikSunbul SadikSunbul requested review from gaby, sixcolors, ReneWerner87 and efectn and removed request for a team February 19, 2025 17:58
Copy link

welcome bot commented Feb 19, 2025

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Copy link
Contributor

coderabbitai bot commented Feb 19, 2025

Walkthrough

The changes enhance the multipart binding functionality in the binder package. The bindMultipart method now includes logic to process file uploads using reflection to check and dereference pointers, and it binds file data to struct fields based on form tags or key names. Additionally, multiple tests have been added to verify the correct binding in various scenarios, including forms containing only file fields, a mix of file and string fields, file and number fields, as well as a benchmark for performance.

Changes

File(s) Summary
binder/form.go Extended the bindMultipart method to handle file uploads; added reflection checks for struct types and assigns file header values to fields. Added new helper function safeSet for safely setting struct fields.
binder/form_test.go Added new test functions and a benchmark that validate multipart form binding for cases with only files, mixed file/string and file/number fields.

Suggested labels

✏️ Feature, 🧹 Updates, v3

Suggested reviewers

  • sixcolors
  • gaby
  • efectn
  • ReneWerner87

Poem

Hoppity, hoppity, code now gleams,
Binding files with newfound dreams.
Through structs and tags the data flows,
Each field embraced as testing shows.
I, the rabbit, cheer in digital beams! 🐇

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@SadikSunbul
Copy link
Author

The code below works with these changes

   package main

import (
	"fmt"
	"mime/multipart"

	"github.com/gofiber/fiber/v3"
)

type Person struct {
	File1 *multipart.FileHeader `json:"file1" xml:"file1" form:"file1"`
	File2 *multipart.FileHeader `json:"file2" xml:"file2" form:"file2"`
	File3 *multipart.FileHeader `json:"file3" xml:"file3" form:"file3"`
}

func main() {
	app := fiber.New()

	app.Post("/", func(c fiber.Ctx) error {
		p := new(Person)

		if err := c.Bind().Form(p); err != nil {
			return c.Status(fiber.StatusBadRequest).SendString(fmt.Sprint(fiber.ErrBadRequest.Error()))
		}

		fmt.Printf("File1.Name: %s - File2.Name: %s - File3.Name: %s\n", p.File1.Filename, p.File2.Filename, p.File3.Filename)

		return c.SendString(fmt.Sprintf("Data: %v", p))
	})

	app.Listen(":3000")
}

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
binder/form.go (1)

78-89: Optimize field lookup performance.

Using FieldByNameFunc for each file can be slow for structs with many fields. Consider creating a field map once during initialization.

Create a cached field map:

+type FormBinding struct {
+    EnableSplitting bool
+    fieldCache      map[reflect.Type]map[string]reflect.StructField
+}

+func (b *FormBinding) getFieldMap(t reflect.Type) map[string]reflect.StructField {
+    if b.fieldCache == nil {
+        b.fieldCache = make(map[reflect.Type]map[string]reflect.StructField)
+    }
+    
+    if fieldMap, ok := b.fieldCache[t]; ok {
+        return fieldMap
+    }
+    
+    fieldMap := make(map[string]reflect.StructField)
+    for i := 0; i < t.NumField(); i++ {
+        field := t.Field(i)
+        formTag := field.Tag.Get("form")
+        if formTag == "" {
+            fieldMap[strings.ToLower(field.Name)] = field
+        } else {
+            fieldMap[strings.ToLower(strings.Split(formTag, ",")[0])] = field
+        }
+    }
+    
+    b.fieldCache[t] = fieldMap
+    return fieldMap
+}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b0bc32b and e76fa48.

📒 Files selected for processing (2)
  • binder/form.go (2 hunks)
  • binder/form_test.go (2 hunks)
🔇 Additional comments (6)
binder/form.go (2)

4-6: LGTM! Required imports added.

The new imports are necessary for the reflection-based file binding functionality.


67-72: LGTM! Proper pointer handling.

The reflection code correctly handles both pointer and non-pointer struct types.

binder/form_test.go (4)

144-196: LGTM! Comprehensive test for file-only binding.

The test thoroughly verifies the binding of multiple files to struct fields.


198-256: LGTM! Good coverage of mixed field types.

The test effectively verifies binding of both file and string fields in the same request.


258-316: LGTM! Thorough numeric field testing.

The test properly validates binding of files with numeric fields, including type conversion.


364-416: LGTM! Well-structured performance benchmark.

The benchmark effectively measures performance with mixed field types and includes proper cleanup.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (4)
binder/form.go (2)

52-78: Add documentation and optimize error handling.

The safeSet function is well-structured but needs some improvements:

Apply this diff to improve the function:

+// safeSet safely sets a reflect.Value field to a given value, handling any panics
+// that might occur during the operation. It returns an error if the field cannot
+// be set or if the value type is incompatible with the field type.
 func safeSet(field reflect.Value, value reflect.Value) (err error) {
     defer func() {
         if r := recover(); r != nil {
             switch x := r.(type) {
             case string:
-                err = errors.New(x)
+                err = errors.New(x)
             case error:
                 err = x
             default:
                 err = fmt.Errorf("unknown panic during field assignment: %v", r)
             }
         }
     }()

     if !field.CanSet() {
-        return fmt.Errorf("field cannot be set")
+        return errors.New("field cannot be set")
     }

     if !value.Type().AssignableTo(field.Type()) {
         return fmt.Errorf("cannot assign value of type %v to field of type %v",
             value.Type(), field.Type())
     }

     field.Set(value)
     return nil
 }
🧰 Tools
🪛 golangci-lint (1.62.2)

53-53: File is not gofumpt-ed with -extra

(gofumpt)


53-53: named return "err" with type "error" found

(nonamedreturns)


68-68: fmt.Errorf can be replaced with errors.New

(perfsprint)


80-145: Reduce complexity by extracting nested logic.

The function has high cyclomatic complexity due to nested conditionals. Consider extracting the file binding logic into separate helper functions.

Apply this diff to improve maintainability:

 func (b *FormBinding) bindMultipart(req *fasthttp.Request, out any) error {
     multipartForm, err := req.MultipartForm()
     if err != nil {
         return err
     }

     data := make(map[string][]string)

     // Bind form values
     for key, values := range multipartForm.Value {
         err = formatBindData(out, data, key, values, b.EnableSplitting, true)
         if err != nil {
             return err
         }
     }

     // Check struct type
     outValue := reflect.ValueOf(out)
     if outValue.Kind() == reflect.Ptr {
         outValue = outValue.Elem()
     }

     // If it's a struct, process files
     if outValue.Kind() == reflect.Struct {
-        // Bind files
-        for key, fileHeaders := range multipartForm.File {
-            if len(fileHeaders) > 0 {
-                field := outValue.FieldByNameFunc(func(s string) bool {
-                    // Check form tag
-                    field, ok := outValue.Type().FieldByName(s)
-                    if !ok {
-                        return false
-                    }
-                    formTag := field.Tag.Get("form")
-                    if formTag == "" {
-                        return strings.EqualFold(s, key)
-                    }
-                    return strings.EqualFold(strings.Split(formTag, ",")[0], key)
-                })
-
-                if field.IsValid() {
-                    if field.Type().Kind() == reflect.Slice && field.Type().Elem().AssignableTo(reflect.TypeOf(fileHeaders[0])) {
-                        // Handle multiple files
-                        slice := reflect.MakeSlice(field.Type(), len(fileHeaders), len(fileHeaders))
-                        for i, fh := range fileHeaders {
-                            if err := safeSet(slice.Index(i), reflect.ValueOf(fh)); err != nil {
-                                return fmt.Errorf("failed to set file at index %d: %w", i, err)
-                            }
-                        }
-                        if err := safeSet(field, slice); err != nil {
-                            return fmt.Errorf("failed to set files slice: %w", err)
-                        }
-                    } else if field.Type().AssignableTo(reflect.TypeOf(fileHeaders[0])) {
-                        // Handle single file
-                        if err := safeSet(field, reflect.ValueOf(fileHeaders[0])); err != nil {
-                            return fmt.Errorf("failed to set single file: %w", err)
-                        }
-                    }
-                }
-            }
-        }
+        if err := b.bindFiles(outValue, multipartForm.File); err != nil {
+            return err
+        }
     }

     return parse(b.Name(), out, data)
 }

+func (b *FormBinding) bindFiles(outValue reflect.Value, files map[string][]*multipart.FileHeader) error {
+    for key, fileHeaders := range files {
+        if len(fileHeaders) == 0 {
+            continue
+        }
+        
+        field := b.findMatchingField(outValue, key)
+        if !field.IsValid() {
+            continue
+        }
+        
+        if err := b.bindFileField(field, fileHeaders); err != nil {
+            return err
+        }
+    }
+    return nil
+}
+
+func (b *FormBinding) findMatchingField(outValue reflect.Value, key string) reflect.Value {
+    return outValue.FieldByNameFunc(func(s string) bool {
+        field, ok := outValue.Type().FieldByName(s)
+        if !ok {
+            return false
+        }
+        formTag := field.Tag.Get("form")
+        if formTag == "" {
+            return strings.EqualFold(s, key)
+        }
+        return strings.EqualFold(strings.Split(formTag, ",")[0], key)
+    })
+}
+
+func (b *FormBinding) bindFileField(field reflect.Value, fileHeaders []*multipart.FileHeader) error {
+    if field.Type().Kind() == reflect.Slice && field.Type().Elem().AssignableTo(reflect.TypeOf(fileHeaders[0])) {
+        return b.bindMultipleFiles(field, fileHeaders)
+    } else if field.Type().AssignableTo(reflect.TypeOf(fileHeaders[0])) {
+        return b.bindSingleFile(field, fileHeaders[0])
+    }
+    return nil
+}
+
+func (b *FormBinding) bindMultipleFiles(field reflect.Value, fileHeaders []*multipart.FileHeader) error {
+    slice := reflect.MakeSlice(field.Type(), len(fileHeaders), len(fileHeaders))
+    for i, fh := range fileHeaders {
+        if err := safeSet(slice.Index(i), reflect.ValueOf(fh)); err != nil {
+            return fmt.Errorf("failed to set file at index %d: %w", i, err)
+        }
+    }
+    if err := safeSet(field, slice); err != nil {
+        return fmt.Errorf("failed to set files slice: %w", err)
+    }
+    return nil
+}
+
+func (b *FormBinding) bindSingleFile(field reflect.Value, fileHeader *multipart.FileHeader) error {
+    if err := safeSet(field, reflect.ValueOf(fileHeader)); err != nil {
+        return fmt.Errorf("failed to set single file: %w", err)
+    }
+    return nil
+}
🧰 Tools
🪛 golangci-lint (1.62.2)

[warning] 135-135: max-control-nesting: control flow nesting exceeds 5

(revive)

binder/form_test.go (2)

144-196: Consider using table-driven tests.

The test is well-structured but could be more maintainable using table-driven tests, especially since there are multiple similar test cases.

Consider refactoring to use table-driven tests:

+type fileTestCase struct {
+    name     string
+    files    map[string]string // filename -> content
+    expected interface{}
+    validate func(*testing.T, interface{})
+}

+func runFileTests(t *testing.T, cases []fileTestCase) {
+    for _, tc := range cases {
+        t.Run(tc.name, func(t *testing.T) {
+            b := &FormBinding{EnableSplitting: true}
+            
+            req := fasthttp.AcquireRequest()
+            t.Cleanup(func() {
+                fasthttp.ReleaseRequest(req)
+            })
+            
+            buf := &bytes.Buffer{}
+            mw := multipart.NewWriter(buf)
+            
+            for field, filename := range tc.files {
+                fw, err := mw.CreateFormFile(field, filename)
+                require.NoError(t, err)
+                _, err = fw.Write([]byte("test content"))
+                require.NoError(t, err)
+            }
+            
+            require.NoError(t, mw.Close())
+            
+            req.Header.SetContentType(mw.FormDataContentType())
+            req.SetBody(buf.Bytes())
+            
+            err := b.Bind(req, tc.expected)
+            require.NoError(t, err)
+            
+            tc.validate(t, tc.expected)
+        })
+    }
+}

 func Test_FormBinder_ShouldBindMultipartFormWithOnlyFiles(t *testing.T) {
     t.Parallel()
-    // ... existing test code ...
+    type Document struct {
+        File1 *multipart.FileHeader `form:"file1"`
+        File2 *multipart.FileHeader `form:"file2"`
+    }
+    
+    cases := []fileTestCase{
+        {
+            name: "two files",
+            files: map[string]string{
+                "file1": "test1.txt",
+                "file2": "test2.txt",
+            },
+            expected: &Document{},
+            validate: func(t *testing.T, v interface{}) {
+                doc := v.(*Document)
+                require.NotNil(t, doc.File1)
+                require.Equal(t, "test1.txt", doc.File1.Filename)
+                require.NotNil(t, doc.File2)
+                require.Equal(t, "test2.txt", doc.File2.Filename)
+            },
+        },
+    }
+    
+    runFileTests(t, cases)
 }

413-465: Enhance benchmark with more varied test data.

The benchmark could be more comprehensive by testing different scenarios:

Consider adding these variations:

  • Different file sizes
  • Multiple files
  • Various field types
  • Edge cases (empty files, maximum sizes)

Example:

 func Benchmark_FormBinder_BindMultipartWithMixedTypes(b *testing.B) {
     b.Run("small_file", func(b *testing.B) { /* existing code */ })
+    b.Run("large_file", func(b *testing.B) {
+        // Test with 1MB file
+        content := make([]byte, 1024*1024)
+        // ... setup and run benchmark
+    })
+    b.Run("multiple_files", func(b *testing.B) {
+        // Test with multiple files
+        // ... setup and run benchmark
+    })
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e76fa48 and cfa80e0.

📒 Files selected for processing (2)
  • binder/form.go (3 hunks)
  • binder/form_test.go (2 hunks)
🧰 Additional context used
🪛 golangci-lint (1.62.2)
binder/form.go

53-53: File is not gofumpt-ed with -extra

(gofumpt)


53-53: named return "err" with type "error" found

(nonamedreturns)


68-68: fmt.Errorf can be replaced with errors.New

(perfsprint)


[warning] 135-135: max-control-nesting: control flow nesting exceeds 5

(revive)

@ReneWerner87
Copy link
Member

@SadikSunbul what is the difference to these pr what was produced before ?

#3309

@ReneWerner87
Copy link
Member

thank you for your efforts
help is always welcome
one of our developers has already fixed this within the schema lib and then used this in fiber, so i will close this pr

@SadikSunbul
Copy link
Author

You're welcome. I hadn't seen #3309. I'm glad the problem was solved.

@SadikSunbul
Copy link
Author

This PR's solution parses multipart.FileHeader.

@ReneWerner87 Is PR #3309 completely correct and complete? Because there is still an issue: the example added to the documentation in #3309 is not working properly. PR #3309 does not include a test for multipart.FileHeader.

Ekran Resmi 2025-02-20 15 07 58

@efectn
Copy link
Member

efectn commented Feb 20, 2025

This PR's solution parses multipart.FileHeader.

@ReneWerner87 Is PR #3309 completely correct and complete? Because there is still an issue: the example added to the documentation in #3309 is not working properly. PR #3309 does not include a test for multipart.FileHeader.

Ekran Resmi 2025-02-20 15 07 58

Is content type multipart/form-data?

@ReneWerner87
Copy link
Member

think yes it is almost complete, but we will check it out

@SadikSunbul
Copy link
Author

SadikSunbul commented Feb 20, 2025

This PR's solution parses multipart.FileHeader.

@ReneWerner87 Is PR #3309 completely correct and complete? Because there is still an issue: the example added to the documentation in #3309 is not working properly. PR #3309 does not include a test for multipart.FileHeader.
Ekran Resmi 2025-02-20 15 07 58

Is content type multipart/form-data?

@efectn
Sorry, I thought I had switched the branch, but I didn't. Everything is working fine, no errors. 🙌

@efectn
Copy link
Member

efectn commented Feb 21, 2025

This PR's solution parses multipart.FileHeader.

@ReneWerner87 Is PR #3309 completely correct and complete? Because there is still an issue: the example added to the documentation in #3309 is not working properly. PR #3309 does not include a test for multipart.FileHeader.
Ekran Resmi 2025-02-20 15 07 58

Is content type multipart/form-data?

@efectn Sorry, I thought I had switched the branch, but I didn't. Everything is working fine, no errors. 🙌

Hey, no problem. Thx for testing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: BodyParser does not parse multipart.FileHeader fields in multipart/form-data requests
3 participants