Skip to content

Commit f655813

Browse files
Fixed an issue where decoded CodableContextKey values became inaccesible after the first access (#7)
1 parent fe22deb commit f655813

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

Sources/ApodiniContext/Context.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ public struct Context: ContextKeyRetrievable {
121121
}
122122

123123
do {
124-
return try Key.decode(from: dataValue)
124+
let value = try Key.decode(from: dataValue)
125+
boxedEntries.entries[ObjectIdentifier(key)] = StoredContextValue(key: key, value: value)
126+
return value
125127
} catch {
126128
fatalError("Error occurred when trying to decode `CodableContextKey` `\(Key.self)` with stored value '\(dataValue)': \(error)")
127129
}

Tests/ApodiniContextTests/ContextKeyTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ class ContextKeyTests: XCTestCase {
141141
)
142142

143143
let decodedContext = try decoder.decode(Context.self, from: encodedContext)
144+
XCTAssertEqual(decodedContext.get(valueFor: CodableStringContextKey.self), "Hello Mars")
145+
XCTAssertEqual(decodedContext.get(valueFor: CodableStringContextKey.self), "Hello Mars")
144146

145147
decodedContext.unsafeAdd(CodableStringContextKey.self, value: "Hello Saturn", allowOverwrite: true)
146148
XCTAssertEqual(decodedContext.get(valueFor: CodableStringContextKey.self), "Hello Saturn")

0 commit comments

Comments
 (0)