Skip to content

fix(conditional): compare modification times at HTTP date precision - #1159

Merged
robtme merged 3 commits into
danielgtaylor:mainfrom
APX403:conditional-subsecond
Nov 29, 2026
Merged

robtme merged 3 commits into
danielgtaylor:mainfrom
APX403:conditional-subsecond

Conversation

@APX403

@APX403 APX403 commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

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 a time.Now() value or a database timestamp:

huma.Get(api, "/thing", func(ctx context.Context, input *struct {
	conditional.Params
}) (*GetThingResponse, error) {
	if input.HasConditionalParams() {
		if err := input.PreconditionFailed("", thing.Modified); err != nil {
			return nil, err
		}
	}
	return &GetThingResponse{LastModified: thing.Modified, Body: thing}, nil
})

With thing.Modified at 12:00:00.123456Z, the response has Last-Modified: Fri, 30 Oct 2026 12:00:00 GMT. A GET that sends it back in If-Modified-Since gets a 200 instead of a 304, and a PUT handled the same way gets a 412 when it sends that date in If-Unmodified-Since:

If-Unmodified-Since: Fri, 30 Oct 2026 12:00:00 GMT precondition failed, resource was modified at Fri, 30 Oct 2026 12:00:00 GMT

autopatch copies Last-Modified into If-Unmodified-Since for 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 PreconditionFailed compares 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 with http.TimeFormat.

Times that aren't in UTC have a separate problem: huma writes Last-Modified in the time's own zone with a GMT suffix (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.

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

codecov Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.13%. Comparing base (606bcc2) to head (bf71b35).
⚠️ Report is 9 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@robtme robtme self-assigned this Nov 2, 2026

@by-ae by-ae left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@APX403

APX403 commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Added that row in be03c10. It fails without the .UTC() (the message then says 22:00:00 GMT) and passes with it. The Last-Modified header is in #1161

@robtme
robtme force-pushed the conditional-subsecond branch from 0dacab2 to bf71b35 Compare November 29, 2026 15:18
@robtme
robtme merged commit c9c21cd into danielgtaylor:main Nov 29, 2026
4 checks passed
@robtme

robtme commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Thanks @APX403!

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.

3 participants