Skip to content

Commit

Permalink
Throw invalidConversion if FluentPersistDriver.get(key:as:) fails to …
Browse files Browse the repository at this point in the history
…convert object (#34)

* Throw `invalidConversion` if JSONDecoder throws a DecodingError

* Use async methods from fluent-kit

Also updated dependency versions
  • Loading branch information
adam-fowler authored Nov 28, 2024
1 parent 373020e commit 54a1cc9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-async-algorithms.git", from: "1.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.48.5"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.5.0"),
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.49.0"),
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.0.0"),
// used in tests
.package(url: "https://github.com/vapor/fluent-sqlite-driver.git", from: "4.7.0"),
.package(url: "https://github.com/vapor/fluent-sqlite-driver.git", from: "4.8.0"),
],
targets: [
.target(
Expand Down
2 changes: 1 addition & 1 deletion Sources/HummingbirdFluent/Fluent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public struct Fluent: Sendable, Service {

/// Shutdown Fluent databases
public func shutdown() async throws {
self.databases.shutdown()
await self.databases.shutdownAsync()
}

/// Return Database connection
Expand Down
6 changes: 5 additions & 1 deletion Sources/HummingbirdFluent/Persist+fluent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public final class FluentPersistDriver: PersistDriver {
.filter(\.$expires > Date())
.first()
guard let data = query?.data else { return nil }
return try JSONDecoder().decode(object, from: data)
do {
return try JSONDecoder().decode(object, from: data)
} catch is DecodingError {
throw PersistError.invalidConversion
}
}
}

Expand Down
25 changes: 25 additions & 0 deletions Tests/HummingbirdFluentTests/PersistTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,31 @@ final class PersistTests: XCTestCase {
}
}

func testInvalidGetAs() async throws {
struct TestCodable: Codable {
let buffer: String
}
let app = try await self.createApplication { router, persist in
router.put("/invalid") { _, _ -> HTTPResponse.Status in
try await persist.set(key: "test", value: TestCodable(buffer: "hello"))
return .ok
}
router.get("/invalid") { _, _ -> String? in
do {
return try await persist.get(key: "test", as: String.self)
} catch let error as PersistError where error == .invalidConversion {
throw HTTPError(.badRequest)
}
}
}
try await app.test(.router) { client in
try await client.execute(uri: "/invalid", method: .put)
try await client.execute(uri: "/invalid", method: .get) { response in
XCTAssertEqual(response.status, .badRequest)
}
}
}

func testRemove() async throws {
let app = try await self.createApplication()
try await app.test(.live) { client in
Expand Down

0 comments on commit 54a1cc9

Please sign in to comment.