Skip to content

Commit 1e78987

Browse files
committed
allow EmptyResponse to be used with CodableParser when API call response does not contain valid JSON.
1 parent 8d81a89 commit 1e78987

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.
33

44
# Next
55

6+
* Allow `EmptyResponse` to be used with CodableParser when network response does not contain valid JSON.
7+
68
## [4.1.0](https://github.com/MLSDev/TRON/releases/tag/4.1.0)
79

810
* Deprecate TRON level headerBuilder in favor of Alamofire.requestAdapter.

Diff for: Source/EmptyResponse.swift

+8-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ import Foundation
2727

2828
#if swift(>=4.0)
2929
/// Struct, that can be used as Model generic constraint in cases, where you don't care about response type.
30-
public struct EmptyResponse: Codable {}
30+
public struct EmptyResponse: Codable {
31+
/// Creates `EmptyResponse`.
32+
public init() {}
33+
}
3134
#else
3235
/// Struct, that can be used as Model generic constraint in cases, where you don't care about response type.
33-
public struct EmptyResponse {}
36+
public struct EmptyResponse {
37+
/// Creates `EmptyResponse`.
38+
public init() {}
39+
}
3440
#endif

Diff for: Source/TRONCodable.swift

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ open class CodableParser<Model: Decodable, ErrorModel: Decodable> : ErrorHandlin
4949
open var serializeResponse: (URLRequest?, HTTPURLResponse?, Data?, Error?) -> Result<Model> {
5050
return { [weak self] request, response, data, error in
5151
do {
52+
if Model.self is EmptyResponse.Type {
53+
// swiftlint:disable:next force_cast
54+
return Result.success(EmptyResponse() as! Model)
55+
}
5256
let model = try (self?.modelDecoder ?? JSONDecoder()).decode(Model.self, from: data ?? Data())
5357
return Result.success(model)
5458
} catch {
@@ -96,6 +100,10 @@ open class CodableDownloadParser<Model: Decodable, ErrorModel: Decodable> : Erro
96100
return { [weak self] request, response, url, error in
97101
if let url = url, let data = try? Data(contentsOf: url) {
98102
do {
103+
if Model.self is EmptyResponse.Type {
104+
// swiftlint:disable:next force_cast
105+
return Result.success(EmptyResponse() as! Model)
106+
}
99107
let model = try (self?.modelDecoder ?? JSONDecoder()).decode(Model.self, from: data)
100108
return Result.success(model)
101109
} catch {

Diff for: Tests/TRONTests/CodableTestCase.swift

+14
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ class CodableTestCase: XCTestCase {
6262
waitForExpectations(timeout: 5, handler: nil)
6363
}
6464

65+
func testEmptyResponseStillCallsSuccessBlock() {
66+
let tron = TRON(baseURL: "http://httpbin.org")
67+
let request : APIRequest<EmptyResponse, CodableError> = tron.codable.request("headers")
68+
request.method = .head
69+
let expectation = self.expectation(description: "Empty response")
70+
request.perform(withSuccess: { _ in
71+
expectation.fulfill()
72+
}, failure: { _ in
73+
XCTFail()
74+
}
75+
)
76+
waitForExpectations(timeout: 1, handler: nil)
77+
}
78+
6579
}
6680

6781
#endif

0 commit comments

Comments
 (0)