From 60e31da07e8328358468ff4b4fcd24e944ebdcc1 Mon Sep 17 00:00:00 2001 From: costapombo <> Date: Wed, 14 Aug 2024 15:48:56 +0100 Subject: [PATCH 1/2] Pass HTTP response object even if the request fails Some services send HTTP headers with content that helps investigating errors. With the current implementation it is not possible to read those headers. --- .../swift5/AlamofireImplementations.mustache | 210 ++++++++---------- 1 file changed, 91 insertions(+), 119 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/swift5/AlamofireImplementations.mustache b/modules/swagger-codegen/src/main/resources/swift5/AlamofireImplementations.mustache index f072650e79d..45be53bcd59 100644 --- a/modules/swagger-codegen/src/main/resources/swift5/AlamofireImplementations.mustache +++ b/modules/swagger-codegen/src/main/resources/swift5/AlamofireImplementations.mustache @@ -118,32 +118,27 @@ open class AlamofireRequestBuilder: RequestBuilder { let cleanupRequest = { syncQueue.async(flags: .barrier) { - _ = managerStore.removeValue(forKey: managerId) + _ = managerStore.removeValue(forKey: managerId) } } let validatedRequest = request.validate() + var responseObj: Response? + var error: Error? switch T.self { case is String.Type: validatedRequest.responseString(completionHandler: { (stringResponse) in cleanupRequest() - if stringResponse.result.isFailure { - completion( - nil, - ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error!) - ) - return - } - - completion( - Response( - response: stringResponse.response!, - body: ((stringResponse.result.value ?? "") as! T) - ), - nil - ) + if stringResponse.result.isFailure { + error = ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error!) + } else { + responseObj = Response( + response: stringResponse.response!, + body: ((stringResponse.result.value ?? "") as! T) + ) + } }) case is URL.Type: validatedRequest.responseData(completionHandler: { (dataResponse) in @@ -180,18 +175,15 @@ open class AlamofireRequestBuilder: RequestBuilder { try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil) try data.write(to: filePath, options: .atomic) - completion( - Response( - response: dataResponse.response!, - body: (filePath as! T) - ), - nil - ) + responseObj = Response( + response: dataResponse.response!, + body: (filePath as! T) + ) } catch let requestParserError as DownloadException { - completion(nil, ErrorResponse.error(400, dataResponse.data, requestParserError)) - } catch let error { - completion(nil, ErrorResponse.error(400, dataResponse.data, error)) + error = ErrorResponse.error(400, dataResponse.data, requestParserError) + } catch let _error { + error = ErrorResponse.error(400, dataResponse.data, _error) } return }) @@ -200,41 +192,34 @@ open class AlamofireRequestBuilder: RequestBuilder { cleanupRequest() if voidResponse.result.isFailure { - completion( - nil, - ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!) - ) - return - } - - completion( - Response( - response: voidResponse.response!, - body: nil), - nil - ) + error = ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!) + } else { + responseObj = Response( + response: voidResponse.response!, + body: nil) + } }) default: validatedRequest.responseData(completionHandler: { (dataResponse) in cleanupRequest() - if dataResponse.result.isFailure { - completion( - nil, - ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!) - ) - return - } - - completion( - Response( - response: dataResponse.response!, - body: (dataResponse.data as! T) - ), - nil - ) + if dataResponse.result.isFailure { + error = ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!) + } else { + responseObj = Response( + response: dataResponse.response!, + body: (dataResponse.data as! T) + ) + } }) } + + + if responseObj == nil, let httpResponse = validatedRequest.response { + responseObj = Response(response: httpResponse, body: nil) + } + + completion(responseObj, error) } open func buildHeaders() -> [String: String] { @@ -327,6 +312,8 @@ open class AlamofireDecodableRequestBuilder: AlamofireRequestBuilde } let validatedRequest = request.validate() + var responseObj: Response? + var error: Error? switch T.self { case is String.Type: @@ -334,89 +321,74 @@ open class AlamofireDecodableRequestBuilder: AlamofireRequestBuilde cleanupRequest() if stringResponse.result.isFailure { - completion( - nil, - ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error!) - ) - return - } - - completion( - Response( - response: stringResponse.response!, - body: ((stringResponse.result.value ?? "") as! T) - ), - nil - ) + error = ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error!) + } else { + responseObj = Response( + response: stringResponse.response!, + body: ((stringResponse.result.value ?? "") as! T) + ) + } }) case is Void.Type: validatedRequest.responseData(completionHandler: { (voidResponse) in cleanupRequest() - if voidResponse.result.isFailure { - completion( - nil, - ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!) - ) - return - } + if voidResponse.result.isFailure { + error = ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!) + } else { + responseObj = Response( + response: voidResponse.response!, + body: nil + ) + } - completion( - Response( - response: voidResponse.response!, - body: nil), - nil - ) }) case is Data.Type: validatedRequest.responseData(completionHandler: { (dataResponse) in cleanupRequest() if dataResponse.result.isFailure { - completion( - nil, - ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!) - ) - return - } - - completion( - Response( - response: dataResponse.response!, - body: (dataResponse.data as! T) - ), - nil - ) + error = ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!) + } else { + responseObj = Response( + response: dataResponse.response!, + body: (dataResponse.data as! T) + ) + } }) default: validatedRequest.responseData(completionHandler: { (dataResponse: DataResponse) in cleanupRequest() - - guard dataResponse.result.isSuccess else { - completion(nil, ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!)) - return - } - - guard let data = dataResponse.data, !data.isEmpty else { - completion(nil, ErrorResponse.error(-1, nil, AlamofireDecodableRequestBuilderError.emptyDataResponse)) - return - } - - guard let httpResponse = dataResponse.response else { - completion(nil, ErrorResponse.error(-2, nil, AlamofireDecodableRequestBuilderError.nilHTTPResponse)) - return - } - - var responseObj: Response? = nil - - let decodeResult: (decodableObj: T?, error: Error?) = CodableHelper.decode(T.self, from: data) - if decodeResult.error == nil { - responseObj = Response(response: httpResponse, body: decodeResult.decodableObj) - } - - completion(responseObj, decodeResult.error) + do { + guard dataResponse.result.isSuccess else { + throw ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!) + } + + guard let data = dataResponse.data, !data.isEmpty else { + throw ErrorResponse.error(-1, nil, AlamofireDecodableRequestBuilderError.emptyDataResponse) + } + + guard let httpResponse = dataResponse.response else { + throw ErrorResponse.error(-2, nil, AlamofireDecodableRequestBuilderError.nilHTTPResponse) + } + + let decodeResult: (decodableObj: T?, error: Error?) = CodableHelper.decode(T.self, from: data) + + if let _error = decodeResult.error { + throw _error + } + responseObj = Response(response: httpResponse, body: decodeResult.decodableObj) + } catch let _error { + error = _error + } }) } + + if responseObj == nil, let httpResponse = validatedRequest.response { + responseObj = Response(response: httpResponse, body: nil) + } + + completion(responseObj, error) } } From 792b3e488dffb15ba519ce50743a68df7a1f5442 Mon Sep 17 00:00:00 2001 From: costapombo <> Date: Wed, 14 Aug 2024 15:52:17 +0100 Subject: [PATCH 2/2] Fix indentation --- .../swift5/AlamofireImplementations.mustache | 162 +++++++++--------- 1 file changed, 81 insertions(+), 81 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/swift5/AlamofireImplementations.mustache b/modules/swagger-codegen/src/main/resources/swift5/AlamofireImplementations.mustache index 45be53bcd59..5dd91f7596d 100644 --- a/modules/swagger-codegen/src/main/resources/swift5/AlamofireImplementations.mustache +++ b/modules/swagger-codegen/src/main/resources/swift5/AlamofireImplementations.mustache @@ -118,27 +118,27 @@ open class AlamofireRequestBuilder: RequestBuilder { let cleanupRequest = { syncQueue.async(flags: .barrier) { - _ = managerStore.removeValue(forKey: managerId) + _ = managerStore.removeValue(forKey: managerId) } } let validatedRequest = request.validate() - var responseObj: Response? - var error: Error? + var responseObj: Response? + var error: Error? switch T.self { case is String.Type: validatedRequest.responseString(completionHandler: { (stringResponse) in cleanupRequest() - if stringResponse.result.isFailure { - error = ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error!) - } else { - responseObj = Response( - response: stringResponse.response!, - body: ((stringResponse.result.value ?? "") as! T) - ) - } + if stringResponse.result.isFailure { + error = ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error!) + } else { + responseObj = Response( + response: stringResponse.response!, + body: ((stringResponse.result.value ?? "") as! T) + ) + } }) case is URL.Type: validatedRequest.responseData(completionHandler: { (dataResponse) in @@ -175,10 +175,10 @@ open class AlamofireRequestBuilder: RequestBuilder { try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil) try data.write(to: filePath, options: .atomic) - responseObj = Response( - response: dataResponse.response!, - body: (filePath as! T) - ) + responseObj = Response( + response: dataResponse.response!, + body: (filePath as! T) + ) } catch let requestParserError as DownloadException { error = ErrorResponse.error(400, dataResponse.data, requestParserError) @@ -192,34 +192,34 @@ open class AlamofireRequestBuilder: RequestBuilder { cleanupRequest() if voidResponse.result.isFailure { - error = ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!) - } else { - responseObj = Response( - response: voidResponse.response!, - body: nil) - } + error = ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!) + } else { + responseObj = Response( + response: voidResponse.response!, + body: nil) + } }) default: validatedRequest.responseData(completionHandler: { (dataResponse) in cleanupRequest() - if dataResponse.result.isFailure { - error = ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!) - } else { - responseObj = Response( - response: dataResponse.response!, - body: (dataResponse.data as! T) - ) - } + if dataResponse.result.isFailure { + error = ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!) + } else { + responseObj = Response( + response: dataResponse.response!, + body: (dataResponse.data as! T) + ) + } }) } - if responseObj == nil, let httpResponse = validatedRequest.response { - responseObj = Response(response: httpResponse, body: nil) - } + if responseObj == nil, let httpResponse = validatedRequest.response { + responseObj = Response(response: httpResponse, body: nil) + } - completion(responseObj, error) + completion(responseObj, error) } open func buildHeaders() -> [String: String] { @@ -312,8 +312,8 @@ open class AlamofireDecodableRequestBuilder: AlamofireRequestBuilde } let validatedRequest = request.validate() - var responseObj: Response? - var error: Error? + var responseObj: Response? + var error: Error? switch T.self { case is String.Type: @@ -322,25 +322,25 @@ open class AlamofireDecodableRequestBuilder: AlamofireRequestBuilde if stringResponse.result.isFailure { error = ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error!) - } else { - responseObj = Response( - response: stringResponse.response!, - body: ((stringResponse.result.value ?? "") as! T) - ) - } + } else { + responseObj = Response( + response: stringResponse.response!, + body: ((stringResponse.result.value ?? "") as! T) + ) + } }) case is Void.Type: validatedRequest.responseData(completionHandler: { (voidResponse) in cleanupRequest() - if voidResponse.result.isFailure { - error = ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!) - } else { - responseObj = Response( - response: voidResponse.response!, - body: nil - ) - } + if voidResponse.result.isFailure { + error = ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!) + } else { + responseObj = Response( + response: voidResponse.response!, + body: nil + ) + } }) case is Data.Type: @@ -349,46 +349,46 @@ open class AlamofireDecodableRequestBuilder: AlamofireRequestBuilde if dataResponse.result.isFailure { error = ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!) - } else { - responseObj = Response( - response: dataResponse.response!, - body: (dataResponse.data as! T) - ) - } + } else { + responseObj = Response( + response: dataResponse.response!, + body: (dataResponse.data as! T) + ) + } }) default: validatedRequest.responseData(completionHandler: { (dataResponse: DataResponse) in cleanupRequest() - do { - guard dataResponse.result.isSuccess else { - throw ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!) - } - - guard let data = dataResponse.data, !data.isEmpty else { - throw ErrorResponse.error(-1, nil, AlamofireDecodableRequestBuilderError.emptyDataResponse) - } - - guard let httpResponse = dataResponse.response else { - throw ErrorResponse.error(-2, nil, AlamofireDecodableRequestBuilderError.nilHTTPResponse) - } - - let decodeResult: (decodableObj: T?, error: Error?) = CodableHelper.decode(T.self, from: data) - - if let _error = decodeResult.error { - throw _error - } - responseObj = Response(response: httpResponse, body: decodeResult.decodableObj) - } catch let _error { - error = _error - } + do { + guard dataResponse.result.isSuccess else { + throw ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!) + } + + guard let data = dataResponse.data, !data.isEmpty else { + throw ErrorResponse.error(-1, nil, AlamofireDecodableRequestBuilderError.emptyDataResponse) + } + + guard let httpResponse = dataResponse.response else { + throw ErrorResponse.error(-2, nil, AlamofireDecodableRequestBuilderError.nilHTTPResponse) + } + + let decodeResult: (decodableObj: T?, error: Error?) = CodableHelper.decode(T.self, from: data) + + if let _error = decodeResult.error { + throw _error + } + responseObj = Response(response: httpResponse, body: decodeResult.decodableObj) + } catch let _error { + error = _error + } }) } - if responseObj == nil, let httpResponse = validatedRequest.response { - responseObj = Response(response: httpResponse, body: nil) - } + if responseObj == nil, let httpResponse = validatedRequest.response { + responseObj = Response(response: httpResponse, body: nil) + } - completion(responseObj, error) + completion(responseObj, error) } }