Skip to content

How to program the refresh token logic when using Swift generated code? #5462

@Ricardo1980

Description

@Ricardo1980
Description

Hello,

I'm using openapi-generator, version 4.2.3, exactly this one:
openapi-generator generate -g swift5 -i my_openapi.yaml -o ./swift5 --library alamofire

The reason I want to use Alamofire is because my api (created by a workmate), uses access token and refresh token, and the access token expires every hour so the refresh token has to be used to get a new one, while the ongoing requests are paused until I get a new valid access token. Basically, if I get 401 in any request, I try to download the access token using the refresh token. If unauthorized, I logout the user, otherwise, I continue with the original request. That logic is very easy to implement using alamofire.

Without this generated code, I do something like:

awsManager = AFManager.getManagerWith(UtilityMethods.setupConfigurationWithTimeout())
let jwtHandler = JWTHandler()
awsManager.adapter = jwtHandler
awsManager.retrier = jwtHandler

In the adapter, I add the access token for all requests, except signup request.
In retrier, I add the refresh token logic.

I already did that in other app using Alamofire but a custom networking layer, not code autogenerated.

https://github.com/Alamofire/Alamofire/blob/master/Documentation/AdvancedUsage.md#requestadapter
https://github.com/Alamofire/Alamofire/blob/master/Documentation/AdvancedUsage.md#requestretrier
for the retrier I used code similar to this one, and working perfectly
https://stackoverflow.com/questions/42079180/understanding-alamofire-oauth-example

Researching the generated code, I see:

    /**
     May be overridden by a subclass if you want to control the session
     configuration.
     */
    open func createSessionManager() -> Alamofire.SessionManager

I guess I have to subclass AlamofireRequestBuilder and overwrite createSessionManager.
Perhaps I also have to subclass AlamofireRequestBuilderFactory and then pass this new instance to OpenAPIClientAPI.requestBuilderFactory

So, the question is, how can I attach the refresh token logic I explained to the generated code without modifying it? Is that possible? Any other suggestion?
How do you usually manage the refresh token logic when using autogenerated code?

Thanks for suggestions.

EDIT:
I can do something like this and inject it using
OpenAPIClientAPI.requestBuilderFactory = CustomAlamofireRequestBuilderFactory()

class CustomAlamofireRequestBuilderFactory: RequestBuilderFactory {
    func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
        return CustomAlamofireRequestBuilder<T>.self
    }

    func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type {
        return AlamofireDecodableRequestBuilder<T>.self
    }
}

But AlamofireDecodableRequestBuilder is a subclass of AlamofireRequestBuilder and I cannot change that. I tried to copy/paste the entire AlamofireDecodableRequestBuilder class (creating CustomAlamofireDecodableRequestBuilder), changing the base class from AlamofireRequestBuilder to CustomAlamofireRequestBuilder, but it does not work because "credential" and "managerStore" are innacesible.

However, if I edit manually the autogenerated code and I use something like:

    open func createSessionManager() -> Alamofire.SessionManager {
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = buildHeaders()

        let sessionManager = Alamofire.SessionManager(configuration: configuration)
        let jwtHandler = JWTHandler()
        sessionManager.adapter = jwtHandler
        sessionManager.retrier = jwtHandler
        return sessionManager
    }

It works. But this is a crappy solution. It means I have to edit the generated code all the time is generated.

Any idea? How should I fix this?

openapi-generator version

4.2.3

OpenAPI declaration file content or url

No issues with this, the code generated works perfectly.

Command line used for generation

openapi-generator generate -g swift5 -i my_openapi.yaml -o ./swift5 --library alamofire

Related issues/PRs

I see several of them but regarding other languages, nothing about swift.

Suggest a fix/enhancement

Explained in the description.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions