Repository navigation
fix(spec): read gin's binding tag in request-body schemas - #607
Merged
Merged
Conversation
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.
|
Warning Review limit reachedYou'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. View limit details
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #600.
Problem
gin validates with the
bindingstruct-tag key, notvalidate. Body schemas read onlyvalidate:, so every gin request body declared the gin way documented no required fields and no constraints:produced
required: [email]and nominLengthonname.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) becomesFrameworkConfig.ValidateTags. Three readers use it:validateonlyvalidate+validateTagsValidateTagvalidate+validateTags_marker field (#166)validate, found by substringvalidate+validateTags, each key looked up wholegin's preset sets
binding. Other presets set nothing, so a chi or echo project'sbinding:tags stay unread unless it lists the key.Mixed projects.
SecondaryViewandMergeFrameworkConfigscarry 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.diveper key. Each key's rules are split ondivebefore they are joined. Concatenated whole,validate:"dive,max=10" binding:"required"would readrequiredas an element rule and lose it from the slice.The web UI config editor gains a "Validator tag keys" section;
TestConfigEditorCoversEveryFrameworkSectionrequires 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-keydive, and a nested component with its ownbindingrules. 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-keydive.TestValidateTagsMerge: gin's preset, chi's empty one,SecondaryView, and a merge listing the key once.mainand this branch, diffed over all 168 fixtures. The only change outside the new fixture istestdata/gin, whoseName stringjson:"name" binding:"required"`` is now required. That is the fix.golangci-lint(Go 1.26 toolchain, as CI),go vetandgofmtare clean.Docs
docs/LIMITATIONS.mdno longer lists the gap,docs/CONFIGURATION.mddocumentsvalidateTags, andCHANGELOG.mdhas a Fixed entry.