From 78f39e1ed6e3eb9ec1aa48614c273e2fde4fe2b8 Mon Sep 17 00:00:00 2001 From: Corey Baker Date: Fri, 12 Jul 2024 06:08:21 -0700 Subject: [PATCH 1/3] fix: Use ParseEncoder when encoding data to ParseServer --- .../Extensions/Parse+Vapor.swift | 35 +++++++++++++++++++ Sources/ParseServerSwift/Parse.swift | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Sources/ParseServerSwift/Extensions/Parse+Vapor.swift b/Sources/ParseServerSwift/Extensions/Parse+Vapor.swift index c23f8e74..25916a1b 100644 --- a/Sources/ParseServerSwift/Extensions/Parse+Vapor.swift +++ b/Sources/ParseServerSwift/Extensions/Parse+Vapor.swift @@ -11,6 +11,8 @@ import ParseSwift extension ParseHookFunctionRequest: Content {} extension ParseHookTriggerRequest: Content {} extension ParseHookResponse: Content {} +extension ParseEncoder: @unchecked Swift.Sendable {} + public extension ParseHookRequestable { /** Produce the set of options that should be used for subsequent `ParseHook` requests. @@ -53,3 +55,36 @@ public extension ParseHookRequestable { return try await self.hydrateUser(options: updatedOptions) } } + +extension ParseEncoder: ContentEncoder { + + public func encode(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders) throws + where E: Encodable + { + try self.encode(encodable, to: &body, headers: &headers, userInfo: [:]) + } + + public func encode(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders, userInfo: [CodingUserInfoKey: Sendable]) throws + where E: Encodable + { + headers.contentType = .json + let jsonEncoder = User.getJSONEncoder() + + if !userInfo.isEmpty { // Changing a coder's userInfo is a thread-unsafe mutation, operate on a copy + try body.writeBytes(JSONEncoder.custom( + dates: jsonEncoder.dateEncodingStrategy, + data: jsonEncoder.dataEncodingStrategy, + keys: jsonEncoder.keyEncodingStrategy, + format: jsonEncoder.outputFormatting, + floats: jsonEncoder.nonConformingFloatEncodingStrategy, + userInfo: jsonEncoder.userInfo.merging(userInfo) { $1 } + ).encode(encodable)) + } else { + guard let parseEncodable = encodable as? ParseEncodable else { + try body.writeBytes(jsonEncoder.encode(encodable)) + return + } + try body.writeBytes(self.encode(parseEncodable, skipKeys: .object)) + } + } +} diff --git a/Sources/ParseServerSwift/Parse.swift b/Sources/ParseServerSwift/Parse.swift index 4fa66f96..7ef219dc 100644 --- a/Sources/ParseServerSwift/Parse.swift +++ b/Sources/ParseServerSwift/Parse.swift @@ -47,7 +47,7 @@ func initializeServer(_ configuration: ParseServerConfiguration, app: Application) async throws { // Parse uses tailored encoders/decoders. These can be retrieved from any ParseObject - ContentConfiguration.global.use(encoder: User.getJSONEncoder(), for: .json) + ContentConfiguration.global.use(encoder: User.getEncoder(), for: .json) ContentConfiguration.global.use(decoder: User.getDecoder(), for: .json) guard let parseServerURL = URL(string: configuration.primaryParseServerURLString) else { From 22d646c46aa756f8ce5b6421861083677ec353b1 Mon Sep 17 00:00:00 2001 From: Corey Baker Date: Fri, 12 Jul 2024 06:34:56 -0700 Subject: [PATCH 2/3] Update ParseSwift --- Package.resolved | 4 ++-- Package.swift | 2 +- .../Extensions/Parse+Vapor.swift | 16 ++++++++++++---- .../ParseServerConfiguration.swift | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Package.resolved b/Package.resolved index ec02aa65..db862e1f 100644 --- a/Package.resolved +++ b/Package.resolved @@ -41,8 +41,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/netreconlab/Parse-Swift.git", "state" : { - "revision" : "10ec67c29050cc7e9d69bc59dda787ec5a145e75", - "version" : "5.10.0" + "revision" : "ef83e349e3c4d055f8fc96f9fa2e3ffdcddee616", + "version" : "5.10.1" } }, { diff --git a/Package.swift b/Package.swift index 158ae524..02ac7d0b 100644 --- a/Package.swift +++ b/Package.swift @@ -25,7 +25,7 @@ let package = Package( ), .package( url: "https://github.com/netreconlab/Parse-Swift.git", - .upToNextMajor(from: "5.10.0") + .upToNextMajor(from: "5.10.1") ) ], targets: [ diff --git a/Sources/ParseServerSwift/Extensions/Parse+Vapor.swift b/Sources/ParseServerSwift/Extensions/Parse+Vapor.swift index 25916a1b..f831750e 100644 --- a/Sources/ParseServerSwift/Extensions/Parse+Vapor.swift +++ b/Sources/ParseServerSwift/Extensions/Parse+Vapor.swift @@ -11,7 +11,6 @@ import ParseSwift extension ParseHookFunctionRequest: Content {} extension ParseHookTriggerRequest: Content {} extension ParseHookResponse: Content {} -extension ParseEncoder: @unchecked Swift.Sendable {} public extension ParseHookRequestable { /** @@ -80,11 +79,20 @@ extension ParseEncoder: ContentEncoder { userInfo: jsonEncoder.userInfo.merging(userInfo) { $1 } ).encode(encodable)) } else { - guard let parseEncodable = encodable as? ParseEncodable else { + if let parseEncodable = encodable as? ParseCloudTypeable { + try body.writeBytes(self.encode(parseEncodable, skipKeys: .cloud)) + } else if let parseEncodable = encodable as? ParseEncodable { + let skipKeys: SkipKeys + if !ParseSwift.configuration.isRequiringCustomObjectIds { + skipKeys = .object + } else { + skipKeys = .customObjectId + } + try body.writeBytes(self.encode(parseEncodable, skipKeys: skipKeys)) + } else { try body.writeBytes(jsonEncoder.encode(encodable)) - return } - try body.writeBytes(self.encode(parseEncodable, skipKeys: .object)) + } } } diff --git a/Sources/ParseServerSwift/ParseServerConfiguration.swift b/Sources/ParseServerSwift/ParseServerConfiguration.swift index de47ede5..9b898e9d 100644 --- a/Sources/ParseServerSwift/ParseServerConfiguration.swift +++ b/Sources/ParseServerSwift/ParseServerConfiguration.swift @@ -35,7 +35,7 @@ public struct ParseServerConfiguration { var hooks = Hooks() var isTesting = false - var logger = Logger(label: "com.parseserverswift") + var logger = Logger(label: "com.parse-server-swift") /** Create a configuration for `ParseServerSwift`. From 35a6187a061afb48f1a3d7d22a5c2ee423cc0c5e Mon Sep 17 00:00:00 2001 From: Corey Baker Date: Fri, 12 Jul 2024 06:43:53 -0700 Subject: [PATCH 3/3] lint --- .../Extensions/Parse+Vapor.swift | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/Sources/ParseServerSwift/Extensions/Parse+Vapor.swift b/Sources/ParseServerSwift/Extensions/Parse+Vapor.swift index f831750e..5536e15d 100644 --- a/Sources/ParseServerSwift/Extensions/Parse+Vapor.swift +++ b/Sources/ParseServerSwift/Extensions/Parse+Vapor.swift @@ -57,19 +57,29 @@ public extension ParseHookRequestable { extension ParseEncoder: ContentEncoder { - public func encode(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders) throws - where E: Encodable - { - try self.encode(encodable, to: &body, headers: &headers, userInfo: [:]) + public func encode( + _ encodable: E, + to body: inout ByteBuffer, + headers: inout HTTPHeaders + ) throws where E: Encodable { + try self.encode( + encodable, + to: &body, + headers: &headers, + userInfo: [:] + ) } - public func encode(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders, userInfo: [CodingUserInfoKey: Sendable]) throws - where E: Encodable - { + public func encode( + _ encodable: E, + to body: inout ByteBuffer, + headers: inout HTTPHeaders, + userInfo: [CodingUserInfoKey: Sendable] + ) throws where E: Encodable { headers.contentType = .json let jsonEncoder = User.getJSONEncoder() - if !userInfo.isEmpty { // Changing a coder's userInfo is a thread-unsafe mutation, operate on a copy + if !userInfo.isEmpty { try body.writeBytes(JSONEncoder.custom( dates: jsonEncoder.dateEncodingStrategy, data: jsonEncoder.dataEncodingStrategy,