Skip to content

Commit 0902da8

Browse files
authored
feat: Add config setting for not sending additional properties for Li… (#103)
* feat: Add config setting for not sending additional properties for LiveQuery connections * more tests
1 parent 4238f91 commit 0902da8

File tree

9 files changed

+100
-14
lines changed

9 files changed

+100
-14
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22
# Parse-Swift Changelog
33

44
### main
5-
[Full Changelog](https://github.com/netreconlab/Parse-Swift/compare/5.5.1...main), [Documentation](https://swiftpackageindex.com/netreconlab/Parse-Swift/main/documentation/parseswift)
5+
[Full Changelog](https://github.com/netreconlab/Parse-Swift/compare/5.6.0...main), [Documentation](https://swiftpackageindex.com/netreconlab/Parse-Swift/main/documentation/parseswift)
66
* _Contributing to this repo? Add info about your change here to be included in the next release_
77

8+
### 5.6.0
9+
[Full Changelog](https://github.com/netreconlab/Parse-Swift/compare/5.5.1...5.6.0), [Documentation](https://swiftpackageindex.com/netreconlab/Parse-Swift/5.6.0/documentation/parseswift)
10+
11+
__New features__
12+
* Adds liveQueryConnectionAdditionalProperties parameter to SDK configuration to prevent additional properties from being sent to LiveQuery servers. This is useful for Parse Servers < 4.0.0 ([#103](https://github.com/netreconlab/Parse-Swift/pull/103)), thanks to [Corey Baker](https://github.com/cbaker6).
13+
814
__Fixes__
915
* Modernize Xcode project by using only one framework target. Also removes Carthage testing from CI ([#101](https://github.com/netreconlab/Parse-Swift/pull/101)), thanks to [Corey Baker](https://github.com/cbaker6).
1016

ParseSwift.playground/Pages/11 - LiveQuery.xcplaygroundpage/Contents.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ class LiveQueryDelegate: ParseLiveQueryDelegate {
2424
Task {
2525
do {
2626
try await initializeParse()
27-
//: Set the delegate.
28-
let delegate = LiveQueryDelegate()
29-
if let client = ParseLiveQuery.defaultClient {
30-
client.receiveDelegate = delegate
31-
} else {
32-
assertionFailure("LiveQuery should have a default client")
33-
}
3427
} catch {
3528
assertionFailure("Error initializing Parse-Swift: \(error)")
3629
}
@@ -121,6 +114,14 @@ Task {
121114
}
122115
}
123116

117+
//: This is how you set a delegate for the default client.
118+
let delegate = LiveQueryDelegate()
119+
if let client = ParseLiveQuery.defaultClient {
120+
client.receiveDelegate = delegate
121+
} else {
122+
assertionFailure("LiveQuery should have a default client")
123+
}
124+
124125
//: Ping the LiveQuery server
125126
ParseLiveQuery.client?.sendPing { error in
126127
if let error = error {
@@ -234,9 +235,13 @@ Task {
234235
print("Unsubscribed from \(query)")
235236
}
236237

238+
//: Delay for 2 seconds
239+
let nanoSeconds = UInt64(2 * 1_000_000_000)
240+
try await Task.sleep(nanoseconds: nanoSeconds)
241+
237242
//: To unsubscribe from your query.
238243
do {
239-
try await query2.unsubscribe()
244+
try await query.unsubscribe()
240245
} catch {
241246
print(error)
242247
}

Sources/ParseSwift/Documentation.docc/ParseSwift.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This repo is maintained by [Corey E. Baker](https://github.com/cbaker6), [1 of 2
1919
### Configure SDK
2020

2121
- ``ParseSwift/initialize(configuration:)``
22-
- ``ParseSwift/initialize(applicationId:clientKey:primaryKey:serverURL:liveQueryServerURL:requiringCustomObjectIds:usingTransactions:usingEqualQueryConstraint:usingPostForQuery:primitiveStore:requestCachePolicy:cacheMemoryCapacity:cacheDiskCapacity:usingDataProtectionKeychain:deletingKeychainIfNeeded:httpAdditionalHeaders:usingAutomaticLogin:maxConnectionAttempts:liveQueryMaxConnectionAttempts:parseFileTransfer:authentication:)``
22+
- ``ParseSwift/initialize(applicationId:clientKey:primaryKey:serverURL:liveQueryServerURL:requiringCustomObjectIds:usingTransactions:usingEqualQueryConstraint:usingPostForQuery:primitiveStore:requestCachePolicy:cacheMemoryCapacity:cacheDiskCapacity:usingDataProtectionKeychain:deletingKeychainIfNeeded:httpAdditionalHeaders:usingAutomaticLogin:maxConnectionAttempts:liveQueryConnectionAdditionalProperties:liveQueryMaxConnectionAttempts:parseFileTransfer:authentication:)``
2323
- ``ParseSwift/configuration``
2424
- ``ParseSwift/setAccessGroup(_:synchronizeAcrossDevices:)``
2525
- ``ParseSwift/updateAuthentication(_:)``

Sources/ParseSwift/LiveQuery/LiveQuerySocket.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ extension LiveQuerySocket {
5252
// MARK: Connect
5353
extension LiveQuerySocket {
5454
func connect(_ task: URLSessionWebSocketTask) async throws {
55+
try await yieldIfNotInitialized()
5556
let encoded = try ParseCoding.jsonEncoder()
5657
.encode(await StandardMessage(operation: .connect,
57-
additionalProperties: true))
58+
// swiftlint:disable:next line_length
59+
additionalProperties: Parse.configuration.liveQueryConnectionAdditionalProperties))
5860
guard let encodedAsString = String(data: encoded, encoding: .utf8) else {
5961
throw ParseError(code: .otherCause,
6062
message: "Could not encode connect message: \(encoded)")

Sources/ParseSwift/LiveQuery/Messages.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,23 @@ struct StandardMessage: LiveQueryable, Codable {
2020

2121
init(operation: ClientOperation, additionalProperties: Bool = false) async {
2222
self.op = operation
23-
if additionalProperties {
23+
switch operation {
24+
case .connect:
2425
self.applicationId = Parse.configuration.applicationId
2526
self.primaryKey = Parse.configuration.primaryKey
2627
self.clientKey = Parse.configuration.clientKey
2728
self.sessionToken = await BaseParseUser.currentContainer()?.sessionToken
28-
self.installationId = await BaseParseInstallation.currentContainer().installationId
29+
if additionalProperties {
30+
self.installationId = await BaseParseInstallation.currentContainer().installationId
31+
}
32+
default:
33+
if additionalProperties {
34+
self.applicationId = Parse.configuration.applicationId
35+
self.primaryKey = Parse.configuration.primaryKey
36+
self.clientKey = Parse.configuration.clientKey
37+
self.sessionToken = await BaseParseUser.currentContainer()?.sessionToken
38+
self.installationId = await BaseParseInstallation.currentContainer().installationId
39+
}
2940
}
3041
}
3142

Sources/ParseSwift/Parse.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ internal func initialize(applicationId: String,
3131
httpAdditionalHeaders: [AnyHashable: Any]? = nil,
3232
usingAutomaticLogin: Bool = false,
3333
maxConnectionAttempts: Int = 5,
34+
liveQueryConnectionAdditionalProperties: Bool = true,
3435
liveQueryMaxConnectionAttempts: Int = 20,
3536
testing: Bool = false,
3637
testLiveQueryDontCloseSocket: Bool = false,
@@ -55,6 +56,7 @@ internal func initialize(applicationId: String,
5556
httpAdditionalHeaders: httpAdditionalHeaders,
5657
usingAutomaticLogin: usingAutomaticLogin,
5758
maxConnectionAttempts: maxConnectionAttempts,
59+
liveQueryConnectionAdditionalProperties: liveQueryConnectionAdditionalProperties,
5860
liveQueryMaxConnectionAttempts: liveQueryMaxConnectionAttempts,
5961
authentication: authentication)
6062
configuration.isMigratingFromObjcSDK = migratingFromObjcSDK
@@ -242,6 +244,7 @@ public func initialize(configuration: ParseConfiguration) async throws { // swif
242244
the server once.
243245
- parameter maxConnectionAttempts: Maximum number of times to try to connect to Parse Server.
244246
Defaults to 5.
247+
- parameter liveQueryConnectionAdditionalProperties: Send additional information when establishing Parse LiveQuery connections.
245248
- parameter liveQueryMaxConnectionAttempts: Maximum number of times to try to connect to a Parse
246249
LiveQuery Server. Defaults to 20.
247250
- parameter parseFileTransfer: Override the default transfer behavior for `ParseFile`'s.
@@ -277,6 +280,7 @@ public func initialize(
277280
httpAdditionalHeaders: [AnyHashable: Any]? = nil,
278281
usingAutomaticLogin: Bool = false,
279282
maxConnectionAttempts: Int = 5,
283+
liveQueryConnectionAdditionalProperties: Bool = true,
280284
liveQueryMaxConnectionAttempts: Int = 20,
281285
parseFileTransfer: ParseFileTransferable? = nil,
282286
authentication: ((URLAuthenticationChallenge,
@@ -301,6 +305,7 @@ public func initialize(
301305
httpAdditionalHeaders: httpAdditionalHeaders,
302306
usingAutomaticLogin: usingAutomaticLogin,
303307
maxConnectionAttempts: maxConnectionAttempts,
308+
liveQueryConnectionAdditionalProperties: liveQueryConnectionAdditionalProperties,
304309
liveQueryMaxConnectionAttempts: liveQueryMaxConnectionAttempts,
305310
parseFileTransfer: parseFileTransfer,
306311
authentication: authentication)

Sources/ParseSwift/ParseConstants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010

1111
enum ParseConstants {
1212
static let sdk = "swift"
13-
static let version = "5.5.1"
13+
static let version = "5.6.0"
1414
static let fileManagementDirectory = "parse/"
1515
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
1616
static let fileManagementLibraryDirectory = "Library/"

Sources/ParseSwift/Types/ParseConfiguration.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public struct ParseConfiguration {
101101
/// Defaults to 5.
102102
public internal(set) var maxConnectionAttempts: Int = 5
103103

104+
/// Send additional information when establishing Parse LiveQuery connections.
105+
/// Defaults to **true**.
106+
public internal(set) var liveQueryConnectionAdditionalProperties: Bool = true
107+
104108
/// Maximum number of times to try to connect to a Parse LiveQuery Server.
105109
/// Defaults to 20.
106110
public internal(set) var liveQueryMaxConnectionAttempts: Int = 20
@@ -158,6 +162,7 @@ public struct ParseConfiguration {
158162
the server once.
159163
- parameter maxConnectionAttempts: Maximum number of times to try to connect to a Parse Server.
160164
Defaults to 5.
165+
- parameter liveQueryConnectionAdditionalProperties: Send additional information when establishing Parse LiveQuery connections.
161166
- parameter liveQueryMaxConnectionAttempts: Maximum number of times to try to connect to a Parse
162167
LiveQuery Server. Defaults to 20.
163168
- parameter parseFileTransfer: Override the default transfer behavior for `ParseFile`'s.
@@ -192,6 +197,7 @@ public struct ParseConfiguration {
192197
httpAdditionalHeaders: [AnyHashable: Any]? = nil,
193198
usingAutomaticLogin: Bool = false,
194199
maxConnectionAttempts: Int = 5,
200+
liveQueryConnectionAdditionalProperties: Bool = true,
195201
liveQueryMaxConnectionAttempts: Int = 20,
196202
parseFileTransfer: ParseFileTransferable? = nil,
197203
authentication: ((URLAuthenticationChallenge,
@@ -218,6 +224,7 @@ public struct ParseConfiguration {
218224
self.httpAdditionalHeaders = httpAdditionalHeaders
219225
self.isUsingAutomaticLogin = usingAutomaticLogin
220226
self.maxConnectionAttempts = maxConnectionAttempts
227+
self.liveQueryConnectionAdditionalProperties = liveQueryConnectionAdditionalProperties
221228
self.liveQueryMaxConnectionAttempts = liveQueryMaxConnectionAttempts
222229
self.parseFileTransfer = parseFileTransfer ?? ParseFileDefaultTransfer()
223230
self.primitiveStore = primitiveStore ?? InMemoryPrimitiveStore()

Tests/ParseSwiftTests/ParseLiveQueryTests.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,23 @@ class ParseLiveQueryTests: XCTestCase {
172172
XCTAssertNil(URLSession.liveQuery.authenticationDelegate)
173173
}
174174

175+
func testInitLiveQueryConnectionWithNoAdditional() async throws {
176+
XCTAssertTrue(Parse.configuration.liveQueryConnectionAdditionalProperties)
177+
guard let url = URL(string: "http://localhost:1337/parse") else {
178+
XCTFail("Should create valid URL")
179+
return
180+
}
181+
try await ParseSwift.initialize(applicationId: "applicationId",
182+
clientKey: "clientKey",
183+
primaryKey: "primaryKey",
184+
serverURL: url,
185+
liveQueryConnectionAdditionalProperties: false,
186+
liveQueryMaxConnectionAttempts: 1,
187+
testing: true,
188+
testLiveQueryDontCloseSocket: true)
189+
XCTAssertFalse(Parse.configuration.liveQueryConnectionAdditionalProperties)
190+
}
191+
175192
func testStandardMessageEncoding() async throws {
176193
guard let installationId = await BaseParseInstallation.currentContainer().installationId else {
177194
XCTFail("Should have installationId")
@@ -186,6 +203,39 @@ class ParseLiveQueryTests: XCTestCase {
186203
XCTAssertEqual(decoded, expected)
187204
}
188205

206+
func testStandardMessageNoAdditionalPropertiesEncoding() async throws {
207+
// swiftlint:disable:next line_length
208+
let expected = "{\"applicationId\":\"applicationId\",\"clientKey\":\"clientKey\",\"masterKey\":\"primaryKey\",\"op\":\"connect\"}"
209+
let message = await StandardMessage(operation: .connect)
210+
let encoded = try ParseCoding.jsonEncoder()
211+
.encode(message)
212+
let decoded = try XCTUnwrap(String(data: encoded, encoding: .utf8))
213+
XCTAssertEqual(decoded, expected)
214+
}
215+
216+
func testStandardMessageNotConnectionEncoding() async throws {
217+
guard let installationId = await BaseParseInstallation.currentContainer().installationId else {
218+
XCTFail("Should have installationId")
219+
return
220+
}
221+
// swiftlint:disable:next line_length
222+
let expected = "{\"applicationId\":\"applicationId\",\"clientKey\":\"clientKey\",\"installationId\":\"\(installationId)\",\"masterKey\":\"primaryKey\",\"op\":\"subscribe\"}"
223+
let message = await StandardMessage(operation: .subscribe, additionalProperties: true)
224+
let encoded = try ParseCoding.jsonEncoder()
225+
.encode(message)
226+
let decoded = try XCTUnwrap(String(data: encoded, encoding: .utf8))
227+
XCTAssertEqual(decoded, expected)
228+
}
229+
230+
func testStandardMessageNotConnectionNoAddEncoding() async throws {
231+
let expected = "{\"op\":\"subscribe\"}"
232+
let message = await StandardMessage(operation: .subscribe, additionalProperties: false)
233+
let encoded = try ParseCoding.jsonEncoder()
234+
.encode(message)
235+
let decoded = try XCTUnwrap(String(data: encoded, encoding: .utf8))
236+
XCTAssertEqual(decoded, expected)
237+
}
238+
189239
func testSubscribeMessageFieldsEncoding() async throws {
190240
// swiftlint:disable:next line_length
191241
let expected = "{\"op\":\"subscribe\",\"query\":{\"className\":\"GameScore\",\"fields\":[\"hello\",\"points\"],\"where\":{\"points\":{\"$gt\":9}}},\"requestId\":1}"

0 commit comments

Comments
 (0)