OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.68k stars 6.55k forks source link

[BUG] [swift5] Alamofire 5.10 introduces compile error with changed type of ParameterEncoding #19902

Open rmustard opened 1 week ago

rmustard commented 1 week ago
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)

  1. In modules/openapi-generator/src/main/resources/swift5/libraries/alamofire/AlamofireImplementations.mustache just specify the desired type as [String: Any]? in the function call.

  2. In modules/openapi-generator/src/main/resources/swift5/JSONDataEncoding.mustache change the type of parameters: argument to conditionally be of type Parameters if alamofire library is selected

  3. If alamofire library is selected Make JSONDataEncoding conform to ParameterEncoding protocol, which would require the Parameters type.

  4. Limit Alamofire package dependency to version 5.9.1 at most.

4brunu commented 6 days ago

Hi, I think I prefer option 1, because the JSONDataEncoding keeps being simple. Could you please open a PR and tag me there? Thanks 👍

rmustard commented 6 days ago

Yeah I will. First time submitting to github, so need to figure out access keys.