Skip to content

Commit 9721627

Browse files
dmonaglevzsg
authored andcommitted
Fix numeric with leading zero fractional (vapor#68)
* Create failing test Numeric numbers with a fractional part with a leading zero (eg: 10.08) will be decoded from the database without the leading zero (ie: 10.80). This is a serious issue which results in corrupted numeric values. * Add padding of zeros to fractional portion of numericals
1 parent 4988607 commit 9721627

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

Sources/PostgreSQL/Data/PostgreSQLData+String.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ extension String: PostgreSQLDataConvertible {
5656
if offset < metadata.weight.bigEndian + 1 {
5757
integer += string
5858
} else {
59-
fractional += string
59+
// Leading zeros matter with fractional
60+
fractional += fractional.count == 0 ? String(repeating: "0", count: 4 - string.count) + string : string
6061
}
6162
}
6263

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class PostgreSQLConnectionTests: XCTestCase {
213213
params += Int16(1) // smallint
214214
params += Int32(2) // integer
215215
params += Int64(3) // bigint
216-
params += String("123456789.123456789") // decimal
216+
params += String("123456789.0123456789") // decimal
217217
params += Double(5) // numeric
218218
params += Float(6) // real
219219
params += Double(7) // double
@@ -238,7 +238,7 @@ class PostgreSQLConnectionTests: XCTestCase {
238238
try XCTAssertEqual(row.firstValue(forColumn: "smallint")?.decode(Int16.self), 1)
239239
try XCTAssertEqual(row.firstValue(forColumn: "integer")?.decode(Int32.self), 2)
240240
try XCTAssertEqual(row.firstValue(forColumn: "bigint")?.decode(Int64.self), 3)
241-
try XCTAssertEqual(row.firstValue(forColumn: "decimal")?.decode(String.self), "123456789.123456789")
241+
try XCTAssertEqual(row.firstValue(forColumn: "decimal")?.decode(String.self), "123456789.0123456789")
242242
try XCTAssertEqual(row.firstValue(forColumn: "real")?.decode(Float.self), 6)
243243
try XCTAssertEqual(row.firstValue(forColumn: "double")?.decode(Double.self), 7)
244244
try XCTAssertEqual(row.firstValue(forColumn: "varchar")?.decode(String.self), "8")

0 commit comments

Comments
 (0)