openapi-generator
openapi-generator copied to clipboard
[BUG] [swift5] Alamofire 5.10 introduces compile error with changed type of ParameterEncoding
Description
The swift5 generator with Alamofire library produces a Package.swift file that allows Alamofire version 5.10 that was released October 13, 2024.
Alamofire 5.10 changed the definition of the type ParameterEncoding
so that it no longer is equivalent to [String: Any]
.
This causes the generated code to appear to be making a recursive call.
extension JSONDataEncoding: ParameterEncoding {
...
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
let urlRequest = try urlRequest.asURLRequest()
return encode(urlRequest, with: parameters)
}
}
openapi-generator version
openapi-generator-maven-plugin 7.9.0
<execution>
<id>swift-gen</id>
<configuration>
<inputSpec>
${project.basedir}/src/main/resources/openapi.yaml
</inputSpec>
<skipIfSpecIsUnchanged>true</skipIfSpecIsUnchanged>
<output>${project.basedir}/clients/swift</output>
<generatorName>swift5</generatorName>
<library>alamofire</library>
<templateDirectory>src/main/resources/templates/swift5</templateDirectory>
<configOptions>
<projectName>swift-api</projectName>
<podVersion>1.0.0</podVersion>
</configOptions>
</configuration>
</execution>
OpenAPI declaration file content or url
The content of the spec file doesn't affect this bug
Steps to reproduce
Generate using openapi-generator generate -g swift5 --library alamofire -i spec.yaml
Compile the swift code using swift build
Observe compile errors
/code/swift-api/AlamofireImplementations.swift:411:16: error: call can throw but is not marked with 'try'
409 | let urlRequest = try urlRequest.asURLRequest()
410 |
411 | return encode(urlRequest, with: parameters)
| |- error: call can throw but is not marked with 'try'
| |- note: did you mean to use 'try'?
| |- note: did you mean to handle error as optional value?
| `- note: did you mean to disable error propagation?
412 | }
413 | }
Related issues/PRs
Suggest a fix
I'll create a PR, but here are various options ( I prefer 1)
-
In
modules/openapi-generator/src/main/resources/swift5/libraries/alamofire/AlamofireImplementations.mustache
just specify the desired typeas [String: Any]?
in the function call. -
In
modules/openapi-generator/src/main/resources/swift5/JSONDataEncoding.mustache
change the type of parameters: argument to conditionally be of typeParameters
if alamofire library is selected -
If alamofire library is selected Make JSONDataEncoding conform to
ParameterEncoding
protocol, which would require theParameters
type. -
Limit Alamofire package dependency to version 5.9.1 at most.