fix: support time_format tag in JSON binding (ShouldBindJSON) - #4771
Open
waterWang wants to merge 1 commit into
Open
fix: support time_format tag in JSON binding (ShouldBindJSON)#4771waterWang wants to merge 1 commit into
waterWang wants to merge 1 commit into
Conversation
The time_format struct tag works for form/query binding but is completely ignored when binding via JSON (ShouldBindJSON). This commit adds support for time_format, time_utc, and time_location tags in the JSON binding path. When a struct has time.Time fields with time_format tags, the JSON decoder falls back to mappingByPtr with a jsonSource setter that uses setTimeField to parse the string value with the custom format. For structs without time_format tags, the original fast path (direct JSON unmarshal) is used, preserving full backward compatibility. Fixes gin-gonic#2170
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.
Description
The
time_formatstruct tag works for form/query binding but is completely ignored when binding via JSON (ShouldBindJSON). This has been a long-standing issue open since 2019 (see #2170).Root cause: The JSON binding path (
binding/json.go) usesjson.Decoder.Decode(obj)directly, which callstime.Time.UnmarshalJSON— this only supports RFC3339 format. Thetime_format/time_utc/time_locationtags are only checked in the form binding path (form_mapping.go→setTimeField).Fix: When a struct has
time.Timefields withtime_formattags, the decoder falls back tomappingByPtrwith ajsonSourcesetter that uses the existingsetTimeFieldfunction. For structs withouttime_formattags, the original fast path (direct JSON unmarshal withUseNumber/DisallowUnknownFieldssupport) is preserved.Changes
binding/json.go: AddedjsonSourcetype that implementssetterinterface. Fortime.Timefields withtime_formattags, it usessetTimeFieldto parse the string value with the custom format. For all other types, it uses standard JSON unmarshaling. ModifieddecodeJSONto check fortime_formattags and fall back to thejsonSourceapproach when needed.binding/json_test.go: Added comprehensive tests covering:"2006-01-02 15:04:05")unix,unixNano)time_utctag combinationtime_locationtag combinationtime_formattagBackward Compatibility
✅ 100% backward compatible — all existing tests pass unchanged. The fast path (direct JSON unmarshal) is used for structs without
time_formattags, soUseNumberandDisallowUnknownFieldssettings are unaffected.Fixes #2170