Skip to content

Commit 3efbe91

Browse files
committed
Create self contained patch with URLEncoding extension
1 parent f1cfe4a commit 3efbe91

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

boat-scaffold/src/main/java/org/openapitools/codegen/languages/BoatSwift5Codegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public void processOpts() {
6868
super.processOpts();
6969
additionalProperties.put("useDBSDataProvider", getLibrary().equals(LIBRARY_DBS));
7070
supportingFiles.add(new SupportingFile("AnyCodable.swift.mustache", sourceFolder, "AnyCodable.swift"));
71+
supportingFiles.add(new SupportingFile("URLEncoding+ExplodedParameters.swift.mustache", sourceFolder, "URLEncoding+ExplodedParameters.swift"));
7172

7273
if (additionalProperties.containsKey(DEPENDENCY_MANAGEMENT)) {
7374
Object dependenciesAsObject = additionalProperties.get(DEPENDENCY_MANAGEMENT);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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&param=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+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"{{baseName}}": {{#isFreeFormObject}}(params.{{paramName}} as{{^required}}?{{/required}} String){{^required}}?{{/required}}.encodeToJSON(){{/isFreeFormObject}}{{^isFreeFormObject}}params.{{paramName}}{{^required}}?{{/required}}.{{#isDateTime}}encodeToJSONDateTime{{/isDateTime}}{{^isDateTime}}encodeToJSON{{/isDateTime}}(){{/isFreeFormObject}}
1+
"{{baseName}}": {{#isContainer}}params.{{paramName}}.{{#isExplode}}exploded{{/isExplode}}{{^isExplode}}commaSeparated{{/isExplode}}{{/isContainer}}{{^isContainer}}{{#isFreeFormObject}}(params.{{paramName}} as{{^required}}?{{/required}} String){{^required}}?{{/required}}.encodeToJSON(){{/isFreeFormObject}}{{^isFreeFormObject}}{{#isEnum}}params.{{paramName}}{{^required}}?{{/required}}.rawValue{{/isEnum}}{{^isEnum}}params.{{paramName}}{{^required}}?{{/required}}.{{#isDateTime}}encodeToJSONDateTime{{/isDateTime}}{{^isDateTime}}encodeToJSON{{/isDateTime}}(){{/isEnum}}{{/isFreeFormObject}}{{/isContainer}}

0 commit comments

Comments
 (0)