Skip to content

Commit

Permalink
Fix an issue where empty Encodable Parameters resulted in a non-nil q…
Browse files Browse the repository at this point in the history
…uery string. (Alamofire#2818)
  • Loading branch information
DavidBarry authored and jshier committed May 6, 2019
1 parent 163db02 commit 1081318
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Source/ParameterEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ open class URLEncodedFormParameterEncoder: ParameterEncoder {
let query: String = try AFResult<String> { try encoder.encode(parameters) }
.mapError { AFError.parameterEncoderFailed(reason: .encoderFailed(error: $0)) }.get()
let newQueryString = [components.percentEncodedQuery, query].compactMap { $0 }.joinedWithAmpersands()
components.percentEncodedQuery = newQueryString
components.percentEncodedQuery = newQueryString.isEmpty ? nil : newQueryString

guard let newURL = components.url else {
throw AFError.parameterEncoderFailed(reason: .missingRequiredComponent(.url))
Expand Down
13 changes: 13 additions & 0 deletions Tests/ParameterEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ final class URLEncodedFormParameterEncoderTests: BaseTestCase {
let components = URLComponents(url: newRequest.url!, resolvingAgainstBaseURL: false)
XCTAssertEqual(components?.percentEncodedQuery, "property=property")
}

func testThatQueryIsNilWhenEncodableResultsInAnEmptyString() throws {
// Given
let encoder = URLEncodedFormParameterEncoder(destination: .queryString)
let request = URLRequest.makeHTTPBinRequest()

// When
let newRequest = try encoder.encode([String: String](), into: request)

// Then
let components = URLComponents(url: newRequest.url!, resolvingAgainstBaseURL: false)
XCTAssertNil(components?.percentEncodedQuery)
}
}

final class URLEncodedFormEncoderTests: BaseTestCase {
Expand Down

0 comments on commit 1081318

Please sign in to comment.