Repository navigation
fix(conditional): compare modification times at HTTP date precision - #1159
Conversation
Last-Modified and the If-Modified-Since and If-Unmodified-Since headers carry whole seconds, so a modification time with a fraction of a second never matched the date a client sends back: If-Modified-Since never gave a 304, and If-Unmodified-Since, including the one autopatch sets, always failed.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1159 +/- ##
==========================================
+ Coverage 94.72% 95.13% +0.41%
==========================================
Files 25 30 +5
Lines 5193 5922 +729
==========================================
+ Hits 4919 5634 +715
- Misses 269 283 +14
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
by-ae
left a comment
There was a problem hiding this comment.
Ran this at 3711687 against its base 606bcc2 with a small humatest handler (GET and PUT, PreconditionFailed("", mod), Last-Modified header set from the same value).
With mod = 2026-10-30 12:00:00.123456 UTC, the header is Fri, 30 Oct 2026 12:00:00 GMT and sending it back gives GET If-Modified-Since -> 200 and PUT If-Unmodified-Since -> 412 on base, 304 and 204 on this branch. go test ./conditional ./autopatch passes on the branch. So the fix does what the description says.
One thing worth a test: the .UTC() in params.go#L70-L72 is not exercised by the new rows, because now/before/after in params_test.go#L108-L113 are all parsed with Z. It matters for the 412 message. With mod = 22:00:00.5 +10:00 (12:00:00.5Z) and If-Unmodified-Since: 11:00:00 GMT, base says resource was modified at Fri, 30 Oct 2026 22:00:00 GMT, and the branch says ... 12:00:00 GMT, which is correct (http.TimeFormat has a literal GMT, so the old code printed the local wall time as GMT). A row with a time.FixedZone value, checking the message from the returned error details, would pin that down.
Related, and you already noted it as out of scope: the Last-Modified header written from a non-UTC time (huma.go#L755-L756) is the same wall time with GMT stamped on it (I got Fri, 30 Oct 2026 22:00:00 GMT for 22:00 +10:00). That one is worth its own issue or PR.
0dacab2 to
bf71b35
Compare
|
Thanks @APX403! |
With
conditional.Params, a resource whose modification time has a fraction of a second never matches the date the client sends back, and that is the usual case for atime.Now()value or a database timestamp:With
thing.Modifiedat12:00:00.123456Z, the response hasLast-Modified: Fri, 30 Oct 2026 12:00:00 GMT. A GET that sends it back inIf-Modified-Sincegets a 200 instead of a 304, and a PUT handled the same way gets a 412 when it sends that date inIf-Unmodified-Since:autopatch copies
Last-ModifiedintoIf-Unmodified-Sincefor its PUT when there is no ETag (autopatch.go#L443-L444), so a PATCH that changes such a resource fails with that 412.HTTP dates carry whole seconds, but
PreconditionFailedcompares them with the full time (params.go#L115, #L127). It now truncates the time to the second first, as net/http does for the same headers (fs.go#L524-L526), and converts it to UTC, which the error messages need since they format it withhttp.TimeFormat.Times that aren't in UTC have a separate problem: huma writes
Last-Modifiedin the time's own zone with aGMTsuffix (huma.go#L755-L756), so the date a client sends back is off by the zone offset. I left that out to keep this change to the comparison.