From ec975819b714311ca8a855f400ddfad505e94769 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 4 Mar 2022 11:55:53 +0100 Subject: [PATCH] Fix for container.decodeNil(forKey:) in the context of including(all:) --- GRDB/Record/FetchableRecord+Decodable.swift | 5 +- ...ciationPrefetchingCodableRecordTests.swift | 141 ++++++++++++++++++ 2 files changed, 144 insertions(+), 2 deletions(-) diff --git a/GRDB/Record/FetchableRecord+Decodable.swift b/GRDB/Record/FetchableRecord+Decodable.swift index 9d76d1d39..27a8e8d3d 100644 --- a/GRDB/Record/FetchableRecord+Decodable.swift +++ b/GRDB/Record/FetchableRecord+Decodable.swift @@ -117,8 +117,6 @@ private struct _RowDecoder: Decoder { } func decodeNil(forKey key: Key) throws -> Bool { - // Nil is only possible for columns and scopes (optional - // associations), not for prefetched rows. let row = decoder.row if let column = try? decodeColumn(forKey: key), row[column] != nil { return false @@ -126,6 +124,9 @@ private struct _RowDecoder: Decoder { if row.scopesTree[key.stringValue] != nil { return false } + if row.prefetchedRows[key.stringValue] != nil { + return false + } return true } diff --git a/Tests/GRDBTests/AssociationPrefetchingCodableRecordTests.swift b/Tests/GRDBTests/AssociationPrefetchingCodableRecordTests.swift index 8749ad797..7db9f641f 100644 --- a/Tests/GRDBTests/AssociationPrefetchingCodableRecordTests.swift +++ b/Tests/GRDBTests/AssociationPrefetchingCodableRecordTests.swift @@ -79,6 +79,91 @@ class AssociationPrefetchingCodableRecordTests: GRDBTestCase { } } + func testMissingIncludingAllHasMany() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + let request = A.orderByPrimaryKey() + + do { + struct Record: FetchableRecord, Decodable, Equatable { + var a: A + var bs: [B]? // Support for missing association + } + + // Record.fetchAll + do { + let records = try Record.fetchAll(db, request) + XCTAssertEqual(records, [ + Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: nil), + Record( + a: A(row: ["cola1": 2, "cola2": "a2"]), + bs: nil), + Record( + a: A(row: ["cola1": 3, "cola2": "a3"]), + bs: nil), + ]) + } + + // Record.fetchOne + do { + let record = try Record.fetchOne(db, request)! + XCTAssertEqual(record, Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: nil)) + } + } + + // Test container.decodeNil + do { + struct Record: FetchableRecord, Decodable, Equatable { + var a: A + var bs: [B]? // Support for missing association + + init(a: A, bs: [B]?) { + self.a = a + self.bs = bs + } + + enum CodingKeys: CodingKey { case a, bs } + init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + a = try container.decode(A.self, forKey: .a) + if try container.decodeNil(forKey: .bs) { + bs = nil + } else { + bs = [] + } + } + } + + // Record.fetchAll + do { + let records = try Record.fetchAll(db, request) + XCTAssertEqual(records, [ + Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: nil), + Record( + a: A(row: ["cola1": 2, "cola2": "a2"]), + bs: nil), + Record( + a: A(row: ["cola1": 3, "cola2": "a3"]), + bs: nil), + ]) + } + + // Record.fetchOne + do { + let record = try Record.fetchOne(db, request)! + XCTAssertEqual(record, Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: nil)) + } + } } + } + func testIncludingAllHasMany() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in @@ -209,6 +294,62 @@ class AssociationPrefetchingCodableRecordTests: GRDBTestCase { ])) } } + + // Test container.decodeNil + do { + struct Record: FetchableRecord, Decodable, Equatable { + var a: A + var bs: [B] + + init(a: A, bs: [B]) { + self.a = a + self.bs = bs + } + + enum CodingKeys: CodingKey { case a, bs } + init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + a = try container.decode(A.self, forKey: .a) + if try container.decodeNil(forKey: .bs) { + fatalError("Test failed") + } else { + bs = try container.decode([B].self, forKey: .bs) + } + } + } + + // Record.fetchAll + do { + let records = try Record.fetchAll(db, request) + XCTAssertEqual(records, [ + Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: [ + B(row: ["colb1": 4, "colb2": 1, "colb3": "b1"]), + B(row: ["colb1": 5, "colb2": 1, "colb3": "b2"]), + ]), + Record( + a: A(row: ["cola1": 2, "cola2": "a2"]), + bs: [ + B(row: ["colb1": 6, "colb2": 2, "colb3": "b3"]), + ]), + Record( + a: A(row: ["cola1": 3, "cola2": "a3"]), + bs: []), + ]) + } + + // Record.fetchOne + do { + let record = try Record.fetchOne(db, request)! + XCTAssertEqual(record, Record( + a: A(row: ["cola1": 1, "cola2": "a1"]), + bs: [ + B(row: ["colb1": 4, "colb2": 1, "colb3": "b1"]), + B(row: ["colb1": 5, "colb2": 1, "colb3": "b2"]), + ])) + } + } } // Request with filters