Skip to content

Commit 1d2d7b7

Browse files
authored
fix: Use ParseEncoder when encoding data to ParseServer (#71)
* fix: Use ParseEncoder when encoding data to ParseServer * Update ParseSwift * lint
1 parent 63d0735 commit 1d2d7b7

File tree

5 files changed

+58
-5
lines changed

5 files changed

+58
-5
lines changed

Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"kind" : "remoteSourceControl",
4242
"location" : "https://github.com/netreconlab/Parse-Swift.git",
4343
"state" : {
44-
"revision" : "10ec67c29050cc7e9d69bc59dda787ec5a145e75",
45-
"version" : "5.10.0"
44+
"revision" : "ef83e349e3c4d055f8fc96f9fa2e3ffdcddee616",
45+
"version" : "5.10.1"
4646
}
4747
},
4848
{

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let package = Package(
2525
),
2626
.package(
2727
url: "https://github.com/netreconlab/Parse-Swift.git",
28-
.upToNextMajor(from: "5.10.0")
28+
.upToNextMajor(from: "5.10.1")
2929
)
3030
],
3131
targets: [

Sources/ParseServerSwift/Extensions/Parse+Vapor.swift

+53
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import ParseSwift
1111
extension ParseHookFunctionRequest: Content {}
1212
extension ParseHookTriggerRequest: Content {}
1313
extension ParseHookResponse: Content {}
14+
1415
public extension ParseHookRequestable {
1516
/**
1617
Produce the set of options that should be used for subsequent `ParseHook` requests.
@@ -53,3 +54,55 @@ public extension ParseHookRequestable {
5354
return try await self.hydrateUser(options: updatedOptions)
5455
}
5556
}
57+
58+
extension ParseEncoder: ContentEncoder {
59+
60+
public func encode<E>(
61+
_ encodable: E,
62+
to body: inout ByteBuffer,
63+
headers: inout HTTPHeaders
64+
) throws where E: Encodable {
65+
try self.encode(
66+
encodable,
67+
to: &body,
68+
headers: &headers,
69+
userInfo: [:]
70+
)
71+
}
72+
73+
public func encode<E>(
74+
_ encodable: E,
75+
to body: inout ByteBuffer,
76+
headers: inout HTTPHeaders,
77+
userInfo: [CodingUserInfoKey: Sendable]
78+
) throws where E: Encodable {
79+
headers.contentType = .json
80+
let jsonEncoder = User.getJSONEncoder()
81+
82+
if !userInfo.isEmpty {
83+
try body.writeBytes(JSONEncoder.custom(
84+
dates: jsonEncoder.dateEncodingStrategy,
85+
data: jsonEncoder.dataEncodingStrategy,
86+
keys: jsonEncoder.keyEncodingStrategy,
87+
format: jsonEncoder.outputFormatting,
88+
floats: jsonEncoder.nonConformingFloatEncodingStrategy,
89+
userInfo: jsonEncoder.userInfo.merging(userInfo) { $1 }
90+
).encode(encodable))
91+
} else {
92+
if let parseEncodable = encodable as? ParseCloudTypeable {
93+
try body.writeBytes(self.encode(parseEncodable, skipKeys: .cloud))
94+
} else if let parseEncodable = encodable as? ParseEncodable {
95+
let skipKeys: SkipKeys
96+
if !ParseSwift.configuration.isRequiringCustomObjectIds {
97+
skipKeys = .object
98+
} else {
99+
skipKeys = .customObjectId
100+
}
101+
try body.writeBytes(self.encode(parseEncodable, skipKeys: skipKeys))
102+
} else {
103+
try body.writeBytes(jsonEncoder.encode(encodable))
104+
}
105+
106+
}
107+
}
108+
}

Sources/ParseServerSwift/Parse.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func initializeServer(_ configuration: ParseServerConfiguration,
4747
app: Application) async throws {
4848

4949
// Parse uses tailored encoders/decoders. These can be retrieved from any ParseObject
50-
ContentConfiguration.global.use(encoder: User.getJSONEncoder(), for: .json)
50+
ContentConfiguration.global.use(encoder: User.getEncoder(), for: .json)
5151
ContentConfiguration.global.use(decoder: User.getDecoder(), for: .json)
5252

5353
guard let parseServerURL = URL(string: configuration.primaryParseServerURLString) else {

Sources/ParseServerSwift/ParseServerConfiguration.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public struct ParseServerConfiguration {
3535

3636
var hooks = Hooks()
3737
var isTesting = false
38-
var logger = Logger(label: "com.parseserverswift")
38+
var logger = Logger(label: "com.parse-server-swift")
3939

4040
/**
4141
Create a configuration for `ParseServerSwift`.

0 commit comments

Comments
 (0)