fix(binding): improve empty slice/array handling in form binding#4380
fix(binding): improve empty slice/array handling in form binding#4380appleboy merged 1 commit intogin-gonic:masterfrom
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.
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 | ||
| } |
There was a problem hiding this comment.
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 | ||
| } |
There was a problem hiding this comment.
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