From aabce0b0c80121062d6a634330b0f5f2f9ec1ca5 Mon Sep 17 00:00:00 2001 From: Joannis Orlandos Date: Tue, 6 Sep 2022 10:28:55 +0200 Subject: [PATCH] Update field encoding so that it uses the generic path --- Sources/Meow/KeyPathModel/KeyPathModels.swift | 3 ++- Tests/MeowTests/MeowTests.swift | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Sources/Meow/KeyPathModel/KeyPathModels.swift b/Sources/Meow/KeyPathModel/KeyPathModels.swift index 76d81e35..5b0c662f 100644 --- a/Sources/Meow/KeyPathModel/KeyPathModels.swift +++ b/Sources/Meow/KeyPathModel/KeyPathModels.swift @@ -281,7 +281,8 @@ public struct Field: Codable { } public func encode(to encoder: Encoder) throws { - try _wrappedValue!.encode(to: encoder) + var container = encoder.singleValueContainer() + try container.encode(_wrappedValue!) } } diff --git a/Tests/MeowTests/MeowTests.swift b/Tests/MeowTests/MeowTests.swift index 5355a983..c8c86dc3 100644 --- a/Tests/MeowTests/MeowTests.swift +++ b/Tests/MeowTests/MeowTests.swift @@ -38,6 +38,17 @@ class MeowTests: XCTestCase { self.meow = MeowDatabase(mongo) } + func testEncodeDate() throws { + struct Entity: Model { + @Field var _id: ObjectId + @Field var date: Date + } + + let entity = Entity(_id: ObjectId(), date: Date()) + let document = try BSONEncoder().encode(entity) + XCTAssertTrue(document["date"] is Date) + } + func testAggregate() async throws { _ = try await meow[User.self].buildCheckedAggregate { Match { user in