Skip to content

Commit bbac3af

Browse files
committed
Merge pull request #84 from ishkawa/explicit-null-value
Improve nullable keys handling, restore [String: AnyObject] for `parameters`
2 parents cc32258 + ac3fc03 commit bbac3af

5 files changed

+16
-20
lines changed

APIKit/RequestType.swift

+9-15
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ public protocol RequestType {
1616
var baseURL: NSURL { get }
1717
var method: HTTPMethod { get }
1818
var path: String { get }
19-
var parameters: [String: AnyObject?] { get }
19+
20+
/// A parameter dictionary for the request. You can pass `NSNull()` as a
21+
/// value for nullable keys, those should be existed in the encoded query or
22+
/// the request body.
23+
var parameters: [String: AnyObject] { get }
2024

2125
/// You can add any configurations here
2226
///
@@ -45,7 +49,7 @@ public protocol RequestType {
4549

4650
/// Default implementation of RequestType protocol
4751
public extension RequestType {
48-
public var parameters: [String: AnyObject?] {
52+
public var parameters: [String: AnyObject] {
4953
return [:]
5054
}
5155

@@ -78,16 +82,15 @@ public extension RequestType {
7882
}
7983

8084
let URLRequest = NSMutableURLRequest()
81-
let paramObject = parametersToAnyObject(parameters)
82-
85+
8386
switch method {
8487
case .GET, .HEAD, .DELETE:
8588
if parameters.count > 0 {
86-
components.percentEncodedQuery = URLEncodedSerialization.stringFromDictionary(paramObject)
89+
components.percentEncodedQuery = URLEncodedSerialization.stringFromDictionary(parameters)
8790
}
8891
default:
8992
do {
90-
URLRequest.HTTPBody = try requestBodyBuilder.buildBodyFromObject(paramObject)
93+
URLRequest.HTTPBody = try requestBodyBuilder.buildBodyFromObject(parameters)
9194
} catch {
9295
return .Failure(.RequestBodySerializationError(error))
9396
}
@@ -134,12 +137,3 @@ public extension RequestType {
134137
return .Success(response)
135138
}
136139
}
137-
138-
/// Convert `var parameters: [String: AnyObject?]` (non-AnyObject) to AnyObject using NSNull
139-
private func parametersToAnyObject(parameters: [String: AnyObject?]) -> [String: AnyObject] {
140-
var object = [String: AnyObject]()
141-
for (key, value) in parameters {
142-
object[key] = value ?? NSNull()
143-
}
144-
return object
145-
}

APIKit/URLEncodedSerialization.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public final class URLEncodedSerialization {
6262

6363
public static func stringFromDictionary(dictionary: [String: AnyObject]) -> String {
6464
let pairs = dictionary.map { key, value -> String in
65-
guard (value is NSNull) == false else {
65+
if value is NSNull {
6666
return "\(escape(key))"
6767
}
6868

APIKitTests/RequestCreateTaskInURLSessionTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class RequestCreateTaskInURLSessionTest: XCTestCase {
1818
var baseURL: NSURL { return NSURL(string: b)! }
1919
var method: HTTPMethod { return m }
2020
var path: String { return p }
21-
var parameters: [String: AnyObject?] { return params }
21+
var parameters: [String: AnyObject] { return params }
2222
func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) -> Response? { return nil }
2323
}
2424

APIKitTests/RequestTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class RequestTests: XCTestCase {
1717
return "/"
1818
}
1919

20-
var parameters: [String: AnyObject?] {
20+
var parameters: [String: AnyObject] {
2121
return [
2222
"q": query,
23-
"dummy": nil
23+
"dummy": NSNull()
2424
]
2525
}
2626

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ var path: String
154154
var parameters: [String: AnyObject]
155155
```
156156

157-
`parameters` will be converted into query parameter if `method` is one of `.GET`, `.HEAD` and `.DELETE`. Otherwise, it will be serialized by `requestBodyBuilder` and set to `HTTPBody` of `NSURLRequest`.
157+
`parameters` will be converted into query parameter if `method` is one of `.GET`, `.HEAD` and `.DELETE`.
158+
Otherwise, it will be serialized by `requestBodyBuilder` and set to `HTTPBody` of `NSURLRequest`.
159+
You can pass `NSNull()` as a value for nullable keys if you'd like to preserve the keys when its value is absent.
158160

159161
#### Configuring format of HTTP body
160162

0 commit comments

Comments
 (0)