Skip to content

Commit

Permalink
Fix swift5 binary upload (#17422)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stormshield-robinc authored Dec 21, 2023
1 parent 3099a2f commit 0a71402
Show file tree
Hide file tree
Showing 73 changed files with 274 additions and 165 deletions.
12 changes: 9 additions & 3 deletions modules/openapi-generator/src/main/resources/swift5/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,12 @@ extension {{projectName}}API {
localVariablePath = localVariablePath.replacingOccurrences(of: "{{=<% %>=}}{<%baseName%>}<%={{ }}=%>", with: {{paramName}}PostEscape, options: .literal, range: nil){{/pathParams}}
let localVariableURLString = {{projectName}}API.basePath + localVariablePath
{{#bodyParam}}
{{#isBinary}}
let localVariableParameters = ["body": {{paramName}}]
{{/isBinary}}
{{^isBinary}}
let localVariableParameters = JSONEncodingHelper.encodingParameters(forEncodableObject: {{paramName}})
{{/isBinary}}
{{/bodyParam}}
{{^bodyParam}}
{{#hasFormParams}}
Expand All @@ -431,9 +436,10 @@ extension {{projectName}}API {
]){{/hasQueryParams}}{{^hasQueryParams}}
let localVariableUrlComponents = URLComponents(string: localVariableURLString){{/hasQueryParams}}

let localVariableNillableHeaders: [String: Any?] = [{{^headerParams}}{{^hasFormParams}}
:{{/hasFormParams}}{{/headerParams}}{{#hasFormParams}}
"Content-Type": {{^consumes}}"multipart/form-data"{{/consumes}}{{#consumes.0}}"{{{mediaType}}}"{{/consumes.0}},{{/hasFormParams}}{{#headerParams}}
let localVariableNillableHeaders: [String: Any?] = [{{^headerParams}}{{^hasFormParams}}{{^hasConsumes}}
:{{/hasConsumes}}{{/hasFormParams}}{{/headerParams}}{{#hasFormParams}}
"Content-Type": {{^consumes}}"multipart/form-data"{{/consumes}}{{#consumes.0}}"{{{mediaType}}}"{{/consumes.0}},{{/hasFormParams}}{{^hasFormParams}}{{#hasConsumes}}
"Content-Type": {{#consumes.0}}"{{{mediaType}}}"{{/consumes.0}},{{/hasConsumes}}{{/hasFormParams}}{{#headerParams}}
{{> _param}},{{/headerParams}}
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,14 +592,20 @@ private class OctetStreamEncoding: ParameterEncoding {
var urlRequest = urlRequest
var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
guard let body = parameters?["body"] else { return urlRequest }

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
}

urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)
switch body {
case let fileURL as URL:
urlRequest.httpBody = try Data(contentsOf: fileURL)
case let data as Data:
urlRequest.httpBody = data
default:
fatalError("Unprocessable body \(body)")
}

return urlRequest
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ open class AnotherFakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ open class FakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -258,7 +258,7 @@ open class FakeAPI {
])

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -302,7 +302,7 @@ open class FakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -636,7 +636,7 @@ open class FakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ open class FakeClassnameTags123API {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ open class PetAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -309,7 +309,7 @@ open class PetAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,14 +592,20 @@ private class OctetStreamEncoding: ParameterEncoding {

var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
guard let body = parameters?["body"] else { return urlRequest }

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
}

urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)
switch body {
case let fileURL as URL:
urlRequest.httpBody = try Data(contentsOf: fileURL)
case let data as Data:
urlRequest.httpBody = data
default:
fatalError("Unprocessable body \(body)")
}

return urlRequest
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ open class AnotherFakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ open class FakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -210,7 +210,7 @@ open class FakeAPI {
])

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -246,7 +246,7 @@ open class FakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -548,7 +548,7 @@ open class FakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ open class FakeClassnameTags123API {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ open class PetAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -261,7 +261,7 @@ open class PetAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,14 +592,20 @@ private class OctetStreamEncoding: ParameterEncoding {

var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
guard let body = parameters?["body"] else { return urlRequest }

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
}

urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)
switch body {
case let fileURL as URL:
urlRequest.httpBody = try Data(contentsOf: fileURL)
case let data as Data:
urlRequest.httpBody = data
default:
fatalError("Unprocessable body \(body)")
}

return urlRequest
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ open class AnotherFakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ open class FakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -315,7 +315,7 @@ open class FakeAPI {
])

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -368,7 +368,7 @@ open class FakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -738,7 +738,7 @@ open class FakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ open class FakeClassnameTags123API {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ open class PetAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -366,7 +366,7 @@ open class PetAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,14 +592,20 @@ private class OctetStreamEncoding: ParameterEncoding {

var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
guard let body = parameters?["body"] else { return urlRequest }

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
}

urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)
switch body {
case let fileURL as URL:
urlRequest.httpBody = try Data(contentsOf: fileURL)
case let data as Data:
urlRequest.httpBody = data
default:
fatalError("Unprocessable body \(body)")
}

return urlRequest
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ open class AnotherFakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
"Content-Type": "application/json",
"uuid_test": uuidTest.encodeToJSON(),
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ open class FakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/xml",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -256,7 +256,7 @@ open class FakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -302,7 +302,7 @@ open class FakeAPI {
])

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -346,7 +346,7 @@ open class FakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down Expand Up @@ -680,7 +680,7 @@ open class FakeAPI {
let localVariableUrlComponents = URLComponents(string: localVariableURLString)

let localVariableNillableHeaders: [String: Any?] = [
:
"Content-Type": "application/json",
]

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)
Expand Down
Loading

0 comments on commit 0a71402

Please sign in to comment.