Decode person ids in one pass in Swift and Kotlin

Merged
JE
1d ago
+1739-73

Swift and Kotlin rewrote every response body that mentioned personable_type before decoding it: parse to a tree, turn a sentinel person id ("basecamp", "campfire") into 0 plus system_label, serialize the tree again, then do the typed decode. That's three passes over nearly every body, since nearly every record embeds a creator. This change moves the rewrite into the generated Person's own decoder, so a body is decoded once.

A person on the reviewer side needs to read this diff before it lands. It changes generators and the person-id contract.

What

  • Swift: the generator gives Person.init(from:) one call, container.decodePersonID(id:systemLabel:personableType:) (hand-written, swift/Sources/Basecamp/PersonList.swift). It reads the id once through FlexibleInt's reader (now FlexibleInt.read(from:), which also returns the sentinel text) and sets the label. BaseService passes response bytes straight to BaseService.decoder.
  • Kotlin: the generator marks Person @KeepGeneratedSerializer @Serializable(with = PersonSerializer::class) and emits internal object PersonSerializer : OnePassPersonSerializer<Person>(Person.generatedSerializer()). OnePassPersonSerializer (hand-written, kotlin/.../serialization/) runs the plugin's own serializer through a decoder that forwards every call except two. It reads the id element once as a JsonElement so it can see a quoted sentinel. It holds the system_label element until the object ends, because personable_type and id may come after it. Nothing is re-encoded. FlexibleLongSerializer.fromElement is the reader, split out unchanged. BaseService no longer calls normalizePersonIds. That includes error bodies, where it did nothing useful. A response Kotlin returns untyped (JsonElement: gauges, notifications, assignments and others) has no Person serializer to do the labelling, so the generator now runs the normalizer over the tree that service already parsed (normalizePersonIds(JsonElement), internal, with no re-encode). Callers still get 0 plus system_label there, and UntypedPersonIdsTest pins this through the public methods.
  • The rule is the normalizer's exactly. A syntax refusal on an object whose personable_type key is present (with any value, null included) reads as id 0 with system_label set to the id's text, and the body's own system_label is never read in that case, in either member order. A range refusal still fails the read. With no personable_type, the reader gives 0 and there's no label, as before.
  • normalizePersonIds stays in both SDKs (public in Kotlin, internal in Swift) as the oracle the new tests use. Its existing tests are untouched and pass.
  • SPEC.md "Person Ids Off the Wire" and the AGENTS.md hand-written table are updated.

Behavior that narrows, on purpose

The normalizer reached any object with personable_type, whatever type it decoded into. The one pass reaches the generated Person, which is where Go reads a person id. Go runs no pre-decode pass on these paths. Two latent differences follow, both now matching Go:

  • A string id on a person-shaped type that isn't Person (UpcomingSchedulePerson, MyAssignmentAssignee, OutOfOfficePerson, TemplateLibraryConfirmationPerson, whose ids are plain int64 in Go) used to be rewritten. Now it fails the read as it does in Go. BC3 sends integers at all of them today. SPEC already rules these sites strict for the positional pass.
  • A free-form JSON member (WebhookEvent.details, and Swift's FeedEvent.details) now carries a person id as the wire sent it, not rewritten.

Nothing BC3 sends is lost. The rewrite keyed on the personable_type key on the wire. Person is the only schema in openapi.json that declares it, and it's the only BC3 partial that renders it. The four plain-id shapes are BC3's people/_person_minimal partial (id, name, avatar_url):

  • UpcomingSchedulePerson: api/schedules/calendar/_entry and _assignable
  • MyAssignmentAssignee: api/my/assignments/_assignable
  • OutOfOfficePerson: api/out_of_offices/show
  • TemplateLibraryConfirmationPerson: api/template_library/copies/add_people_confirmation

The rewrite never touched any of them. A sentinel there failed the read before, and it still does, as in Go. SPEC.md records this per schema. I chose that over extending the one pass to those shapes. Extending it would make their ids flexible, which is a spec and Go change for a value BC3 can't put there with the key the rewrite needed.

Differential over the whole corpus (PersonIdReachDifferentialTest in Kotlin, PersonIdReachDifferentialTests in Swift). The corpus is every spec/fixtures file plus every conformance/tests mock body: 531 bodies. Each body is decoded into every generated model through the old pipeline (normalizePersonIds, then decode) and through the new one. Comparing every model is a superset of each operation's response type and needs no mapping.

  • As written: 531 bodies × 264 models in Swift (140,184 pairs) and × 108 models in Kotlin (57,348 pairs), 0 differences. No corpus body carries a sentinel, so this pass only shows that nothing else moved.
  • Seeded: each personable_type object's id is set to "basecamp" in turn (305 seeded bodies), then decoded again into every model the body decodes into. That's 31,197 pairs in Swift and 10,804 in Kotlin. No difference lands on Person. There are 372 differences in Swift and 303 in Kotlin, and each one was traced down the OpenAPI schema to where the seeded object lands. Every one lands on a schema whose id the reference reads as a strict int64. These are a person body decoded as a model it isn't, such as a campfire's creator read as UpcomingAssignableCompletion.
  • The real response types: under its operation's real 2xx response schema, every personable_type object in the conformance bodies (55 of 55) lands on Person.
  • Guard: both tests fail if any schema other than Person declares personable_type. Such a schema would need the one pass.

Neither narrowing changes a public signature. I've labelled this enhancement rather than breaking, so there's no MIGRATING entry. If you want these latent narrowings released as breaking, relabel it.

Tests

  • PersonIdDecodeTests (Swift) and OnePassPersonDecodeTest (Kotlin) run the old pipeline as the oracle: normalize the text, then decode. Kotlin decodes with the plugin's untouched Person.generatedSerializer(). Each test requires the one pass to give the same person, or to fail where the old pipeline failed, on every body in a grid. The grid crosses 86 id spellings (the 74-row corpus quoted, plus bare literals, 7.0, 1e3, null, true, [], {}, and absent) with 5 personable_type shapes ("User", "LocalPerson", null, wrong-typed, absent) and 9 system_label placements (string, null, wrong-typed or object, each before or after id, plus absent). That's 3,870 bodies per SDK. Both also cover nested and list positions, and Kotlin covers an encode round trip.
  • Mutation check: with the sentinel label disabled, the Swift suite failed 1,009 assertions (decodePersonID) and 4 of the 5 Kotlin OnePassPersonDecodeTest tests failed (OnePassPersonSerializer).
  • Swift: the full swift test suite ran locally, 671 tests passing, before the differential was added. Kotlin: the full jvmTest suite ran locally, 803 tests passing.

Benchmark

The body is a 50-todo list (about 165 KB). Each todo carries a creator, two assignees and a completion subscriber, all full people with personable_type. Each round decodes it 200 times and records the mean per decode. There were 9 rounds of old and new, interleaved, and I report the median round. "Old" is normalizePersonIds followed by the decode; "new" is the decode alone. Both hosts were heavily loaded (Mac load 40 to 75), so the spread is wide but the medians are clearly separated.

old (median)new (median)
Swift, Mac, swift test -c release37.6 ms10.2 ms3.7x
Kotlin JVM, thelio, after 3 warm-up rounds1.21 ms0.48 ms2.5x

Swift rounds, old: 24.8 to 58.2 ms; new: 7.0 to 16.8 ms. Kotlin rounds, old: 1.17 to 1.28 ms; new: 0.45 to 0.49 ms. The benchmark harness was a throwaway test and isn't committed.

Outside review

I ran one outside review on db38678, verified both findings, and fixed both on 7694e33:

  • Medium. Kotlin's untyped (JsonElement) responses had lost the rewrite. This was real, and it's fixed as described above. The new tests fail on db38678, where those ids come back as the wire's strings.
  • Low. Swift's differential runs the new decoder on both sides, so a change to how an ordinary system_label is read would not show. I added outright assertions: a numeric id keeps its wire label, an absent id keeps its wire label, an object label fails, and a sentinel overrides an object label in both orders.

Siblings

  • #1142 (response cache): its Kotlin and Swift hunks call normalizePersonIds before decoding. After this change, a cached body decodes onto the model with no pre-pass, and gets the same person. Those calls can simply go when it rebases.
  • #1140 (typed dates): nothing here touches the person's other members. Kotlin composes through the plugin serializer, so a typed created_at needs no change here.