From 03d82c275be588970eda4d5ab335609a8260014b Mon Sep 17 00:00:00 2001 From: apgupta3303 <98926720+apgupta3303@users.noreply.github.com> Date: Wed, 13 Mar 2024 13:47:24 -0400 Subject: [PATCH] Fixing decoding times if times was never encoded error (#12) # [*Fixing Decoding Times Error*](https://github.com/StanfordSpezi/SpeziMedication/pull/12#issue-2182904376) ## :gear: Release Notes - This PR changes the decoding schedule, so it only decodes the times field if it is present. This is necessary because times is not encoded if it was empty, so then it errors when trying to decode it if it was never encoded. ## :pencil: Code of Conduct & Contributing Guidelines By submitting creating this pull request, you agree to follow our [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md): - [x] I agree to follow the [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md). --- Sources/SpeziMedication/Models/Schedule.swift | 2 +- .../SpeziMedicationTests.swift | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/Sources/SpeziMedication/Models/Schedule.swift b/Sources/SpeziMedication/Models/Schedule.swift index 15891ea..26d82d2 100644 --- a/Sources/SpeziMedication/Models/Schedule.swift +++ b/Sources/SpeziMedication/Models/Schedule.swift @@ -38,7 +38,7 @@ public struct Schedule: Codable, Equatable, Hashable { public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) self.frequency = try container.decode(Frequency.self, forKey: .frequency) - self.times = try container.decode([ScheduledTime].self, forKey: .times) + self.times = try container.decodeIfPresent([ScheduledTime].self, forKey: .times) ?? [] self.startDate = try container.decode(Date.self, forKey: .startDate) } diff --git a/Tests/SpeziMedicationTests/SpeziMedicationTests.swift b/Tests/SpeziMedicationTests/SpeziMedicationTests.swift index a8fcf58..8a4766f 100644 --- a/Tests/SpeziMedicationTests/SpeziMedicationTests.swift +++ b/Tests/SpeziMedicationTests/SpeziMedicationTests.swift @@ -11,6 +11,59 @@ import XCTest final class SpeziMedicationTests: XCTestCase { + func testScheduleDecodingWithAndWithoutTimes() throws { + let jsonDecoder = JSONDecoder() + jsonDecoder.dateDecodingStrategy = .iso8601 + + // Test decoding without times + let noTimesJSON = """ + { + "frequency": { + "asNeeded": true + }, + "startDate": "2023-12-07T08:00:00Z" + } + """ + guard let data = noTimesJSON.data(using: .utf8) else { + XCTFail("Failed to encode JSON string to Data") + return + } + let noTimesSchedule = try jsonDecoder.decode(Schedule.self, from: data) + XCTAssertEqual(noTimesSchedule.times, []) + + // Test decoding with times + let withTimesJSON = """ + { + "frequency": { + "regularDayIntervals": 2 + }, + "startDate": "2023-12-07T08:00:00Z", + "times": [ + { + "time": { + "hour": 8, + "minute": 0 + }, + "dosage": 5.0 + }, + { + "time": { + "hour": 15, + "minute": 0 + }, + "dosage": 5.0 + } + ] + } + """ + guard let dataWithTimes = withTimesJSON.data(using: .utf8) else { + XCTFail("Failed to encode JSON string to Data") + return + } + let withTimesSchedule = try jsonDecoder.decode(Schedule.self, from: dataWithTimes) + XCTAssertNotNil(withTimesSchedule.times) + } + // swiftlint:disable:next function_body_length func testScheduleEncoding() throws { let jsonEncoder = JSONEncoder()