Skip to content

Commit

Permalink
Merge pull request #83 from inamiy/nullable-parameter-value
Browse files Browse the repository at this point in the history
Fix parameter type from [String:AnyObject] to [String:AnyObject?] for nullable value.
  • Loading branch information
ishkawa committed Oct 15, 2015
2 parents ab49f07 + 2e141ba commit cc32258
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
20 changes: 15 additions & 5 deletions APIKit/RequestType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public protocol RequestType {
var baseURL: NSURL { get }
var method: HTTPMethod { get }
var path: String { get }
var parameters: [String: AnyObject] { get }
var parameters: [String: AnyObject?] { get }

/// You can add any configurations here
///
Expand Down Expand Up @@ -45,7 +45,7 @@ public protocol RequestType {

/// Default implementation of RequestType protocol
public extension RequestType {
public var parameters: [String: AnyObject] {
public var parameters: [String: AnyObject?] {
return [:]
}

Expand Down Expand Up @@ -78,15 +78,16 @@ public extension RequestType {
}

let URLRequest = NSMutableURLRequest()

let paramObject = parametersToAnyObject(parameters)

switch method {
case .GET, .HEAD, .DELETE:
if parameters.count > 0 {
components.percentEncodedQuery = URLEncodedSerialization.stringFromDictionary(parameters)
components.percentEncodedQuery = URLEncodedSerialization.stringFromDictionary(paramObject)
}
default:
do {
URLRequest.HTTPBody = try requestBodyBuilder.buildBodyFromObject(parameters)
URLRequest.HTTPBody = try requestBodyBuilder.buildBodyFromObject(paramObject)
} catch {
return .Failure(.RequestBodySerializationError(error))
}
Expand Down Expand Up @@ -133,3 +134,12 @@ public extension RequestType {
return .Success(response)
}
}

/// Convert `var parameters: [String: AnyObject?]` (non-AnyObject) to AnyObject using NSNull
private func parametersToAnyObject(parameters: [String: AnyObject?]) -> [String: AnyObject] {
var object = [String: AnyObject]()
for (key, value) in parameters {
object[key] = value ?? NSNull()
}
return object
}
4 changes: 4 additions & 0 deletions APIKit/URLEncodedSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public final class URLEncodedSerialization {

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

let valueAsString = (value as? String) ?? "\(value)"
return "\(escape(key))=\(escape(valueAsString))"
}
Expand Down
2 changes: 1 addition & 1 deletion APIKitTests/RequestCreateTaskInURLSessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RequestCreateTaskInURLSessionTest: XCTestCase {
var baseURL: NSURL { return NSURL(string: b)! }
var method: HTTPMethod { return m }
var path: String { return p }
var parameters: [String: AnyObject] { return params }
var parameters: [String: AnyObject?] { return params }
func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) -> Response? { return nil }
}

Expand Down
7 changes: 4 additions & 3 deletions APIKitTests/RequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ class RequestTests: XCTestCase {
return "/"
}

var parameters: [String: AnyObject] {
var parameters: [String: AnyObject?] {
return [
"q": query,
"dummy": nil
]
}

Expand All @@ -35,7 +36,7 @@ class RequestTests: XCTestCase {

func testJapanesesURLQueryParameterEncoding() {
OHHTTPStubs.stubRequestsPassingTest({ request in
XCTAssert(request.URL?.query == "q=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF")
XCTAssert(request.URL?.query == "q=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF&dummy")
return true
}, withStubResponse: { request in
return OHHTTPStubsResponse(data: NSData(), statusCode: 200, headers: nil)
Expand All @@ -53,7 +54,7 @@ class RequestTests: XCTestCase {

func testSymbolURLQueryParameterEncoding() {
OHHTTPStubs.stubRequestsPassingTest({ request in
XCTAssert(request.URL?.query == "q=%21%22%23%24%25%26%27%28%290%3D~%7C%60%7B%7D%2A%2B%3C%3E%3F_")
XCTAssert(request.URL?.query == "q=%21%22%23%24%25%26%27%28%290%3D~%7C%60%7B%7D%2A%2B%3C%3E%3F_&dummy")
return true
}, withStubResponse: { request in
return OHHTTPStubsResponse(data: NSData(), statusCode: 200, headers: nil)
Expand Down

0 comments on commit cc32258

Please sign in to comment.