Skip to content

Commit 0428cee

Browse files
authored
Stop accepting null as a basic type for userdefaults (#188)
1 parent 0a55564 commit 0428cee

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

Sources/Segment/Utilities/Storage.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ internal class Storage: Subscriber {
117117
result = true
118118
} else {
119119
switch value {
120-
case is NSNull:
121-
fallthrough
120+
// NSNull is not valid for UserDefaults
121+
//case is NSNull:
122+
// fallthrough
122123
case is Decimal:
123124
fallthrough
124125
case is NSNumber:

Tests/Segment-Tests/Storage_Tests.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,56 @@ class StorageTests: XCTestCase {
1717
override func tearDownWithError() throws {
1818
// Put teardown code here. This method is called after the invocation of each test method in the class.
1919
}
20+
21+
func testSettingsWrite() throws {
22+
let dummySettings = """
23+
{
24+
"integrations": {
25+
"Segment.io": {
26+
"apiKey": "1234",
27+
"unbundledIntegrations": [],
28+
"addBundledMetadata": true
29+
}
30+
},
31+
"middlewareSettings": {
32+
"routingRules": [
33+
{
34+
"transformers": [
35+
[
36+
{
37+
"type": "allow_properties",
38+
"config": {
39+
"allow": {
40+
"traits": null,
41+
"context": null,
42+
"_metadata": null,
43+
"integrations": null,
44+
}
45+
}
46+
}
47+
]
48+
]
49+
}
50+
]
51+
},
52+
}
53+
"""
54+
let jsonData = dummySettings.data(using: .utf8)!
55+
let jsonSettings = try! JSONSerialization.jsonObject(with: jsonData)
56+
57+
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
58+
analytics.storage.hardReset(doYouKnowHowToUseThis: true)
59+
60+
// this will crash if it fails.
61+
let j = try! JSON(jsonSettings)
62+
analytics.storage.write(.settings, value: j)
63+
64+
RunLoop.main.run(until: Date.distantPast)
65+
66+
let result: JSON? = analytics.storage.read(.settings)
67+
68+
XCTAssertNotNil(result)
69+
}
2070

2171
func testBasicWriting() throws {
2272
let analytics = Analytics(configuration: Configuration(writeKey: "test"))

0 commit comments

Comments
 (0)