Skip to content

feat(binding): add per-request strict JSON binding (JSONStrict)#4642

Open
walle250ai wants to merge 1 commit into
gin-gonic:masterfrom
walle250ai:feat/add-json-strict-binding
Open

feat(binding): add per-request strict JSON binding (JSONStrict)#4642
walle250ai wants to merge 1 commit into
gin-gonic:masterfrom
walle250ai:feat/add-json-strict-binding

Conversation

@walle250ai
Copy link
Copy Markdown

Summary

The existing global flag is not safe for concurrent use: toggling it affects all in-flight requests, making per-endpoint strict validation impossible without race conditions.

This PR adds a request-scoped strict JSON binding path:

  • — new that always enables , independent of the global flag
  • — returns error on unknown fields, does not abort
  • — aborts with HTTP 400 on any binding error including unknown fields

The existing behavior and are untouched.

Test plan

  • : valid payload, unknown fields (must error), empty body, invalid JSON, independence from global flag
  • : HTTP-layer behavior of (400 on unknown fields/invalid JSON/empty body) and (error returned, no abort)
  • All 13 new tests pass:

🤖 Generated with Claude Code

…ejection

Add jsonStrictBinding that always enables DisallowUnknownFields on the
JSON decoder, independent of the global EnableDecoderDisallowUnknownFields
flag. Exposes BindJSONStrict and ShouldBindJSONStrict on Context.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Qodo-Free-For-OSS
Copy link
Copy Markdown

Hi, Context.BindJSONStrict/ShouldBindJSONStrict reference binding.JSONStrict, but the nomsgpack build variant does not define that symbol, so builds with -tags nomsgpack will not compile.

Severity: action required | Category: correctness

How to fix: Define JSONStrict for nomsgpack

Agent prompt to fix - you can give this to your LLM of choice:

Issue description

Building with -tags nomsgpack fails because context.go references binding.JSONStrict but the nomsgpack binding variant doesn’t define it.

Issue Context

  • binding/binding.go is guarded by //go:build !nomsgpack.
  • In nomsgpack builds, binding/binding_nomsgpack.go is used instead and currently lacks a JSONStrict variable.

Fix Focus Areas

  • binding/binding_nomsgpack.go[71-88]
  • context.go[767-884]

What to change

  • Add JSONStrict = jsonStrictBinding{} (typed consistently with the other vars in this file) into the var (...) block in binding/binding_nomsgpack.go.
  • Ensure it implements the same interfaces as JSON in nomsgpack builds (it should, since jsonStrictBinding has Bind and BindBody).

We noticed a couple of other issues in this PR as well - happy to share if helpful.


Found by Qodo. Free code review for open-source maintainers.

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.

2 participants