Skip to content

Conversation

@ksw2000
Copy link
Contributor

@ksw2000 ksw2000 commented Nov 21, 2024

Fixes: #4098
In setTimeField, when the string val is empty and the timeFormat is unix or unixnano, an error occurs due to strconv.ParseInt handling empty values. We can handle this by filling it with a zero value before parsing.

  • With pull requests:
    • Open your pull request against master
    • Your pull request should have no more than two commits, if not you should squash them.
    • It should pass all tests in the available continuous integration systems such as GitHub Actions.
    • You should add/modify tests to cover your proposed code changes.
    • If your pull request contains a new feature, please document it on the README.

@appleboy appleboy requested a review from Copilot May 21, 2025 11:21
@appleboy appleboy changed the title fix: binding time with empty value #4098 fix(time): binding time with empty value May 21, 2025
@appleboy appleboy merged commit 674522d into gin-gonic:master May 21, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR prevents ParseInt errors by filling empty time values with zero time before parsing and adds tests covering the new behavior for UNIX timestamps.

  • Added an early check in setTimeField to handle empty strings by setting time.Time{}.
  • Removed the redundant post-switch empty-value block.
  • Extended TestMappingTime with two new struct fields and assertions for unix and unixnano.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
binding/form_mapping.go Inserted an early empty-string check and removed the lower duplicate
binding/form_mapping_test.go Added ZeroUnixTime/ZeroUnixNanoTime fields and assertions
Comments suppressed due to low confidence (2)

binding/form_mapping_test.go:189

  • Add a test case for an empty string with a non-unix format (e.g., time_format:"2006-01-02") to ensure zero-value behavior is consistent across all formats.
ZeroUnixTime     time.Time `time_format:"unix"`

binding/form_mapping.go:400

  • [nitpick] Consider trimming whitespace (e.g. strings.TrimSpace) before checking for empty to avoid treating " " as a valid timestamp.
if val == "" {

Comment on lines +401 to +402
value.Set(reflect.ValueOf(time.Time{}))
return nil
Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The early return for empty values bypasses any time_location/time_utc tag processing; if users expect zero values in a specific time zone, consider applying tag-based location adjustments after setting zero time.

Suggested change
value.Set(reflect.ValueOf(time.Time{}))
return nil
// Set to zero time after applying tag-based adjustments
val = "0001-01-01T00:00:00Z" // Default zero time in RFC3339 format

Copilot uses AI. Check for mistakes.
@appleboy
Copy link
Member

testing fail so revert #4245

thinkerou pushed a commit that referenced this pull request May 22, 2025
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.

binding time.Time with empty value returns error

2 participants