Go: pointerize the five optional timestamps that could not represent absence
Closes #562.
⚠️ This is a silent breaking change. Read the migration note.
Five optional timestamps on the hand-written Go wrapper surface move from time.Time to *time.Time. Nothing at your call sites will stop compiling. hc.UpdatedAt.IsZero() compiles exactly as it did before — Go inserts the dereference for a value-receiver method call — and then panics on nil at runtime. No compiler error, just a crash in production the first time the API omits the field.
| Type | Field | Before | After |
|---|---|---|---|
HillChart | UpdatedAt | time.Time `json:"updated_at,omitempty"` | *time.Time `json:"updated_at,omitempty"` |
Notification | ReadAt | time.Time `json:"read_at,omitempty"` | *time.Time `json:"read_at,omitempty"` |
Notification | UnreadAt | time.Time `json:"unread_at,omitempty"` | *time.Time `json:"unread_at,omitempty"` |
SearchResult | CreatedAt | time.Time `json:"created_at"` | *time.Time `json:"created_at,omitempty"` |
SearchResult | UpdatedAt | time.Time `json:"updated_at"` | *time.Time `json:"updated_at,omitempty"` |
Migration
Nil-check before touching any of the five. Every pattern in the first block below compiles both before and after this change; only the second block is safe after it.
// BEFORE — still compiles after this change, and panics on nil.
if hc.UpdatedAt.IsZero() { … }
fmt.Println(hc.UpdatedAt.Format(time.RFC3339))
age := time.Since(n.ReadAt)
sort.Slice(rs, func(i, j int) bool { return rs[i].CreatedAt.Before(rs[j].CreatedAt) })
// AFTER — nil means "the API did not send this field".
if hc.UpdatedAt == nil { … } // absent
if hc.UpdatedAt != nil && hc.UpdatedAt.IsZero() { … } // present and zero
if hc.UpdatedAt != nil {
fmt.Println(hc.UpdatedAt.Format(time.RFC3339))
}
if n.ReadAt != nil {
age := time.Since(*n.ReadAt)
}
sort.Slice(rs, func(i, j int) bool {
if rs[i].CreatedAt == nil || rs[j].CreatedAt == nil {
return rs[j].CreatedAt != nil // absent sorts last
}
return rs[i].CreatedAt.Before(*rs[j].CreatedAt)
})
Two behavioural notes beyond the type:
- Absent no longer reads as the zero time. Code that treated
IsZero()as "absent" must switch to== nil. That reading was never sound anyway: it could not distinguish an omitted key from a server that genuinely sent0001-01-01T00:00:00Z. - The wire form changes on re-marshal. An absent timestamp previously came back out as
"updated_at":"0001-01-01T00:00:00Z"; it is now omitted. If you round-trip these structs through your own storage or golden files, that fabricated instant will stop appearing — which is the point, but it is a diff.
SearchResult additionally gains ,omitempty on both tags. Those two were previously tagged as required while the schema marks them optional and the generated client types them *time.Time; the tags now match reality.
The defect, precisely
All five schemas mark the field optional and the generated client types all five as *time.Time. The wrapper flattened them — through deref() in hillChartFromGenerated / searchResultFromGenerated, and via direct json.Unmarshal for Notification.
That did two things at once:
- Absence collapsed to the zero time, indistinguishable from a real (if implausible) value.
,omitemptywas inert.encoding/json's empty set isfalse/0/ nil pointer / nil interface / empty array, slice, map, string. A struct is never empty, andtime.Timeis a struct — so the key was re-emitted on every marshal, carrying the fabricated0001-01-01T00:00:00Z.
Point 2 is the boundary that separates timestamps from the rest of this surface. Title string `json:"title,omitempty"` is lossy in a benign way: absent and "" collapse, and both are then omitted, so the value round-trips. A value-typed optional time.Time fabricates data on the wire.
How the five were found — and five more that are not fixed here
The issue body names HillChart.UpdatedAt; its title also names the SearchResult timestamps. Neither grep nor any existing gate can enumerate the class, because the wrapper source does not say the field is optional — that evidence lives in the schema and in the generated client. So the enumeration is a type cross-reference between the two sides:
- parse every non-test
go/pkg/basecamp/*.gofor struct fields typed exactlytime.Time, keyed(struct name, json tag) - parse
go/pkg/generated/client.gen.gothe same way - report every wrapper field whose name-matched, tag-matched generated counterpart is
*time.Time
Against this PR's base (a7354ae35) that scan reads 106 value-typed time.Time wrapper fields, of which 10 have an optional generated counterpart, 0 unpaired. It discriminates: generated.Webhook.CreatedAt, generated.Question.CreatedAt and generated.ClientApproval.CreatedAt are value time.Time (genuinely required) and do not appear; EverythingFile.CreatedAt/UpdatedAt are already *time.Time and do not appear.
This PR fixes 5 of those 10. The other 5 are real and are reported, not fixed, in #620:
| Struct | Field | Wrapper | Generated |
|---|---|---|---|
TimelineEvent | CreatedAt | timeline.go:20 — time.Time `json:"created_at"` | *time.Time |
WebhookDelivery | CreatedAt | webhooks.go:40 — time.Time `json:"created_at"` | *time.Time |
QuestionReminder | RemindAt | checkins.go:143 — time.Time `json:"remind_at"` | *time.Time |
ClientApprovalResponse | CreatedAt | client_approvals.go:68 — time.Time `json:"created_at"` | *time.Time |
ClientApprovalResponse | UpdatedAt | client_approvals.go:69 — time.Time `json:"updated_at"` | *time.Time |
None is @required in spec/basecamp.smithy. Each is a silent break with the same failure mode as the five above, so each needs the same deliberate migration note — which is why they are a separate change rather than a late amendment to this one, not because they are less real.
The five that do ship together ship together because the failure mode is invisible to the compiler: splitting them would ask consumers to hunt for nil-safety three times over, each time with no build error to guide them.
Red proof
go/pkg/basecamp/optional_timestamps_test.go is written so every assertion compiles against both the fixed and un-fixed trees — it asserts on marshaled JSON keys, never on the field types, so nothing had to be edited between the two runs. Against the un-fixed tree, verbatim from go test ./pkg/basecamp/ -run 'TestOptionalTimestamps|TestNilOptionalTimestampPanics|TestNoValueTypedOptionalTimestamps' -v:
=== RUN TestOptionalTimestampsOmitWhenAbsent
=== RUN TestOptionalTimestampsOmitWhenAbsent/HillChart
optional_timestamps_test.go:112: HillChart.updated_at was absent on the wire but re-marshaled as "0001-01-01T00:00:00Z"; a value-typed time.Time cannot represent absence and `,omitempty` never fires for a struct
=== RUN TestOptionalTimestampsOmitWhenAbsent/Notification
optional_timestamps_test.go:112: Notification.read_at was absent on the wire but re-marshaled as "0001-01-01T00:00:00Z"; a value-typed time.Time cannot represent absence and `,omitempty` never fires for a struct
optional_timestamps_test.go:112: Notification.unread_at was absent on the wire but re-marshaled as "0001-01-01T00:00:00Z"; a value-typed time.Time cannot represent absence and `,omitempty` never fires for a struct
=== RUN TestOptionalTimestampsOmitWhenAbsent/SearchResult
optional_timestamps_test.go:112: SearchResult.created_at was absent on the wire but re-marshaled as "0001-01-01T00:00:00Z"; a value-typed time.Time cannot represent absence and `,omitempty` never fires for a struct
optional_timestamps_test.go:112: SearchResult.updated_at was absent on the wire but re-marshaled as "0001-01-01T00:00:00Z"; a value-typed time.Time cannot represent absence and `,omitempty` never fires for a struct
--- FAIL: TestOptionalTimestampsOmitWhenAbsent (0.00s)
--- FAIL: TestOptionalTimestampsOmitWhenAbsent/HillChart (0.00s)
--- FAIL: TestOptionalTimestampsOmitWhenAbsent/Notification (0.00s)
--- FAIL: TestOptionalTimestampsOmitWhenAbsent/SearchResult (0.00s)
=== RUN TestOptionalTimestampsRoundTripWhenPresent
=== RUN TestOptionalTimestampsRoundTripWhenPresent/HillChart
=== RUN TestOptionalTimestampsRoundTripWhenPresent/Notification
=== RUN TestOptionalTimestampsRoundTripWhenPresent/SearchResult
--- PASS: TestOptionalTimestampsRoundTripWhenPresent (0.00s)
--- PASS: TestOptionalTimestampsRoundTripWhenPresent/HillChart (0.00s)
--- PASS: TestOptionalTimestampsRoundTripWhenPresent/Notification (0.00s)
--- PASS: TestOptionalTimestampsRoundTripWhenPresent/SearchResult (0.00s)
=== RUN TestOptionalTimestampsSurviveGeneratedConversion
=== RUN TestOptionalTimestampsSurviveGeneratedConversion/HillChart
optional_timestamps_test.go:168: HillChart.updated_at: generated value carried a nil timestamp but the wrapper emitted "0001-01-01T00:00:00Z"
=== RUN TestOptionalTimestampsSurviveGeneratedConversion/SearchResult
optional_timestamps_test.go:168: SearchResult.created_at: generated value carried a nil timestamp but the wrapper emitted "0001-01-01T00:00:00Z"
optional_timestamps_test.go:168: SearchResult.updated_at: generated value carried a nil timestamp but the wrapper emitted "0001-01-01T00:00:00Z"
--- FAIL: TestOptionalTimestampsSurviveGeneratedConversion (0.00s)
--- FAIL: TestOptionalTimestampsSurviveGeneratedConversion/HillChart (0.00s)
--- FAIL: TestOptionalTimestampsSurviveGeneratedConversion/SearchResult (0.00s)
=== RUN TestNilOptionalTimestampPanicsOnValueReceiverCall
optional_timestamps_test.go:191: expected a nil-pointer panic from IsZero() on an absent UpdatedAt: if this line is reached the field is still value-typed and absence reads as 0001-01-01T00:00:00Z
--- FAIL: TestNilOptionalTimestampPanicsOnValueReceiverCall (0.00s)
=== RUN TestNoValueTypedOptionalTimestamps
optional_timestamps_test.go:280: optional timestamp is value-typed and cannot represent absence (`,omitempty` is inert for a struct, so the key is emitted as 0001-01-01T00:00:00Z): hill_charts.go:15:2: UpdatedAt time.Time `json:"updated_at,omitempty"`
optional_timestamps_test.go:280: optional timestamp is value-typed and cannot represent absence (`,omitempty` is inert for a struct, so the key is emitted as 0001-01-01T00:00:00Z): my_notifications.go:46:2: ReadAt time.Time `json:"read_at,omitempty"`
optional_timestamps_test.go:280: optional timestamp is value-typed and cannot represent absence (`,omitempty` is inert for a struct, so the key is emitted as 0001-01-01T00:00:00Z): my_notifications.go:47:2: UnreadAt time.Time `json:"unread_at,omitempty"`
--- FAIL: TestNoValueTypedOptionalTimestamps (0.03s)
FAIL
FAIL github.com/basecamp/basecamp-sdk/go/pkg/basecamp 0.310s
FAIL
REAL_EXIT=1
Note what TestNilOptionalTimestampPanicsOnValueReceiverCall actually proves. Pre-fix, hc.UpdatedAt.IsZero() on an absent timestamp does not panic and does not complain — it silently answers true. That silence is the disease. The panic is the post-fix migration hazard, and the test asserts it so the hazard lives in code rather than only in this description.
TestOptionalTimestampsRoundTripWhenPresent is the positive control and passes on both trees, so the fix cannot have been "omit everything".
Note also that the AST guard's red run lists only three of the five — that is not a defect in the run, it is the guard's coverage, and the next section is about exactly that.
Post-fix, all six subtests pass — see verification below.
Why the existing gate did not catch this, and what is still unguarded
scripts/check-go-optional-pointers enforces this invariant on the generated client, so the first question was whether it should have fired. Stating the answer plainly, because an earlier version of this description and a comment on #562 both got it wrong in the direction that hides a bug:
Pointed at the wrapper surface, it would have caught three of the five. HillChart.UpdatedAt, Notification.ReadAt and Notification.UnreadAt are value-typed and carry ,omitempty, which is exactly its violation shape. Run against the pre-fix files it exits 1 on each:
$ scripts/check-go-optional-pointers <pre-fix hill_charts.go>
check-go-optional-pointers: 5 optional field(s) cannot represent absence (non-pointer value type with omitempty):
hill_charts.go:15: UpdatedAt time.Time
…
REAL_EXIT=1
$ scripts/check-go-optional-pointers <pre-fix my_notifications.go>
my_notifications.go:46: ReadAt time.Time
my_notifications.go:47: UnreadAt time.Time
REAL_EXIT=1
It caught none of them only because make go-check-optional-pointers invokes it with no argument:
path = ARGV[0] || File.expand_path("../go/pkg/generated/client.gen.go", __dir__)
so its entire domain is go/pkg/generated/client.gen.go. That scoping should nonetheless stay: pointed at the whole pre-fix wrapper surface it reports 345 violations, because this package flattens optional *string / *bool / *int to value types on purpose — that is what makes it the "clean" surface.
It could not have caught the other two at any scope. It keys entirely on ,omitempty (next unless m[3].match?(OMITEMPTY)), and SearchResult.CreatedAt/UpdatedAt were tagged json:"created_at" with no options — tagged as if required. Run against the pre-fix search.go it reports four violations and neither timestamp is among them.
So this PR adds TestNoValueTypedOptionalTimestamps: an AST guard over go/pkg/basecamp/*.go rejecting any value-typed time.Time carrying ,omitempty in any tag key. It runs under make go-test — no new Makefile target, no new CI wiring, nothing for a sibling lane to collide with. It asserts it scanned a non-zero number of time.Time fields, so a broken walk fails loudly instead of reporting success forever.
Be clear about what that guard does not do. It inherits the same ,omitempty blind spot (if !hasOmitempty(tag) { continue }), so it would not have caught the two SearchResult fields either, and it does not catch the five holdouts in #620 — all five are tagged without options. It prevents regression of the three-field shape, not of the class.
Two gaps stay open, reported rather than quietly closed:
-
Type-level wrapper/generated drift is unguarded — this is the one that matters.
scripts/check-wrapper-driftalready computes the(wrapper, generated)struct pairing from the*FromGeneratedconverters, but compares JSON tag names only. A wrapper field typedtime.Timewhose generated counterpart is*time.Timepasses it cleanly. That is the only place in the tree holding both sides of the comparison, and it is where the 10-field cross-reference above belongs as a permanent gate. Two limits for whoever writes it:Notificationhas no*FromGeneratedconverter (directjson.Unmarshal), so converter-derived pairing misses it; and the comparison must be scoped to timestamps or it re-derives the 345-violation wall. Tracked in #620. -
Six non-timestamp fields are in the same inert-
omitemptyclass:go/pkg/basecamp/account_service.go:28: Limits AccountLimits `json:"limits,omitempty"` go/pkg/basecamp/account_service.go:29: Settings AccountSettings `json:"settings,omitempty"` go/pkg/basecamp/account_service.go:30: Subscription AccountSubscription `json:"subscription,omitempty"` go/pkg/basecamp/my_assignments.go:24: Bucket MyAssignmentBucket `json:"bucket,omitempty"` go/pkg/basecamp/my_assignments.go:25: Parent MyAssignmentParent `json:"parent,omitempty"` go/pkg/basecamp/people.go:571: Person OutOfOfficePerson `json:"person,omitempty"`Named structs, so
omitemptyis equally inert, and each emits a fully zero-valued nested object instead of being omitted. Less harmful than a fabricated instant — a zero{}reads as obviously empty in a way0001-01-01T00:00:00Zdoes not — and outside this lane, so they are named in #620 rather than swept in.
Review findings, all real, all fixed
1. The SDK's own godoc demonstrated the hazard (chatgpt-codex-connector). ExampleSearchService_Search_sorted called r.CreatedAt.Format(…) with no nil check — the exact unsafe pattern this PR warns about, shipped in the documentation. Fixed in 1fa9c13b4. Reproduced literally first: that expression against a SearchResult{Title: "Kickoff notes"} panics with runtime error: invalid memory address or nil pointer dereference.
make check was green over it because godoc examples without an // Output: comment are compiled but never executed, and because TestNoValueTypedOptionalTimestamps is a declaration guard — it can prove no field is declared with the unsafe shape, but it cannot see an unsafe use.
So rather than fix the one named site and hope, I enumerated the class with the compiler. In a scratch copy all five fields became a defined pointer type with an empty method set:
type probeTime *time.Time // defined, not an alias: no method set, no selector auto-deref
Every auto-dereferencing use site then becomes a compile error, so the type checker lists them exhaustively. Three passes, neutralizing each batch to reach the next package:
pkg/basecamp/hill_charts_test.go:84:18: hc.UpdatedAt.IsZero undefined (type probeTime has no field or method IsZero)
pkg/basecamp/hill_charts_test.go:87:18: hc.UpdatedAt.Year undefined (type probeTime has no field or method Year)
pkg/basecamp/hill_charts_test.go:88:55: hc.UpdatedAt.Year undefined (type probeTime has no field or method Year)
pkg/basecamp/optional_timestamps_test.go:196:19: hc.UpdatedAt.IsZero undefined (type probeTime has no field or method IsZero)
pkg/basecamp/search_test.go:193:25: r1.CreatedAt.IsZero undefined (type probeTime has no field or method IsZero)
pkg/basecamp/search_test.go:198:25: r1.UpdatedAt.IsZero undefined (type probeTime has no field or method IsZero)
vet: pkg/basecamp/example_test.go:255:26: r.CreatedAt.Format undefined (type basecamp.probeTime has no field or method Format)
$ go vet ./...
REAL_EXIT=0
Seven sites in the whole module; the final pass is clean. Six are deliberately guarded (hill_charts_test.go behind if hc.UpdatedAt == nil, search_test.go behind if … == nil { … } else if, and optional_timestamps_test.go:196 is the intentional nil-deref in the panic test). The seventh was the example. The probe is a one-off technique, not a committed guard — a permanent version needs a type-aware nilness pass, which belongs with the check-wrapper-drift gap above.
2. A doc cross-reference that named a field that does not exist (copilot-pull-request-reviewer). The HillChart.UpdatedAt comment cited TimelineEvent.UpdatedAt as precedent. TimelineEvent (timeline.go:18) has no UpdatedAt; the optional *time.Time pair at timeline.go:85-86 belongs to TimelineAttachment (timeline.go:62). Now cites EverythingFile.UpdatedAt (everything.go:64), re-resolved to its declaring struct rather than assumed. Fixed in 1ec2c7583.
That error came from #562's own table. My correction on the issue then overshot — it called TimelineEvent.CreatedAt "required and value-typed", when structure TimelineEvent carries no @required on created_at and the generated client types it *time.Time. It is value-typed, and it is a holdout, not a counter-example. Re-corrected on the issue and tracked in #620.
3. A planning doc left stale by this change (copilot-pull-request-reviewer, raised in the review body rather than as an inline thread). go/BRIEF-bc5-forward-compat-wrappers.md:147 told the next author to type Notification.BubbleUpAt as *time.Time "rather than the bare time.Time pattern the wrapper uses for ReadAt / UnreadAt" — a counter-example this PR deletes. The advice was right and its only concrete anchor inverted. Now cites Card.CompletedAt (cards.go:101), Todo.CompletedAt (todos.go:52) and the newly-pointerized ReadAt/UnreadAt (my_notifications.go:52-53), each re-resolved to source.
Scope
Go only. No spec change: spec/basecamp.smithy already marks all five optional (no @required on HillChart.updated_at, Notification.read_at / unread_at, SearchResult.created_at / updated_at), and the generated client already types them *time.Time — the wrapper was the only thing out of step. spec/basecamp.smithy and openapi.json are untouched, as is every generated artifact.
No conformance fixture and no runner dispatch: nothing under conformance/runner/** reads any of the five fields, so no runner gains a new case in any of the six.