Skip to content

Commit

Permalink
fix: integer query parameters value 1 and 0 being converted to boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
phiHero committed Nov 5, 2024
1 parent 16ec542 commit 4f9d06b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/Typesense/utils/CreateURLQueryParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func createURLQuery<T: Codable>(forSchema: T?) throws -> [URLQueryItem]{
var stringVal = ""
if let str = val as? String {
stringVal = str
} else if let bool = val as? Bool {
} else if type(of: val) == type(of: NSNumber(booleanLiteral: true)) || type(of: val) == type(of: NSNumber(booleanLiteral: false)), let bool = val as? Bool{
stringVal = String(bool)
} else if let int = val as? Int {
stringVal = String(int)
Expand Down
11 changes: 10 additions & 1 deletion Tests/TypesenseTests/CreateURLQueryParamsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,41 @@ final class CreateURLQueryParamsTests: XCTestCase {
var name = "Fast"
var age: Int = 20
var price: Int64 = 2000
var page: Int? = 1
var groupLimit: Int64? = 0
var speed: Double = 120.555
var mileage: Float = 120.5
var isElectric = true
var inStock: Bool = false
var empty: String? = nil

enum CodingKeys: String, CodingKey {
case name
case age
case price
case page
case groupLimit = "group_limit"
case speed
case mileage
case isElectric = "is_electric"
case inStock = "in_stock"
case empty
}
}
func testCreateURLQueryParams() {
do {
let queryParams = try createURLQuery(forSchema: Car())
print(queryParams)
XCTAssertEqual(queryParams.count, 6)
XCTAssertEqual(queryParams.count, 9)
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "name", value: "Fast")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "age", value: "20")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "price", value: "2000")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "page", value: "1")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "group_limit", value: "0")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "speed", value: "120.555")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "mileage", value: "120.5")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "is_electric", value: "true")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "in_stock", value: "false")))
XCTAssertFalse(queryParams.contains(URLQueryItem(name: "empty", value: "null")))
} catch (let error) {
print(error)
Expand Down
2 changes: 1 addition & 1 deletion Tests/TypesenseTests/DocumentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ final class DocumentTests: XCTestCase {
}

func testDocumentSearch() async {
let searchParams = SearchParameters(q: "stark", queryBy: "company_name", filterBy: "num_employees:>100", sortBy: "num_employees:desc")
let searchParams = SearchParameters(q: "*", queryBy: "company_name", page: 0, groupBy: "country", groupLimit: 1)

do {
try await createDocument()
Expand Down

0 comments on commit 4f9d06b

Please sign in to comment.