Decode person ids in one pass in Swift and Kotlin
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 throughFlexibleInt's reader (nowFlexibleInt.read(from:), which also returns the sentinel text) and sets the label.BaseServicepasses response bytes straight toBaseService.decoder. - Kotlin: the generator marks
Person@KeepGeneratedSerializer @Serializable(with = PersonSerializer::class)and emitsinternal 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 theidelement once as aJsonElementso it can see a quoted sentinel. It holds thesystem_labelelement until the object ends, becausepersonable_typeandidmay come after it. Nothing is re-encoded.FlexibleLongSerializer.fromElementis the reader, split out unchanged.BaseServiceno longer callsnormalizePersonIds. That includes error bodies, where it did nothing useful. A response Kotlin returns untyped (JsonElement: gauges, notifications, assignments and others) has noPersonserializer 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 get0plussystem_labelthere, andUntypedPersonIdsTestpins this through the public methods. - The rule is the normalizer's exactly. A syntax refusal on an object whose
personable_typekey is present (with any value,nullincluded) reads as id0withsystem_labelset to the id's text, and the body's ownsystem_labelis never read in that case, in either member order. A range refusal still fails the read. With nopersonable_type, the reader gives0and there's no label, as before. normalizePersonIdsstays 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 plainint64in 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'sFeedEvent.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/_entryand_assignableMyAssignmentAssignee:api/my/assignments/_assignableOutOfOfficePerson:api/out_of_offices/showTemplateLibraryConfirmationPerson: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_typeobject'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 onPerson. 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 strictint64. These are a person body decoded as a model it isn't, such as a campfire's creator read asUpcomingAssignableCompletion. - The real response types: under its operation's real 2xx response schema, every
personable_typeobject in the conformance bodies (55 of 55) lands onPerson. - Guard: both tests fail if any schema other than
Persondeclarespersonable_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) andOnePassPersonDecodeTest(Kotlin) run the old pipeline as the oracle: normalize the text, then decode. Kotlin decodes with the plugin's untouchedPerson.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 5personable_typeshapes ("User","LocalPerson",null, wrong-typed, absent) and 9system_labelplacements (string,null, wrong-typed or object, each before or afterid, 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 KotlinOnePassPersonDecodeTesttests failed (OnePassPersonSerializer). - Swift: the full
swift testsuite ran locally, 671 tests passing, before the differential was added. Kotlin: the fulljvmTestsuite 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 release | 37.6 ms | 10.2 ms | 3.7x |
| Kotlin JVM, thelio, after 3 warm-up rounds | 1.21 ms | 0.48 ms | 2.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 ondb38678, 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_labelis 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
normalizePersonIdsbefore 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_atneeds no change here.