Skip to content

feat(binding): add support for binding whole request at once#4543

Open
takanuva15 wants to merge 2 commits intogin-gonic:masterfrom
takanuva15:master
Open

feat(binding): add support for binding whole request at once#4543
takanuva15 wants to merge 2 commits intogin-gonic:masterfrom
takanuva15:master

Conversation

@takanuva15
Copy link
Contributor

@takanuva15 takanuva15 commented Feb 19, 2026

With this PR, users can now call c.BindAll to allow Gin to bind headers, request path params, query params, and body in a single call. This resolves a number of tickets regarding this functionality:

relates to #1824
relates to #2309
closes #2535
closes #2758
closes #2919
closes #4338
closes #3095
closes #4510

MVE:

package main

import (
  "net/http"

  "github.com/gin-gonic/gin"
)

type Person struct {
  Age       int       `header:"x-age" binding:"required"`
  ID        string    `uri:"id" binding:"required,uuid"`
  Name      string    `form:"name" binding:"required"`
  Addresses [2]string `json:"addresses" binding:"required"`
}

func main() {
  route := gin.Default()
  route.POST("/:id", func(c *gin.Context) {
    var person Person
    if err := c.ShouldBindAll(&person); err != nil {
      c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
      return
    }
    c.JSON(http.StatusOK, person)
  })
  route.Run(":8080")
}

Test it with:

curl -X POST -H "x-age:25" -H "Content-Type: application/json" -d '{"addresses":["foo","bar"]}' 'http://localhost:8080/987fbc97-4bed-5078-9f07-9141ba07c9f3?name=Bob'
# {"Age":25,"ID":"987fbc97-4bed-5078-9f07-9141ba07c9f3","Name":"Bob","addresses":["foo","bar"]}

Pull Request Checklist

Please ensure your pull request meets the following requirements:

  • Open your pull request against the master branch.
  • All tests pass in available continuous integration systems (e.g., GitHub Actions).
  • Tests are added or modified as needed to cover code changes.
  • If the pull request introduces a new feature, the feature is documented in the docs/doc.md.

Thank you for contributing!

@codecov
Copy link

codecov bot commented Feb 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.39%. Comparing base (3dc1cd6) to head (64a0c53).
⚠️ Report is 275 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4543      +/-   ##
==========================================
- Coverage   99.21%   98.39%   -0.83%     
==========================================
  Files          42       49       +7     
  Lines        3182     3170      -12     
==========================================
- Hits         3157     3119      -38     
- Misses         17       42      +25     
- Partials        8        9       +1     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags sonic 98.37% <100.00%> (?)
-tags go_json 98.25% <93.75%> (?)
-tags nomsgpack 98.36% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.25 98.39% <100.00%> (?)
go-1.26 98.39% <100.00%> (?)
macos-latest 98.39% <100.00%> (-0.83%) ⬇️
ubuntu-latest 98.39% <100.00%> (-0.83%) ⬇️

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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@takanuva15
Copy link
Contributor Author

@appleboy All PR checks passing. Ready for review

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new “bind everything” capability to Gin’s Context so a single call can bind headers, URI params, query params, and request body into one struct, with validation applied after all parts are bound.

Changes:

  • Introduce Context.BindAll / Context.ShouldBindAll to bind headers + URI + query + body in one call.
  • Add a new internal-style binding engine binding.All (via BindingMany) to perform multi-source binding and defer validation.
  • Document the new API and add unit tests covering success + validation failure paths.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
context.go Adds BindAll / ShouldBindAll methods that delegate to binding.All.
binding/all.go Implements the new multi-source binder that binds header/uri/query then binds body via binding.Default.
binding/binding.go Adds BindingMany interface and exports All binder (non-nomsgpack build).
binding/binding_nomsgpack.go Adds BindingMany interface and exports All binder (nomsgpack build).
binding/binding_test.go Adds unit tests for binding.All across header/uri/query/body and failure cases.
context_test.go Adds tests for Context.BindAll and Context.ShouldBindAll.
docs/doc.md Documents “Bind All” usage with an end-to-end example.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@takanuva15 takanuva15 force-pushed the master branch 2 times, most recently from 15e33b4 to 5355337 Compare March 4, 2026 02:18
@takanuva15
Copy link
Contributor Author

@appleboy Addressed all comments. Ready for re-review

@appleboy
Copy link
Member

appleboy commented Mar 4, 2026

@takanuva15 If the AI recommendations have already been implemented, please click the Resolve button to indicate that the issue has been addressed.

@takanuva15
Copy link
Contributor Author

@appleboy done, comments resolved. Ready for rereview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

3 participants