Skip to content

Commit

Permalink
Merge pull request #565 from WalletConnect/develop
Browse files Browse the repository at this point in the history
Hotfix v1.0.4
  • Loading branch information
llbartekll authored Nov 2, 2022
2 parents 849b5eb + eb6cfd2 commit 7ccf26c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
11 changes: 10 additions & 1 deletion Sources/Commons/AnyCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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.")
}
Expand All @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions Sources/Commons/AnyCodableError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

public enum AnyCodableError: Error {
case nullFound
}
2 changes: 1 addition & 1 deletion Sources/WalletConnectRelay/EnvironmentInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum EnvironmentInfo {
}

static var sdkVersion: String {
"v1.0.3"
"v1.0.4"
}

static var operatingSystem: String {
Expand Down
8 changes: 7 additions & 1 deletion Tests/CommonsTests/AnyCodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 7ccf26c

Please sign in to comment.