diff --git a/Sources/Commons/AnyCodable.swift b/Sources/Commons/AnyCodable.swift index 34fe575af..52552e344 100644 --- a/Sources/Commons/AnyCodable.swift +++ b/Sources/Commons/AnyCodable.swift @@ -137,7 +137,12 @@ extension AnyCodable: Decodable, Encodable { if let container = try? decoder.container(keyedBy: CodingKeys.self) { var result = [String: Any]() try container.allKeys.forEach { (key) throws in - result[key.stringValue] = try container.decode(AnyCodable.self, forKey: key).value + do { + let codable = try container.decode(AnyCodable.self, forKey: key) + result[key.stringValue] = codable.value + } catch AnyCodableError.nullFound { + // Ignoring that key + } } value = result } else if var container = try? decoder.unkeyedContainer() { @@ -155,6 +160,8 @@ extension AnyCodable: Decodable, Encodable { value = boolVal } else if let stringVal = try? container.decode(String.self) { value = stringVal + } else if container.decodeNil() { + throw AnyCodableError.nullFound } else { throw DecodingError.dataCorruptedError(in: container, debugDescription: "The container contains nothing serializable.") } @@ -179,6 +186,8 @@ extension AnyCodable: Decodable, Encodable { let decodable = AnyCodable(value) try container.encode(decodable, forKey: codingKey) } + } else if value is NSNull { + // ignoring that key } else { var container = encoder.singleValueContainer() if let intVal = value as? Int { diff --git a/Sources/Commons/AnyCodableError.swift b/Sources/Commons/AnyCodableError.swift new file mode 100644 index 000000000..42b9c44c6 --- /dev/null +++ b/Sources/Commons/AnyCodableError.swift @@ -0,0 +1,5 @@ +import Foundation + +public enum AnyCodableError: Error { + case nullFound +} diff --git a/Sources/WalletConnectRelay/EnvironmentInfo.swift b/Sources/WalletConnectRelay/EnvironmentInfo.swift index a32faf141..a8866ba7f 100644 --- a/Sources/WalletConnectRelay/EnvironmentInfo.swift +++ b/Sources/WalletConnectRelay/EnvironmentInfo.swift @@ -18,7 +18,7 @@ enum EnvironmentInfo { } static var sdkVersion: String { - "v1.0.3" + "v1.0.4" } static var operatingSystem: String { diff --git a/Tests/CommonsTests/AnyCodableTests.swift b/Tests/CommonsTests/AnyCodableTests.swift index 05f42c9f2..bce2de631 100644 --- a/Tests/CommonsTests/AnyCodableTests.swift +++ b/Tests/CommonsTests/AnyCodableTests.swift @@ -111,6 +111,12 @@ final class AnyCodableTests: XCTestCase { XCTAssertNotEqual(a, b) } + func testNullDecoding() throws { + let data = "{\"key\":\"value\",\"null\":null}".data(using: .utf8)! + let codable = try JSONDecoder().decode(AnyCodable.self, from: data) + XCTAssertEqual(codable, AnyCodable(["key": "value"])) + } + func testTwoWayDataRepresentation() throws { let fromInit = AnyCodable(SampleStruct.stubFixed()) let fromJSON = try JSONDecoder().decode(AnyCodable.self, from: SampleStruct.sampleJSONData) @@ -243,7 +249,7 @@ final class AnyCodableTests: XCTestCase { } let nullData = "null".data(using: .utf8)! XCTAssertThrowsError(try JSONDecoder().decode(AnyCodable.self, from: nullData)) { error in - XCTAssert(error is DecodingError) + XCTAssert(error is AnyCodableError) } } }