-
Notifications
You must be signed in to change notification settings - Fork 8.5k
fix(binding): improve empty slice/array handling in form binding #4380
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
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4380 +/- ##
==========================================
- Coverage 99.21% 98.92% -0.29%
==========================================
Files 42 44 +2
Lines 3182 3446 +264
==========================================
+ Hits 3157 3409 +252
- Misses 17 26 +9
- Partials 8 11 +3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes empty slice/array handling in form binding to properly use default values when available and prevents index out of bounds errors when no defaults exist. It addresses issue #4377.
Key changes:
- Modified the condition from checking form field presence to checking if the field has empty values
- Added proper handling for cases where no default value exists for empty slices/arrays
- Ensured default values are applied when empty arrays/slices are encountered
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| binding/form_mapping.go | Updated slice and array handling logic to check for empty values instead of field presence |
| binding/form_mapping_test.go | Added comprehensive test coverage for empty slice/array scenarios with and without defaults |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| if len(vs) == 0 { | ||
| if !opt.isDefaultExists { | ||
| return false, nil | ||
| } |
Copilot
AI
Sep 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition len(vs) == 0 will be true both when the field is not present in the form (ok == false) and when the field is present but has an empty slice. Consider adding a comment to clarify this intentional behavior change from the previous !ok check.
| if len(vs) == 0 { | ||
| if !opt.isDefaultExists { | ||
| return false, nil | ||
| } |
Copilot
AI
Sep 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition len(vs) == 0 will be true both when the field is not present in the form (ok == false) and when the field is present but has an empty slice. Consider adding a comment to clarify this intentional behavior change from the previous !ok check.
This ensures that empty slices/arrays properly use default values when available,
and prevents index out of bounds errors when no defaults exist.
Fixes #4377