From 3b46c7aa803e0e0a394ebeb805e05127212aff84 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Wed, 20 Nov 2024 11:48:07 +0000 Subject: [PATCH] Persist invalid conversion (#36) * Throw PersistError.invalidConversion on invalid get(key:as:) * Use HB 2.5 --- Package.swift | 2 +- .../PostgresPersistDriver.swift | 22 ++++++++++------ .../PersistTests.swift | 25 +++++++++++++++++++ 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/Package.swift b/Package.swift index 6fc15de..fa80b09 100644 --- a/Package.swift +++ b/Package.swift @@ -13,7 +13,7 @@ let package = Package( .library(name: "PostgresMigrations", targets: ["PostgresMigrations"]), ], dependencies: [ - .package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"), + .package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.5.0"), .package(url: "https://github.com/vapor/postgres-nio", from: "1.21.0"), ], targets: [ diff --git a/Sources/HummingbirdPostgres/PostgresPersistDriver.swift b/Sources/HummingbirdPostgres/PostgresPersistDriver.swift index 27b0fa0..11a78a2 100644 --- a/Sources/HummingbirdPostgres/PostgresPersistDriver.swift +++ b/Sources/HummingbirdPostgres/PostgresPersistDriver.swift @@ -35,7 +35,11 @@ extension PSQLError { /// to build your table. /// ``` /// let migrations = DatabaseMigrations() -/// let persist = PostgresPersistDriver(client: postgresClient, migrations: migrations) +/// let persist = PostgresPersistDriver( +/// client: postgresClient, +/// migrations: migrations, +/// logger: logger +/// ) /// var app = Application(...) /// app.runBeforeServerStart { /// try await migrations.apply(client: postgresClient, logger: logger, dryRun: applyMigrations) @@ -127,13 +131,17 @@ public final class PostgresPersistDriver: PersistDriver { "SELECT data, expires FROM _hb_pg_persist WHERE id = \(key)", logger: self.logger ) - guard let (object, expires) = try await stream.decode((WrapperObject, Date).self) - .first(where: { _ in true }) - else { - return nil + do { + guard let (object, expires) = try await stream.decode((WrapperObject, Date).self) + .first(where: { _ in true }) + else { + return nil + } + guard expires > .now else { return nil } + return object.value + } catch is DecodingError { + throw PersistError.invalidConversion } - guard expires > .now else { return nil } - return object.value } /// Remove key diff --git a/Tests/HummingbirdPostgresTests/PersistTests.swift b/Tests/HummingbirdPostgresTests/PersistTests.swift index 24120ae..4a78635 100644 --- a/Tests/HummingbirdPostgresTests/PersistTests.swift +++ b/Tests/HummingbirdPostgresTests/PersistTests.swift @@ -211,6 +211,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(.router) { client in