|
| 1 | +// |
| 2 | +// URLEncoding+ExplodedParameters.swift |
| 3 | +// client-common-ios |
| 4 | +// |
| 5 | +// Created by Mirko Tomic on 16. 6. 2025.. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +/// ArrayParam wrapper for controlling OpenAPI array parameter encoding |
| 11 | +public struct ArrayParam { |
| 12 | + public init(_ value: [Any?], exploded: Bool) { |
| 13 | + self.value = value |
| 14 | + self.explode = exploded |
| 15 | + } |
| 16 | + |
| 17 | + public let value: [Any?] |
| 18 | + public let explode: Bool |
| 19 | + |
| 20 | + /// Returns URLQueryItems based on explode setting |
| 21 | + func queryItems(for key: String) -> [URLQueryItem] { |
| 22 | + if explode { |
| 23 | + return value.compactMap { $0 }.map { URLQueryItem(name: key, value: "\($0)") } |
| 24 | + } else { |
| 25 | + let joined = value.compactMap { $0 }.map { "\($0)" }.joined(separator: ",") |
| 26 | + return [URLQueryItem(name: key, value: joined)] |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +public extension URLEncoding { |
| 32 | + /// Creates a URL request by encoding parameters with exploded array support |
| 33 | + /// and applying them onto an existing request url. |
| 34 | + /// |
| 35 | + /// This method supports the ArrayParam type for controlling exploded vs comma-separated arrays |
| 36 | + /// according to OpenAPI specification. |
| 37 | + /// |
| 38 | + /// - parameter urlRequest: The request to have parameters applied. |
| 39 | + /// - parameter parameters: The parameters to apply. Can include ArrayParam instances. |
| 40 | + /// |
| 41 | + /// - throws: An `Error` if the encoding process encounters an error. |
| 42 | + /// |
| 43 | + /// - returns: The encoded request. |
| 44 | + static func encodeWithExplodedSupport(_ urlRequest: URLRequest, with parameters: [String: Any]?) throws -> URLRequest { |
| 45 | + var urlRequest = urlRequest |
| 46 | + guard let parameters = parameters else { return urlRequest } |
| 47 | + |
| 48 | + guard let url = urlRequest.url else { |
| 49 | + throw CallError.requestMissingUrl |
| 50 | + } |
| 51 | + |
| 52 | + if var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false), !parameters.isEmpty { |
| 53 | + urlComponents.queryItems = mapValuesToQueryItemsWithExplodedSupport(parameters) |
| 54 | + urlRequest.url = urlComponents.url |
| 55 | + } |
| 56 | + |
| 57 | + return urlRequest |
| 58 | + } |
| 59 | + |
| 60 | + private static func mapValuesToQueryItemsWithExplodedSupport(_ source: [String: Any?]) -> [URLQueryItem]? { |
| 61 | + let items = source.compactMap { key, value -> [URLQueryItem]? in |
| 62 | + guard let value else { |
| 63 | + return nil |
| 64 | + } |
| 65 | + |
| 66 | + switch value { |
| 67 | + case let array as [Any?]: |
| 68 | + return ArrayParam(array, exploded: true) |
| 69 | + .queryItems(for: key) |
| 70 | +
|
| 71 | + case let arrayParam as ArrayParam: |
| 72 | + return arrayParam |
| 73 | + .queryItems(for: key) |
| 74 | +
|
| 75 | + default: |
| 76 | + return [URLQueryItem(name: key, value: "\(value)")] |
| 77 | + } |
| 78 | + }.flatMap { $0 } |
| 79 | + |
| 80 | + return items.isEmpty ? nil : items |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// Helper extensions for easier ArrayParam creation |
| 85 | +public extension Array { |
| 86 | + /// Creates an ArrayParam with exploded=true for OpenAPI query parameter encoding |
| 87 | + /// Example: ["a", "b"].exploded -> ?param=a¶m=b |
| 88 | + var exploded: ArrayParam { |
| 89 | + ArrayParam(self, exploded: true) |
| 90 | + } |
| 91 | + |
| 92 | + /// Creates an ArrayParam with exploded=false for OpenAPI query parameter encoding |
| 93 | + /// Example: ["a", "b"].commaSeparated -> ?param=a,b |
| 94 | + var commaSeparated: ArrayParam { |
| 95 | + ArrayParam(self, exploded: false) |
| 96 | + } |
| 97 | +} |
0 commit comments