Skip to content

fix: Set default value for form fields#4047

Merged
appleboy merged 1 commit intogin-gonic:masterfrom
ahmadSaeedGoda:fix/apply-default-value-if-empty-value-provided-by-client-during-model-binding
Sep 6, 2024
Merged

fix: Set default value for form fields#4047
appleboy merged 1 commit intogin-gonic:masterfrom
ahmadSaeedGoda:fix/apply-default-value-if-empty-value-provided-by-client-during-model-binding

Conversation

@ahmadSaeedGoda
Copy link
Copy Markdown
Contributor

  • Use provided default value in struct tags when binding a request input to struct for validation.

Fixes: How to apply default value if empty value provided by client during model binding? #4042, #13042df, #a41721a

  • With pull requests:
    • Open your pull request against master
    • Your pull request should have no more than two commits, if not you should squash them.
    • It should pass all tests in the available continuous integration systems such as GitHub Actions.
    • You should add/modify tests to cover your proposed code changes.
    • If your pull request contains a new feature, please document it on the README.

- Use specified default value in struct tags when binding a request input to struct for validation, even if sent empty, not only when not sent at all.
- Add string field to `TestMappingDefault` test case.
- Add test case for not sent form field to default to the value specified via code.
- Add test case for form field sent empty to default to the value specified via code.

Fixes: How to apply default value if empty value provided by client during model binding? gin-gonic#4042, #13042df, #a41721a
@ahmadSaeedGoda ahmadSaeedGoda force-pushed the fix/apply-default-value-if-empty-value-provided-by-client-during-model-binding branch from b31017d to 497351c Compare September 4, 2024 03:14
@ahmadSaeedGoda
Copy link
Copy Markdown
Contributor Author

ahmadSaeedGoda commented Sep 5, 2024

@thinkerou @appleboy Kindly review this tiny portion, highly appreciated! 🆒

@codecov
Copy link
Copy Markdown

codecov Bot commented Sep 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.85%. Comparing base (3dc1cd6) to head (497351c).
Report is 69 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4047      +/-   ##
==========================================
- Coverage   99.21%   98.85%   -0.36%     
==========================================
  Files          42       44       +2     
  Lines        3182     2801     -381     
==========================================
- Hits         3157     2769     -388     
- Misses         17       19       +2     
- Partials        8       13       +5     
Flag Coverage Δ
?
-tags "sonic avx" 98.84% <100.00%> (?)
-tags go_json 98.84% <100.00%> (?)
-tags nomsgpack 98.83% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 98.85% <100.00%> (-0.36%) ⬇️
go-1.22 98.85% <100.00%> (?)
macos-latest 98.85% <100.00%> (-0.36%) ⬇️
ubuntu-latest 98.85% <100.00%> (-0.36%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@appleboy appleboy merged commit 28e57f5 into gin-gonic:master Sep 6, 2024
@appleboy appleboy added the type/bug Found something you weren't expecting? Report it here! label Sep 6, 2024
@appleboy appleboy added this to the v1.11 milestone Sep 6, 2024
1911860538 pushed a commit to 1911860538/gin that referenced this pull request Feb 26, 2025
- Use specified default value in struct tags when binding a request input to struct for validation, even if sent empty, not only when not sent at all.
- Add string field to `TestMappingDefault` test case.
- Add test case for not sent form field to default to the value specified via code.
- Add test case for form field sent empty to default to the value specified via code.

Fixes: How to apply default value if empty value provided by client during model binding? gin-gonic#4042, #13042df, #a41721a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement type/bug Found something you weren't expecting? Report it here!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants