Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use ParseEncoder when encoding data to ParseServer #71

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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))

Check warning on line 90 in Sources/ParseServerSwift/Extensions/Parse+Vapor.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ParseServerSwift/Extensions/Parse+Vapor.swift#L83-L90

Added lines #L83 - L90 were not covered by tests
} else {
if let parseEncodable = encodable as? ParseCloudTypeable {
try body.writeBytes(self.encode(parseEncodable, skipKeys: .cloud))

Check warning on line 93 in Sources/ParseServerSwift/Extensions/Parse+Vapor.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ParseServerSwift/Extensions/Parse+Vapor.swift#L93

Added line #L93 was not covered by tests
} else if let parseEncodable = encodable as? ParseEncodable {
let skipKeys: SkipKeys
if !ParseSwift.configuration.isRequiringCustomObjectIds {
skipKeys = .object
} else {
skipKeys = .customObjectId

Check warning on line 99 in Sources/ParseServerSwift/Extensions/Parse+Vapor.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ParseServerSwift/Extensions/Parse+Vapor.swift#L95-L99

Added lines #L95 - L99 were not covered by tests
}
try body.writeBytes(self.encode(parseEncodable, skipKeys: skipKeys))

Check warning on line 101 in Sources/ParseServerSwift/Extensions/Parse+Vapor.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ParseServerSwift/Extensions/Parse+Vapor.swift#L101

Added line #L101 was not covered by tests
} 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