diff --git a/modules/openapi-generator/src/main/resources/swift5/libraries/urlsession/URLSessionImplementations.mustache b/modules/openapi-generator/src/main/resources/swift5/libraries/urlsession/URLSessionImplementations.mustache index b045c18c66b5..3f60f35560ea 100644 --- a/modules/openapi-generator/src/main/resources/swift5/libraries/urlsession/URLSessionImplementations.mustache +++ b/modules/openapi-generator/src/main/resources/swift5/libraries/urlsession/URLSessionImplementations.mustache @@ -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 diff --git a/modules/openapi-generator/src/main/resources/swift6/libraries/urlsession/URLSessionImplementations.mustache b/modules/openapi-generator/src/main/resources/swift6/libraries/urlsession/URLSessionImplementations.mustache index a20f72804cd7..94fde0e7e582 100644 --- a/modules/openapi-generator/src/main/resources/swift6/libraries/urlsession/URLSessionImplementations.mustache +++ b/modules/openapi-generator/src/main/resources/swift6/libraries/urlsession/URLSessionImplementations.mustache @@ -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 diff --git a/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index 6fa4d0e20eb9..aa6558b927bb 100644 --- a/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index 6fa4d0e20eb9..aa6558b927bb 100644 --- a/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index 6fa4d0e20eb9..aa6558b927bb 100644 --- a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index 6fa4d0e20eb9..aa6558b927bb 100644 --- a/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index 6fa4d0e20eb9..aa6558b927bb 100644 --- a/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index 6fa4d0e20eb9..aa6558b927bb 100644 --- a/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index a76ea20537dd..8d45a2bbb69f 100644 --- a/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index 6fa4d0e20eb9..aa6558b927bb 100644 --- a/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/URLSessionImplementations.swift b/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/URLSessionImplementations.swift index 6fa4d0e20eb9..aa6558b927bb 100644 --- a/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift b/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift index 6fa4d0e20eb9..aa6558b927bb 100644 --- a/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift +++ b/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift6/asyncAwaitLibrary/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift b/samples/client/petstore/swift6/asyncAwaitLibrary/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift index 043a2d5d8e21..a583e24501bf 100644 --- a/samples/client/petstore/swift6/asyncAwaitLibrary/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift +++ b/samples/client/petstore/swift6/asyncAwaitLibrary/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift6/combineDeferredLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift b/samples/client/petstore/swift6/combineDeferredLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift index 043a2d5d8e21..a583e24501bf 100644 --- a/samples/client/petstore/swift6/combineDeferredLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift +++ b/samples/client/petstore/swift6/combineDeferredLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift6/combineLibrary/Sources/CombineLibrary/Infrastructure/URLSessionImplementations.swift b/samples/client/petstore/swift6/combineLibrary/Sources/CombineLibrary/Infrastructure/URLSessionImplementations.swift index 043a2d5d8e21..a583e24501bf 100644 --- a/samples/client/petstore/swift6/combineLibrary/Sources/CombineLibrary/Infrastructure/URLSessionImplementations.swift +++ b/samples/client/petstore/swift6/combineLibrary/Sources/CombineLibrary/Infrastructure/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift6/default/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift b/samples/client/petstore/swift6/default/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift index 043a2d5d8e21..a583e24501bf 100644 --- a/samples/client/petstore/swift6/default/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift +++ b/samples/client/petstore/swift6/default/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift6/objcCompatible/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift b/samples/client/petstore/swift6/objcCompatible/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift index 043a2d5d8e21..a583e24501bf 100644 --- a/samples/client/petstore/swift6/objcCompatible/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift +++ b/samples/client/petstore/swift6/objcCompatible/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift6/oneOf/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift b/samples/client/petstore/swift6/oneOf/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift index 043a2d5d8e21..a583e24501bf 100644 --- a/samples/client/petstore/swift6/oneOf/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift +++ b/samples/client/petstore/swift6/oneOf/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift6/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift b/samples/client/petstore/swift6/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift index 043a2d5d8e21..a583e24501bf 100644 --- a/samples/client/petstore/swift6/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift +++ b/samples/client/petstore/swift6/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift6/resultLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift b/samples/client/petstore/swift6/resultLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift index cf58e2a66eda..99cdd86d4468 100644 --- a/samples/client/petstore/swift6/resultLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift +++ b/samples/client/petstore/swift6/resultLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift6/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift b/samples/client/petstore/swift6/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift index 043a2d5d8e21..a583e24501bf 100644 --- a/samples/client/petstore/swift6/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift +++ b/samples/client/petstore/swift6/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift6/urlsessionLibrary/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift b/samples/client/petstore/swift6/urlsessionLibrary/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift index 043a2d5d8e21..a583e24501bf 100644 --- a/samples/client/petstore/swift6/urlsessionLibrary/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift +++ b/samples/client/petstore/swift6/urlsessionLibrary/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift @@ -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 diff --git a/samples/client/petstore/swift6/validation/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift b/samples/client/petstore/swift6/validation/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift index 043a2d5d8e21..a583e24501bf 100644 --- a/samples/client/petstore/swift6/validation/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift +++ b/samples/client/petstore/swift6/validation/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift @@ -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