gin/binding/json_time_format_test.go
waterWang b03993df02 fix(binding): respect time_format tag in JSON binding (ShouldBindJSON)
The time_format, time_utc, and time_location struct tags work correctly
for form/query binding (ShouldBindQuery, ShouldBindForm) but are
completely ignored when using ShouldBindJSON or ShouldBindBodyWithJSON.

encoding/json only understands RFC3339 for time.Time fields, so custom
time formats such as "2006-01-02 15:04:05" or "unix" produce a parse
error like:
  parsing time "2001-11-11 11:11:11" as "2006-01-02T15:04:05Z07:00":
  cannot parse " 11:11:11" as "T"

This fix replaces the single-pass JSON decode with a field-by-field
decode when the target struct has any time.Time field that carries a
time_format tag.  Fields with time_format are parsed by the existing
setTimeField helper (same one used by form binding), so all existing
tag features (time_format, time_utc, time_location, unix/unixmilli/
unixmicro/unixnano) work identically in JSON binding.

Fast path: when the struct has no time_format tags at all, the
original single-pass decoder.Decode path is used (zero overhead).

Fixes #2170
2026-08-10 09:51:07 +08:00

63 lines
1.8 KiB
Go

// Copyright 2019 Gin Core Team. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package binding
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type timeFormatReq struct {
Timestamp time.Time `json:"Timestamp" time_format:"2006-01-02 15:04:05"`
UnixTime time.Time `json:"UnixTime" time_format:"unix"`
Plain time.Time `json:"Plain"`
}
func TestJSONBindingTimeFormat(t *testing.T) {
var s timeFormatReq
err := jsonBinding{}.BindBody([]byte(`{
"Timestamp": "2001-11-11 11:11:11",
"UnixTime": 1575528300,
"Plain": "2001-11-11T11:11:11Z"
}`), &s)
require.NoError(t, err)
// Custom time_format tag is honored.
assert.Equal(t, time.Date(2001, 11, 11, 11, 11, 11, 0, time.Local), s.Timestamp)
// unix time_format tag is honored.
assert.Equal(t, time.Unix(1575528300, 0), s.UnixTime)
// Fields without a time_format tag keep default RFC3339 parsing.
assert.Equal(t, time.Date(2001, 11, 11, 11, 11, 11, 0, time.UTC), s.Plain)
}
type timeFormatPtrReq struct {
Timestamp *time.Time `json:"Timestamp" time_format:"2006-01-02 15:04:05"`
}
func TestJSONBindingTimeFormatPointer(t *testing.T) {
var s timeFormatPtrReq
err := jsonBinding{}.BindBody([]byte(`{"Timestamp": "2020-01-02 03:04:05"}`), &s)
require.NoError(t, err)
require.NotNil(t, s.Timestamp)
assert.Equal(t, time.Date(2020, 1, 2, 3, 4, 5, 0, time.Local), *s.Timestamp)
}
type timeFormatNested struct {
Inner struct {
When time.Time `json:"when" time_format:"2006-01-02"`
} `json:"inner"`
}
func TestJSONBindingTimeFormatNested(t *testing.T) {
var s timeFormatNested
err := jsonBinding{}.BindBody([]byte(`{"inner": {"when": "2019-12-07"}}`), &s)
require.NoError(t, err)
assert.Equal(t, time.Date(2019, 12, 7, 0, 0, 0, 0, time.Local), s.Inner.When)
}