openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open rmustard opened this issue 4 months ago • 2 comments

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.

rmustard avatar Oct 18 '24 01:10 rmustard