Skip to content

Commit

Permalink
Fixes URLSessionImplementations template for swift generator (#20381)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas1893 authored Dec 30, 2024
1 parent 25283d4 commit 93158ea
Show file tree
Hide file tree
Showing 23 changed files with 299 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest
var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest
var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
var urlRequest = urlRequest

var requestBodyComponents = URLComponents()
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])

/// `httpBody` needs to be percent encoded
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
return URLQueryItem(
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
}
requestBodyComponents.queryItems = percentEncodedQueryItems

if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
}

/// We can't use `requestBodyComponents.percentEncodedQuery` since this does NOT percent encode the `+` sign
/// that is why we do the percent encoding manually for each key/value pair
urlRequest.httpBody = requestBodyComponents.query?.data(using: .utf8)

return urlRequest
Expand Down
Loading

0 comments on commit 93158ea

Please sign in to comment.