Skip to content

Commit cf8e4ee

Browse files
authored
Merge pull request #20 from vapor/fix/issue-19
Fix crasher in PostreSQLSerializer when ID is not provided
2 parents 6c5be2f + 5e95dc6 commit cf8e4ee

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

Sources/FluentPostgreSQL/PostgreSQLSerializer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public final class PostgreSQLSerializer: GeneralSQLSerializer {
104104
// Differs from Fluent implementation
105105
// Removes idKey value
106106
// The idKey needs to be blank when inserting a record
107-
if (dict["id"]?.isNull)! {
107+
if dict["id"]?.isNull ?? false {
108108
dict.removeValue(forKey: "id")
109109
}
110110

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import XCTest
2+
import Node
3+
@testable import FluentPostgreSQL
4+
5+
class SerializerTests: XCTestCase {
6+
static let allTests = [
7+
("testSerializerInsert", testSerializerInsert),
8+
("testSerializerInsertRemovesNullID", testSerializerInsertRemovesNullID),
9+
("testSerializerInsertWorksWithoutID", testSerializerInsertWorksWithoutID),
10+
]
11+
12+
func testSerializerInsert() throws {
13+
let input = Node.object([
14+
"id": Node.string("foo"),
15+
"name": Node.string("bar")
16+
])
17+
18+
let serializer = PostgreSQLSerializer(sql: .insert(table: "test", data: input))
19+
let result = serializer.serialize()
20+
21+
if "INSERT INTO test (id, name) VALUES ($1, $2)" == result.0 {
22+
XCTAssertEqual(2, result.1.count)
23+
XCTAssertEqual(Node.string("foo"), result.1.first ?? Node.null)
24+
XCTAssertEqual(Node.string("bar"), result.1.last ?? Node.null)
25+
} else if "INSERT INTO test (name, id) VALUES ($1, $2)" == result.0 {
26+
XCTAssertEqual(2, result.1.count)
27+
XCTAssertEqual(Node.string("bar"), result.1.first ?? Node.null)
28+
XCTAssertEqual(Node.string("foo"), result.1.last ?? Node.null)
29+
} else {
30+
XCTFail("serialize() returned an unexpected SQL statement: \(result.0)")
31+
}
32+
}
33+
34+
func testSerializerInsertRemovesNullID() throws {
35+
let input = Node.object([
36+
"id": Node.null,
37+
"name": Node.string("bar")
38+
])
39+
40+
let serializer = PostgreSQLSerializer(sql: .insert(table: "test", data: input))
41+
let result = serializer.serialize()
42+
43+
XCTAssertEqual("INSERT INTO test (name) VALUES ($1)", result.0)
44+
XCTAssertEqual(1, result.1.count)
45+
XCTAssertEqual(Node.string("bar"), result.1.first ?? Node.null)
46+
}
47+
48+
func testSerializerInsertWorksWithoutID() throws {
49+
let input = Node.object([
50+
"name": Node.string("bar")
51+
])
52+
53+
let serializer = PostgreSQLSerializer(sql: .insert(table: "test", data: input))
54+
let result = serializer.serialize()
55+
56+
XCTAssertEqual("INSERT INTO test (name) VALUES ($1)", result.0)
57+
XCTAssertEqual(1, result.1.count)
58+
XCTAssertEqual(Node.string("bar"), result.1.first ?? Node.null)
59+
}
60+
}

Tests/LinuxMain.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ XCTMain([
77
testCase(DriverTests.allTests),
88
testCase(JoinTests.allTests),
99
testCase(SchemaTests.allTests),
10+
testCase(SerializerTests.allTests)
1011
])
1112

1213
#endif

0 commit comments

Comments
 (0)