@@ -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