Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rmustard opened this issue Oct 18, 2024 · 2 comments

Comments

@rmustard
Copy link
Contributor

rmustard commented Oct 18, 2024

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
Copy link
Contributor

4brunu commented Oct 18, 2024

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
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants