fix(bind): honor OpenAPI date, date-time and date-time-local format tags (v4) - #3156
Merged
Merged
Conversation
Backport of #3145. Swag and OpenAPI put format:"date-time" and format:"date-time-local" on time.Time fields. Since v4.14.0 the binder treated those values as Go layouts, so valid input failed to bind. date-time now uses the same RFC3339 decoding as an untagged time.Time. date-time-local accepts 2006-01-02T15:04:05 with optional fractional seconds, rejects time-zone suffixes, and assigns UTC. Other format values are still Go layouts. Refs #3107
…uts (v4) Also gofmt two existing lines in TestTimeFormatBinding.
Map format:"date" (OpenAPI full-date) to 2006-01-02, state that the format tag applies to time.Time and *time.Time fields but not slices, and use the ok/nok subtest naming used elsewhere in bind_test.go.
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.
Backport of #3145 to v4.
Swag and OpenAPI put format names such as
format:"date-time"ontime.Timefields. Since v4.14.0 addedformattag support, the binder has treated those names as Go time layouts, so valid input failed to bind. Before v4.14.0 the tag was ignored, so RFC3339 values bound to swag-annotatedformat:"date-time"fields. That case broke on upgrade.date-timeuses the same RFC3339 decoding as an untaggedtime.Time, which restores the pre-v4.14.0 behavior.date-time-localaccepts2006-01-02T15:04:05with optional fractional seconds, rejects time-zone suffixes, and assigns UTC.dateaccepts2006-01-02and assigns UTC.date-time-localanddate, zoned RFC3339 values bound before v4.14.0 only because the tag was ignored. They stay rejected, as the OpenAPI definitions require.timepackage, so it is as lenient as untaggedtime.Timebinding (for example, it accepts a comma before fractional seconds).formatvalues are still Go layouts. Slice fields never used the tag and are unchanged.Compared with current v4, the only inputs that bound before and now fail are the literal strings
date-time,date-time-local, anddate, which parsed to0000-01-01T00:00:00Z.The tests are ported from #3145 and adapted to the v4 binder API. They cover path, query, header, form, and multipart sources, scalar and pointer fields, JSON decoding, custom types, the literal format names, and slice fields. Without the fix, 53 of the new cases fail. With it,
go test -race ./...,staticcheck, andgolintpass.The
BindPathParamsdoc now mentions the OpenAPI names and headers, and its HTMLdatetime-localexample uses aformtag. Two existing lines inTestTimeFormatBindingare gofmt-aligned.Refs #3107