Skip to content

Commit e6d4223

Browse files
committed
EncodableRecord takes precedence over Encodable when a record is encoded with a SingleValueEncodingContainer.
1 parent ba3c938 commit e6d4223

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

GRDB/Record/EncodableRecord+Encodable.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,11 @@ extension RecordEncoder: SingleValueEncodingContainer {
224224
}
225225

226226
func encode<T>(_ value: T) throws where T : Encodable {
227-
try value.encode(to: self)
227+
if let record = value as? EncodableRecord {
228+
try record.encode(to: &_persistenceContainer)
229+
} else {
230+
try value.encode(to: self)
231+
}
228232
}
229233
}
230234

Tests/GRDBTests/MutablePersistableRecordEncodableTests.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,56 @@ extension MutablePersistableRecordEncodableTests {
124124
XCTAssertEqual(row[1], "bar")
125125
}
126126
}
127+
128+
// Regression test for <https://github.com/groue/GRDB.swift/issues/1565>
129+
// Here we test that `EncodableRecord` takes precedence over `Encodable`
130+
// when a record is encoded with a `SingleValueEncodingContainer`.
131+
func testSingleValueContainerWithEncodableRecord() throws {
132+
struct Struct: Encodable, EncodableRecord {
133+
let value: String
134+
135+
func encode(to container: inout PersistenceContainer) throws {
136+
container["column1"] = "test"
137+
container["column2"] = 12
138+
}
139+
}
140+
141+
struct Wrapper<Model: Encodable>: MutablePersistableRecord, Encodable {
142+
static var databaseTableName: String { "t1" }
143+
var model: Model
144+
var otherValue: String
145+
146+
enum CodingKeys: String, CodingKey {
147+
case otherValue
148+
}
149+
150+
func encode(to encoder: any Encoder) throws {
151+
var modelContainer = encoder.singleValueContainer()
152+
try modelContainer.encode(model)
153+
154+
var container = encoder.container(keyedBy: CodingKeys.self)
155+
try container.encode(otherValue, forKey: .otherValue)
156+
}
157+
}
158+
159+
let dbQueue = try makeDatabaseQueue()
160+
try dbQueue.inDatabase { db in
161+
try db.create(table: "t1") { t in
162+
t.column("column1", .text)
163+
t.column("column2", .integer)
164+
t.column("otherValue", .text)
165+
}
166+
167+
var value = Wrapper(model: Struct(value: "foo"), otherValue: "bar")
168+
try assert(value, isEncodedIn: ["column1": "test", "column2": 12, "otherValue": "bar"])
169+
170+
try value.insert(db)
171+
let row = try Row.fetchOne(db, sql: "SELECT column1, column2, otherValue FROM t1")!
172+
XCTAssertEqual(row[0], "test")
173+
XCTAssertEqual(row[1], 12)
174+
XCTAssertEqual(row[2], "bar")
175+
}
176+
}
127177
}
128178

129179
// MARK: - Different kinds of single-value properties

0 commit comments

Comments
 (0)