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][Swift6] OpenAPIInterceptor shoud retry 401 errors even if thre is no let response urlSession #19939

Closed
5 of 6 tasks
biovolt opened this issue Oct 22, 2024 · 1 comment · Fixed by #19988
Closed
5 of 6 tasks

Comments

@biovolt
Copy link

biovolt commented Oct 22, 2024

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When you call a service with Bearer auth like JWT and the token was revoked or something made the token no longer valid, e.g., a user password change, the service can return a 401, and no Body / no error with the response. This scenario will not call the retry because "if let response, let error" will fail. The reason is that error is nil and skip over the interceptor.retry call.

                case let .success(modifiedRequest):
                    let dataTask = urlSession.dataTaskFromProtocol(with: modifiedRequest) { data, response, error in
                        self.cleanupRequest()
                        if let response, let error {
                            self.openAPIClient.interceptor.retry(urlRequest: modifiedRequest, urlSession: urlSession, requestBuilder: self, data: data, response: response, error: error) { retry in
                                switch retry {
                                case .retry:
                                    self.execute(completion: completion)

                                case .dontRetry:
                                    self.openAPIClient.apiResponseQueue.async {
                                        self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
                                    }
                                }
                            }
                        } else {
                            self.openAPIClient.apiResponseQueue.async {
                                self.processRequestResponse(urlRequest: request, data: data, response: response, error: error, completion: completion)
                            }
                        }
                    }
openapi-generator version

7.10.0-SNAPSHOT
Commit SHA 266193f

OpenAPI declaration file content or url

I am not allowed to share this: Sorry
-->

Generation Details

java -jar openapi-generator-cli.jar generate -i swagger.json -g swift6 -o . -p projectName=******OpenApiClient -p podVersion=$3
-p sortModelPropertiesByRequiredFlag=true
-p hashableModels=false
-p removeMigrationProjectNameClass=true
-p enumUnknownDefaultCase=true
-p podSource=************openapiclient.git
-p podHomepage=*************openapiclient
-p podDocumentationURL=*************openapiclient/-/blob/master/README.md
-p podSwiftVersion=6.0

Steps to reproduce

Generate the openapi code and add it to a project authenticate with Bearer auth like JWT. Once the app has a valid JWT invalidate the token server side. so that the server returns a 401 with no error / no body and it won't call the interceptor.retry

The raw response from the service is with no Body

HTTP/1.1 401 Unauthorized
Date: Tue, 22 Oct 2024 06:35:04 GMT
Transfer-Encoding: chunked
WWW-Authenticate: Basic
Request-Context: appId=cid-v1:5461468-2fa1-4e29-ad9b-4178841351987
Strict-Transport-Security: max-age=2592000
X-Platform: Core
Content-Security-Policy: object-src none
Content-Security-Policy: frame-ancestors 'self'
X-Frame-Options: SAMEORIGIN
Related issues/PRs

I could not find any

Suggest a fix

Inside URLSessionImplementations.swift " if let response, let error {" remove the requirement for error to have a value.
Error is not required it is the HTTP status code that determines what action should be taken. However, I am not sure why a 401 with no body does not return an error from urlSession.dataTaskFromProtocol

@4brunu
Copy link
Contributor

4brunu commented Oct 28, 2024

Hi @biovolt, I thought a 401 status code would have an error.
Can ou please check if this PR fixes your issue?
#19988

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

Successfully merging a pull request may close this issue.

2 participants