Skip to content

Commit 3d5b140

Browse files
authored
Fix URLSessionImplementations file upload (#6043)
* Fix URLSessionImplementations file upload * Generated petstore
1 parent eccdf1d commit 3d5b140

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+415
-287
lines changed

modules/openapi-generator/src/main/resources/swift5/libraries/urlsession/URLSessionImplementations.mustache

+31-12
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,8 @@ fileprivate class FileUploadEncoding: ParameterEncoding {
488488
}
489489

490490
var body = urlRequest.httpBody.orEmpty
491-
492-
body.append("--\(boundary)--")
491+
492+
body.append("\r\n--\(boundary)--\r\n")
493493

494494
urlRequest.httpBody = body
495495

@@ -507,15 +507,24 @@ fileprivate class FileUploadEncoding: ParameterEncoding {
507507
let mimetype = self.contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
508508
509509
let fileName = fileURL.lastPathComponent
510-
510+
511+
// If we already added something then we need an additional newline.
512+
if (body.count > 0) {
513+
body.append("\r\n")
514+
}
515+
516+
// Value boundary.
511517
body.append("--\(boundary)\r\n")
512-
body.append("Content-Disposition: form-data; name=\"\(name)\"; filename=\"\(fileName)\"\r\n\r\n")
513518

514-
body.append("Content-Type: \(mimetype)\r\n\r\n")
519+
// Value headers.
520+
body.append("Content-Disposition: form-data; name=\"\(name)\"; filename=\"\(fileName)\"\r\n")
521+
body.append("Content-Type: \(mimetype)\r\n")
515522

516-
body.append(fileData)
523+
// Separate headers and body.
524+
body.append("\r\n")
517525

518-
body.append("\r\n\r\n")
526+
// The value data.
527+
body.append(fileData)
519528

520529
urlRequest.httpBody = body
521530

@@ -527,14 +536,24 @@ fileprivate class FileUploadEncoding: ParameterEncoding {
527536
var urlRequest = urlRequest
528537
529538
var body = urlRequest.httpBody.orEmpty
530-
539+
540+
// If we already added something then we need an additional newline.
541+
if (body.count > 0) {
542+
body.append("\r\n")
543+
}
544+
545+
// Value boundary.
531546
body.append("--\(boundary)\r\n")
532-
body.append("Content-Disposition: form-data; name=\"\(name)\"\r\n\r\n")
533547

548+
// Value headers.
549+
body.append("Content-Disposition: form-data; name=\"\(name)\"\r\n")
550+
551+
// Separate headers and body.
552+
body.append("\r\n")
553+
554+
// The value data.
534555
body.append(data)
535-
536-
body.append("\r\n\r\n")
537-
556+
538557
urlRequest.httpBody = body
539558

540559
return urlRequest

samples/client/petstore/swift5/default/Package.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let package = Package(
1414
// Products define the executables and libraries produced by a package, and make them visible to other packages.
1515
.library(
1616
name: "PetstoreClient",
17-
targets: ["PetstoreClient"])
17+
targets: ["PetstoreClient"]),
1818
],
1919
dependencies: [
2020
// Dependencies declare other packages that this package depends on.
@@ -26,6 +26,6 @@ let package = Package(
2626
name: "PetstoreClient",
2727
dependencies: [],
2828
path: "PetstoreClient/Classes"
29-
)
29+
),
3030
]
3131
)

samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/APIHelper.swift

+9-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import Foundation
88

99
public struct APIHelper {
10-
public static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
10+
public static func rejectNil(_ source: [String:Any?]) -> [String:Any]? {
1111
let destination = source.reduce(into: [String: Any]()) { (result, item) in
1212
if let value = item.value {
1313
result[item.key] = value
@@ -20,17 +20,17 @@ public struct APIHelper {
2020
return destination
2121
}
2222

23-
public static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
23+
public static func rejectNilHeaders(_ source: [String:Any?]) -> [String:String] {
2424
return source.reduce(into: [String: String]()) { (result, item) in
25-
if let collection = item.value as? [Any?] {
26-
result[item.key] = collection.filter({ $0 != nil }).map { "\($0!)" }.joined(separator: ",")
25+
if let collection = item.value as? Array<Any?> {
26+
result[item.key] = collection.filter({ $0 != nil }).map{ "\($0!)" }.joined(separator: ",")
2727
} else if let value: Any = item.value {
2828
result[item.key] = "\(value)"
2929
}
3030
}
3131
}
3232

33-
public static func convertBoolToString(_ source: [String: Any]?) -> [String: Any]? {
33+
public static func convertBoolToString(_ source: [String: Any]?) -> [String:Any]? {
3434
guard let source = source else {
3535
return nil
3636
}
@@ -46,15 +46,15 @@ public struct APIHelper {
4646
}
4747

4848
public static func mapValueToPathItem(_ source: Any) -> Any {
49-
if let collection = source as? [Any?] {
49+
if let collection = source as? Array<Any?> {
5050
return collection.filter({ $0 != nil }).map({"\($0!)"}).joined(separator: ",")
5151
}
5252
return source
5353
}
5454

55-
public static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
55+
public static func mapValuesToQueryItems(_ source: [String:Any?]) -> [URLQueryItem]? {
5656
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
57-
if let collection = item.value as? [Any?] {
57+
if let collection = item.value as? Array<Any?> {
5858
let value = collection.filter({ $0 != nil }).map({"\($0!)"}).joined(separator: ",")
5959
result.append(URLQueryItem(name: item.key, value: value))
6060
} else if let value = item.value {
@@ -68,3 +68,4 @@ public struct APIHelper {
6868
return destination
6969
}
7070
}
71+

samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/APIs.swift

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ import Foundation
99
open class PetstoreClientAPI {
1010
public static var basePath = "http://petstore.swagger.io:80/v2"
1111
public static var credential: URLCredential?
12-
public static var customHeaders: [String: String] = [:]
12+
public static var customHeaders: [String:String] = [:]
1313
public static var requestBuilderFactory: RequestBuilderFactory = URLSessionRequestBuilderFactory()
1414
public static var apiResponseQueue: DispatchQueue = .main
1515
}
1616

1717
open class RequestBuilder<T> {
1818
var credential: URLCredential?
19-
var headers: [String: String]
20-
public let parameters: [String: Any]?
19+
var headers: [String:String]
20+
public let parameters: [String:Any]?
2121
public let isBody: Bool
2222
public let method: String
2323
public let URLString: String
2424

2525
/// Optional block to obtain a reference to the request's progress instance when available.
2626
/// With the URLSession http client the request's progress only works on iOS 11.0, macOS 10.13, macCatalyst 13.0, tvOS 11.0, watchOS 4.0.
2727
/// If you need to get the request's progress in older OS versions, please use Alamofire http client.
28-
public var onProgressReady: ((Progress) -> Void)?
28+
public var onProgressReady: ((Progress) -> ())?
2929

30-
required public init(method: String, URLString: String, parameters: [String: Any]?, isBody: Bool, headers: [String: String] = [:]) {
30+
required public init(method: String, URLString: String, parameters: [String:Any]?, isBody: Bool, headers: [String:String] = [:]) {
3131
self.method = method
3232
self.URLString = URLString
3333
self.parameters = parameters
@@ -37,7 +37,7 @@ open class RequestBuilder<T> {
3737
addHeaders(PetstoreClientAPI.customHeaders)
3838
}
3939

40-
open func addHeaders(_ aHeaders: [String: String]) {
40+
open func addHeaders(_ aHeaders:[String:String]) {
4141
for (header, value) in aHeaders {
4242
headers[header] = value
4343
}
@@ -60,5 +60,5 @@ open class RequestBuilder<T> {
6060

6161
public protocol RequestBuilderFactory {
6262
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type
63-
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type
63+
func getBuilder<T:Decodable>() -> RequestBuilder<T>.Type
6464
}

samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/APIs/AnotherFakeAPI.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import Foundation
99

10+
11+
1012
open class AnotherFakeAPI {
1113
/**
1214
To test special tags
@@ -15,7 +17,7 @@ open class AnotherFakeAPI {
1517
- parameter apiResponseQueue: The queue on which api response is dispatched.
1618
- parameter completion: completion handler to receive the data and the error objects
1719
*/
18-
open class func call123testSpecialTags(body: Client, apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue, completion: @escaping ((_ data: Client?, _ error: Error?) -> Void)) {
20+
open class func call123testSpecialTags(body: Client, apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue, completion: @escaping ((_ data: Client?,_ error: Error?) -> Void)) {
1921
call123testSpecialTagsWithRequestBuilder(body: body).execute(apiResponseQueue) { result -> Void in
2022
switch result {
2123
case let .success(response):

0 commit comments

Comments
 (0)