From 54a1cc9c61bc18bdbdcfa7b5f18f154c095f1b47 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 28 Nov 2024 12:48:48 +0000 Subject: [PATCH] Throw invalidConversion if FluentPersistDriver.get(key:as:) fails to convert object (#34) * Throw `invalidConversion` if JSONDecoder throws a DecodingError * Use async methods from fluent-kit Also updated dependency versions --- Package.swift | 6 ++--- Sources/HummingbirdFluent/Fluent.swift | 2 +- .../HummingbirdFluent/Persist+fluent.swift | 6 ++++- .../HummingbirdFluentTests/PersistTests.swift | 25 +++++++++++++++++++ 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/Package.swift b/Package.swift index b310a0f..8c31a37 100644 --- a/Package.swift +++ b/Package.swift @@ -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( diff --git a/Sources/HummingbirdFluent/Fluent.swift b/Sources/HummingbirdFluent/Fluent.swift index 79a341e..f6a76b1 100644 --- a/Sources/HummingbirdFluent/Fluent.swift +++ b/Sources/HummingbirdFluent/Fluent.swift @@ -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 diff --git a/Sources/HummingbirdFluent/Persist+fluent.swift b/Sources/HummingbirdFluent/Persist+fluent.swift index e43caf7..5f6f5c1 100644 --- a/Sources/HummingbirdFluent/Persist+fluent.swift +++ b/Sources/HummingbirdFluent/Persist+fluent.swift @@ -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 + } } } diff --git a/Tests/HummingbirdFluentTests/PersistTests.swift b/Tests/HummingbirdFluentTests/PersistTests.swift index 8b45d3f..d2fd428 100644 --- a/Tests/HummingbirdFluentTests/PersistTests.swift +++ b/Tests/HummingbirdFluentTests/PersistTests.swift @@ -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