You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
APIs that have a parameter value of url or path generate the error Cannot convert value of type 'URL' to expected argument type 'String?' when generating a swift-combine client. This is due to duplicate local variables with the name of url and path
/// - GET /path
/// - parameter url: (query) (optional)
/// - returns: AnyPublisher<Response, Error>
open func serviceMethod(url: String? = nil, path: String? = nil) -> AnyPublisher<Response, Error> {
Deferred {
Result<URLRequest, Error> {
guard let baseURL = self.transport.baseURL ?? self.baseURL else {
throw OpenAPITransportError.badURLError()
}
let path = "/path"
let url = baseURL.appendingPathComponent(path)
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)
var queryItems: [URLQueryItem] = []
if let url = url { queryItems.append(URLQueryItem(name: "url", value: url)) } // Error is here, duplicate local variable
components?.queryItems = queryItems
guard let requestURL = components?.url else {
throw OpenAPITransportError.badURLError()
}
var request = URLRequest(url: requestURL)
request.httpMethod = "GET"
return request
}.publisher
}.flatMap { request -> AnyPublisher<Response, Error> in
return self.transport.send(request: request)
.tryMap { response in
try self.decoder.decode(Response.self, from: response.data)
}
.eraseToAnyPublisher()
}.eraseToAnyPublisher()
}
Steps to reproduce
Create a request with url or path as a parameter
Generate a swift-combine client
Use that client in an XCode project
Related issues/PRs
None found
Suggest a fix
So I have two proposals for a fix, either will work for me, and I can create the PR myself
Rename the variable here to localVarURL. This seems to have some precedent in other clients to prefix localVar on local variables to reduce the likelihood of a collision. This however does introduce the possibility of regressing this for someone who has a parameter named localVarURL or localVarPath
- let path = "/path"- let url = baseURL.appendingPathComponent(path)- {{#hasQueryParams}}var{{/hasQueryParams}}{{^hasQueryParams}}let{{/hasQueryParams}} components = URLComponents(url: url, resolvingAgainstBaseURL: false)+ let localVarPath = "/path"+ let localVarURL = baseURL.appendingPathComponent(localVarPath)+ {{#hasQueryParams}}var{{/hasQueryParams}}{{^hasQueryParams}}let{{/hasQueryParams}} components = URLComponents(url: localVarURL, resolvingAgainstBaseURL: false)
Eliminate the local variables by inlining it into the values below
andyland
changed the title
[BUG] Combination error in swift-combine generation with parameter named url
[BUG] Combination error in swift-combine generation with query param named url
Jun 16, 2024
andyland
changed the title
[BUG] Combination error in swift-combine generation with query param named url
[BUG] Compilation error in swift-combine generation with query param named url
Jun 16, 2024
andyland
changed the title
[BUG] Compilation error in swift-combine generation with query param named url
[BUG] Compilation error in swift-combine generation with query param named url or path
Jun 16, 2024
Bug Report Checklist
Description
APIs that have a parameter value of
url
orpath
generate the errorCannot convert value of type 'URL' to expected argument type 'String?'
when generating aswift-combine
client. This is due to duplicate local variables with the name ofurl
andpath
openapi-generator version
7.6.0
OpenAPI declaration file content or url
Generation Details
Steps to reproduce
url
orpath
as a parameterswift-combine
clientRelated issues/PRs
None found
Suggest a fix
So I have two proposals for a fix, either will work for me, and I can create the PR myself
localVarURL
. This seems to have some precedent in other clients to prefixlocalVar
on local variables to reduce the likelihood of a collision. This however does introduce the possibility of regressing this for someone who has a parameter namedlocalVarURL
orlocalVarPath
The text was updated successfully, but these errors were encountered: