Skip to content

Commit

Permalink
fix: Use ParseEncoder when encoding data to ParseServer (#71)
Browse files Browse the repository at this point in the history
* fix: Use ParseEncoder when encoding data to ParseServer

* Update ParseSwift

* lint
  • Loading branch information
cbaker6 committed Jul 12, 2024
1 parent 63d0735 commit 1d2d7b7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
53 changes: 53 additions & 0 deletions Sources/ParseServerSwift/Extensions/Parse+Vapor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ParseSwift
extension ParseHookFunctionRequest: Content {}
extension ParseHookTriggerRequest: Content {}
extension ParseHookResponse: Content {}

public extension ParseHookRequestable {
/**
Produce the set of options that should be used for subsequent `ParseHook` requests.
Expand Down Expand Up @@ -53,3 +54,55 @@ public extension ParseHookRequestable {
return try await self.hydrateUser(options: updatedOptions)
}
}

extension ParseEncoder: ContentEncoder {

public func encode<E>(
_ 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<E>(
_ 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 {
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 {
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))
}

}
}
}
2 changes: 1 addition & 1 deletion Sources/ParseServerSwift/Parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseServerSwift/ParseServerConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down

0 comments on commit 1d2d7b7

Please sign in to comment.