Skip to content

fix(spec): read gin's binding tag in request-body schemas - #607

Merged
ehabterra merged 1 commit into
mainfrom
fix/600-gin-binding-body-schemas
Nov 18, 2026
Merged

ehabterra merged 1 commit into
mainfrom
fix/600-gin-binding-body-schemas

Conversation

@ehabterra

Copy link
Copy Markdown
Owner

Closes #600.

Problem

gin validates with the binding struct-tag key, not validate. Body schemas read only validate:, so every gin request body declared the gin way documented no required fields and no constraints:

type CreateItem struct {
	Name  string `json:"name" binding:"required,min=3"`
	Email string `json:"email" validate:"required,email"`
}

produced required: [email] and no minLength on name.

Change

The validator key is a framework fact. One schema serves a type wherever it appears, so the key cannot belong to a single pattern. ParamPattern.ValidateTag (added in #601, unreleased) becomes FrameworkConfig.ValidateTags. Three readers use it:

reader before after
request/response body schemas validate only validate + validateTags
struct-bound parameters (#355) per-pattern ValidateTag validate + validateTags
struct-level note on a _ marker field (#166) validate, found by substring validate + validateTags, each key looked up whole

gin's preset sets binding. Other presets set nothing, so a chi or echo project's binding: tags stay unread unless it lists the key.

Mixed projects. SecondaryView and MergeFrameworkConfigs carry and union the keys. A tag key can only claim a field that carries it, so a chi API that also binds gin requests keeps gin's rules on gin's types.

dive per key. Each key's rules are split on dive before they are joined. Concatenated whole, validate:"dive,max=10" binding:"required" would read required as an element rule and lose it from the slice.

The web UI config editor gains a "Validator tag keys" section; TestConfigEditorCoversEveryFrameworkSection requires one.

Tests

  • testdata/gin_binding_body + TestTestdata_GinBindingBody: the issue's reproduction, both keys on one field, numeric and slice (max=5,dive,min=2) rules, the cross-key dive, and a nested component with its own binding rules. Checked to fail on the issue's output (required = [email]) with gin's key removed.
  • TestValidatorRules, TestExtractValidationConstraintsFrameworkKey: unconfigured keys ignored, whole-key lookup (xvalidate: does not match), repeated keys, per-key dive.
  • TestValidateTagsMerge: gin's preset, chi's empty one, SecondaryView, and a merge listing the key once.
  • Drift: specs from main and this branch, diffed over all 168 fixtures. The only change outside the new fixture is testdata/gin, whose Name string json:"name" binding:"required"`` is now required. That is the fix.
  • Full suite, golangci-lint (Go 1.26 toolchain, as CI), go vet and gofmt are clean.

Docs

docs/LIMITATIONS.md no longer lists the gap, docs/CONFIGURATION.md documents validateTags, and CHANGELOG.md has a Fixed entry.

gin validates with the `binding` struct-tag key, but body schemas read only
`validate:`, so every gin request body declared the gin way documented no
required fields and no constraints (`binding:"required,min=3"` produced
neither).

The key is a framework fact, not a pattern's: one schema serves a type
wherever it appears. ParamPattern.ValidateTag (unreleased, #355) becomes
FrameworkConfig.ValidateTags, read by the body mapper, the struct-bound
parameters and the struct-level validation note alike. gin's preset sets
`binding`; SecondaryView and MergeFrameworkConfigs carry and union it, since
a tag key can only claim a field that carries it.

Each key's rules are split on `dive` before they are joined, so
`validate:"dive,max=10" binding:"required"` keeps `required` on the slice
rather than reading it as an element rule. The struct-level note now looks
keys up whole instead of by substring.

testdata/gin_binding_body covers the issue's reproduction, both keys on one
field, numeric and slice rules, the cross-key dive and a nested component.
The only drift across all 168 fixtures is testdata/gin, whose body now lists
its `binding:"required"` name field as required.

Closes #600.
@coderabbitai

coderabbitai Bot commented Nov 18, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 29 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 5a9610f and 99fc54f.

Files ignored due to path filters: 3

  • testdata/gin_binding_body/go.mod is excluded by !**/testdata/**
  • testdata/gin_binding_body/go.sum is excluded by !**/*.sum, !**/testdata/**
  • testdata/gin_binding_body/main.go is excluded by !**/testdata/**
Files selected for processing: 12

  • CHANGELOG.md
  • cmd/apispecui/assets/js/config.js
  • docs/CONFIGURATION.md
  • docs/LIMITATIONS.md
  • generator/testdata_gin_binding_body_test.go
  • internal/spec/config.go
  • internal/spec/config_gin.go
  • internal/spec/config_merge.go
  • internal/spec/config_merge_test.go
  • internal/spec/mapper.go
  • internal/spec/struct_binding.go
  • internal/spec/validation_helpers_test.go
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@ehabterra
ehabterra merged commit d206901 into main Nov 18, 2026
7 checks passed
@ehabterra
ehabterra deleted the fix/600-gin-binding-body-schemas branch November 18, 2026 22:03
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.

gin's binding: tag is ignored in body schemas: a gin request body documents no required fields or constraints

1 participant