Skip to content

Commit a4bafa0

Browse files
Dillon Nysdnys1
authored andcommitted
fix(auth): Fix device serialization
Fixes #1696 Fixes device serialization on iOS when just an ID is passed. I confirmed this issue does not exist on Android. commit-id:8d6c6d72
1 parent 334b5a6 commit a4bafa0

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

packages/auth/amplify_auth_cognito_ios/example/ios/unit_tests/amplify_auth_cognito_ios_tests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,4 +2055,16 @@ class amplify_auth_cognito_tests: XCTestCase {
20552055
})
20562056
}
20572057

2058+
func test_decode_and_encode_of_device() throws {
2059+
let deviceJson = [
2060+
"id": "123"
2061+
]
2062+
let data = try JSONSerialization.data(withJSONObject: deviceJson)
2063+
let device = try DeviceHandler.decoder.decode(AWSAuthDevice.self, from: data)
2064+
XCTAssertEqual(device.id, "123")
2065+
let encodedDeviceData = try DeviceHandler.encoder.encode(device)
2066+
let encodedDevice = try JSONSerialization.jsonObject(with: encodedDeviceData) as? [String: String?]
2067+
XCTAssertEqual(encodedDevice?["id"], "123")
2068+
}
2069+
20582070
}

packages/auth/amplify_auth_cognito_ios/ios/Classes/Device/AWSAuthDevice+Codable.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import AmplifyPlugins
2121
extension AWSAuthDevice: Codable {
2222
/// Attribute key for retrieving a device's name.
2323
static let deviceNameKey = "device_name"
24-
24+
2525
enum CodingKeys: String, CodingKey {
2626
case id
2727
case name
@@ -30,24 +30,24 @@ extension AWSAuthDevice: Codable {
3030
case lastAuthenticatedDate
3131
case lastModifiedDate
3232
}
33-
33+
3434
public init(from decoder: Decoder) throws {
3535
let container = try decoder.container(keyedBy: CodingKeys.self)
3636
let id = try container.decode(String.self, forKey: .id)
37-
let name = try container.decode(String.self, forKey: .name)
38-
let attributes = try container.decode([String: String]?.self, forKey: .attributes)
39-
let createdDate = try container.decode(Date?.self, forKey: .createdDate)
40-
let lastAuthenticatedDate = try container.decode(Date?.self, forKey: .lastAuthenticatedDate)
41-
let lastModifiedDate = try container.decode(Date?.self, forKey: .lastModifiedDate)
37+
let name = try container.decodeIfPresent(String?.self, forKey: .name) ?? nil
38+
let attributes = try container.decodeIfPresent([String: String]?.self, forKey: .attributes) ?? nil
39+
let createdDate = try container.decodeIfPresent(Date?.self, forKey: .createdDate) ?? nil
40+
let lastAuthenticatedDate = try container.decodeIfPresent(Date?.self, forKey: .lastAuthenticatedDate) ?? nil
41+
let lastModifiedDate = try container.decodeIfPresent(Date?.self, forKey: .lastModifiedDate) ?? nil
4242
self.init(
4343
id: id,
44-
name: name,
44+
name: name ?? "",
4545
attributes: attributes,
4646
createdDate: createdDate,
4747
lastAuthenticatedDate: lastAuthenticatedDate,
4848
lastModifiedDate: lastModifiedDate)
4949
}
50-
50+
5151
public func encode(to encoder: Encoder) throws {
5252
var container = encoder.container(keyedBy: CodingKeys.self)
5353
try container.encode(id, forKey: .id)

packages/auth/amplify_auth_cognito_ios/ios/Classes/Device/DeviceHandler.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ struct DeviceHandler {
2828
attributes: [.concurrent])
2929

3030
/// Encoder used for proper [Date] handling.
31-
private static let encoder = JSONEncoder(dateEncodingStrategy: .millisecondsSince1970)
31+
internal static let encoder = JSONEncoder(dateEncodingStrategy: .millisecondsSince1970)
3232

3333
/// Decoder used for proper [Date] handling.
34-
private static let decoder = JSONDecoder(dateDecodingStrategy: .millisecondsSince1970)
34+
internal static let decoder = JSONDecoder(dateDecodingStrategy: .millisecondsSince1970)
3535

3636
/// Methods handled by [DeviceHandler].
3737
private static let methods: Set<String> = ["rememberDevice", "forgetDevice", "fetchDevices"]

0 commit comments

Comments
 (0)