Decode TSession.userRoles with backward compatibility#79
Merged
Conversation
Sessions persisted before #76 added userRoles lack the key, so the synthesized Codable decoder throws .keyNotFound. In TidepoolService that kills init?(rawState:), which drops the saved service entry and forces the user to re-authenticate on the next upgrade. Provide a custom init(from:) that uses decodeIfPresent for userRoles defaulting to []. Keeps the public API unchanged (userRoles stays non-optional) and adds a backward-compat test.
CircleCI's m4pro.medium resource class does not support Xcode 14.1.0. Use Xcode 16.4 and drop the OS pin on the simulator destination.
Newer Foundation emits Doubles at full IEEE-754 precision (e.g. 2.34 -> 2.3399...) and key order can differ between JSONEncoder and JSONSerialization output -- both of which broke the previous raw string comparison even though the data is equivalent. Parse both sides back to Foundation objects and compare via NSObject equality, which handles nested values, key ordering, and number equivalence by value. Brings 46 pre-existing test failures back to green on current SDKs (and preserves the encode/decode round-trip assertions).
Camji55
approved these changes
Jun 1, 2026
loopkitdev
pushed a commit
to loopkitdev/LoopWorkspace
that referenced
this pull request
Jun 1, 2026
Includes tidepool-org/TidepoolKit#79 so sessions persisted before userRoles was added decode cleanly on upgrade -- preserves the TidepoolService configuration across the dev->sync upgrade instead of forcing the user to re-authenticate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TSession.userRoles(added in [LOOP-5150] storing user roles with session #76) is a non-optional[String], so the synthesizedCodabledecoder throws.keyNotFoundwhen reading a session JSON persisted before that field existed.TidepoolServiceKit.TidepoolService.init?(rawState:)), this throw bubbles up throughKeychainManager.getSession(for:)and causes the wholeinit?to returnnil, dropping the saved service entry and forcing the user to re-authenticate (and re-add the Tidepool service) after an upgrade.init(from:)that usesdecodeIfPresentforuserRoleswith a[]default. Old JSON decodes cleanly; new JSON unchanged. The public API stays the same (userRolesremains non-optional[String]), so no caller changes are needed.testDecodingLegacySessionWithoutUserRolesYieldsEmptyto lock this in.Test plan
testDecodingLegacySessionWithoutUserRolesYieldsEmptypasses.testInitializerstill passes.testCodableAsJSONfailure ondevis unrelated (JSON key-order mismatch — fails ondevbefore this PR too).