From 4f9d06b48335500127a8cdd4c910aaceabded01d Mon Sep 17 00:00:00 2001 From: Hayden Date: Tue, 5 Nov 2024 22:12:44 +0700 Subject: [PATCH] fix: integer query parameters value 1 and 0 being converted to boolean --- Sources/Typesense/utils/CreateURLQueryParams.swift | 2 +- Tests/TypesenseTests/CreateURLQueryParamsTests.swift | 11 ++++++++++- Tests/TypesenseTests/DocumentTests.swift | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Sources/Typesense/utils/CreateURLQueryParams.swift b/Sources/Typesense/utils/CreateURLQueryParams.swift index 16e6701..b92c915 100644 --- a/Sources/Typesense/utils/CreateURLQueryParams.swift +++ b/Sources/Typesense/utils/CreateURLQueryParams.swift @@ -21,7 +21,7 @@ func createURLQuery(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) diff --git a/Tests/TypesenseTests/CreateURLQueryParamsTests.swift b/Tests/TypesenseTests/CreateURLQueryParamsTests.swift index acccf20..f924840 100644 --- a/Tests/TypesenseTests/CreateURLQueryParamsTests.swift +++ b/Tests/TypesenseTests/CreateURLQueryParamsTests.swift @@ -6,18 +6,24 @@ 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 } } @@ -25,13 +31,16 @@ final class CreateURLQueryParamsTests: XCTestCase { 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) diff --git a/Tests/TypesenseTests/DocumentTests.swift b/Tests/TypesenseTests/DocumentTests.swift index eeef4f2..7c4cf75 100644 --- a/Tests/TypesenseTests/DocumentTests.swift +++ b/Tests/TypesenseTests/DocumentTests.swift @@ -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()