Skip to content

Commit 660d2f3

Browse files
Jonas1893timon-sbr
authored andcommitted
Fixes URLSessionImplementations template for swift generator (OpenAPITools#20381)
1 parent b9784a5 commit 660d2f3

File tree

23 files changed

+299
-23
lines changed

23 files changed

+299
-23
lines changed

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
611611
var urlRequest = urlRequest
612612
613613
var requestBodyComponents = URLComponents()
614-
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
614+
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
615+
616+
/// `httpBody` needs to be percent encoded
617+
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
618+
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
619+
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
620+
return URLQueryItem(
621+
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
622+
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
623+
}
624+
requestBodyComponents.queryItems = percentEncodedQueryItems
615625

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

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

622634
return urlRequest

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
636636
var urlRequest = urlRequest
637637
638638
var requestBodyComponents = URLComponents()
639-
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
639+
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
640+
641+
/// `httpBody` needs to be percent encoded
642+
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
643+
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
644+
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
645+
return URLQueryItem(
646+
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
647+
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
648+
}
649+
requestBodyComponents.queryItems = percentEncodedQueryItems
640650

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

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

647659
return urlRequest

samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
611611
var urlRequest = urlRequest
612612

613613
var requestBodyComponents = URLComponents()
614-
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
614+
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
615+
616+
/// `httpBody` needs to be percent encoded
617+
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
618+
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
619+
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
620+
return URLQueryItem(
621+
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
622+
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
623+
}
624+
requestBodyComponents.queryItems = percentEncodedQueryItems
615625

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

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

622634
return urlRequest

samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
611611
var urlRequest = urlRequest
612612

613613
var requestBodyComponents = URLComponents()
614-
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
614+
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
615+
616+
/// `httpBody` needs to be percent encoded
617+
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
618+
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
619+
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
620+
return URLQueryItem(
621+
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
622+
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
623+
}
624+
requestBodyComponents.queryItems = percentEncodedQueryItems
615625

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

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

622634
return urlRequest

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
611611
var urlRequest = urlRequest
612612

613613
var requestBodyComponents = URLComponents()
614-
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
614+
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
615+
616+
/// `httpBody` needs to be percent encoded
617+
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
618+
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
619+
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
620+
return URLQueryItem(
621+
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
622+
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
623+
}
624+
requestBodyComponents.queryItems = percentEncodedQueryItems
615625

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

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

622634
return urlRequest

samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
611611
var urlRequest = urlRequest
612612

613613
var requestBodyComponents = URLComponents()
614-
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
614+
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
615+
616+
/// `httpBody` needs to be percent encoded
617+
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
618+
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
619+
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
620+
return URLQueryItem(
621+
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
622+
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
623+
}
624+
requestBodyComponents.queryItems = percentEncodedQueryItems
615625

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

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

622634
return urlRequest

samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
611611
var urlRequest = urlRequest
612612

613613
var requestBodyComponents = URLComponents()
614-
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
614+
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
615+
616+
/// `httpBody` needs to be percent encoded
617+
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
618+
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
619+
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
620+
return URLQueryItem(
621+
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
622+
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
623+
}
624+
requestBodyComponents.queryItems = percentEncodedQueryItems
615625

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

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

622634
return urlRequest

samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
611611
var urlRequest = urlRequest
612612

613613
var requestBodyComponents = URLComponents()
614-
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
614+
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
615+
616+
/// `httpBody` needs to be percent encoded
617+
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
618+
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
619+
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
620+
return URLQueryItem(
621+
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
622+
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
623+
}
624+
requestBodyComponents.queryItems = percentEncodedQueryItems
615625

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

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

622634
return urlRequest

samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
611611
var urlRequest = urlRequest
612612

613613
var requestBodyComponents = URLComponents()
614-
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
614+
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
615+
616+
/// `httpBody` needs to be percent encoded
617+
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
618+
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
619+
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
620+
return URLQueryItem(
621+
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
622+
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
623+
}
624+
requestBodyComponents.queryItems = percentEncodedQueryItems
615625

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

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

622634
return urlRequest

samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
611611
var urlRequest = urlRequest
612612

613613
var requestBodyComponents = URLComponents()
614-
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
614+
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
615+
616+
/// `httpBody` needs to be percent encoded
617+
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
618+
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
619+
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
620+
return URLQueryItem(
621+
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
622+
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
623+
}
624+
requestBodyComponents.queryItems = percentEncodedQueryItems
615625

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

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

622634
return urlRequest

samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/URLSessionImplementations.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
611611
var urlRequest = urlRequest
612612

613613
var requestBodyComponents = URLComponents()
614-
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
614+
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
615+
616+
/// `httpBody` needs to be percent encoded
617+
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
618+
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
619+
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
620+
return URLQueryItem(
621+
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
622+
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
623+
}
624+
requestBodyComponents.queryItems = percentEncodedQueryItems
615625

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

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

622634
return urlRequest

samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -611,12 +611,24 @@ private class FormURLEncoding: ParameterEncoding {
611611
var urlRequest = urlRequest
612612

613613
var requestBodyComponents = URLComponents()
614-
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
614+
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
615+
616+
/// `httpBody` needs to be percent encoded
617+
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
618+
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
619+
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
620+
return URLQueryItem(
621+
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
622+
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
623+
}
624+
requestBodyComponents.queryItems = percentEncodedQueryItems
615625

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

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

622634
return urlRequest

samples/client/petstore/swift6/asyncAwaitLibrary/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -636,12 +636,24 @@ private class FormURLEncoding: ParameterEncoding {
636636
var urlRequest = urlRequest
637637

638638
var requestBodyComponents = URLComponents()
639-
requestBodyComponents.queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
639+
let queryItems = APIHelper.mapValuesToQueryItems(parameters ?? [:])
640+
641+
/// `httpBody` needs to be percent encoded
642+
/// -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
643+
/// "application/x-www-form-urlencoded: [...] Non-alphanumeric characters in both keys and values are percent-encoded"
644+
let percentEncodedQueryItems = queryItems?.compactMap { queryItem in
645+
return URLQueryItem(
646+
name: queryItem.name.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.name,
647+
value: queryItem.value?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? queryItem.value)
648+
}
649+
requestBodyComponents.queryItems = percentEncodedQueryItems
640650

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

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

647659
return urlRequest

0 commit comments

Comments
 (0)