typescript-fetch: only mark RequestOpts as async when auth methods are present#23180
Closed
guizmaii wants to merge 2 commits intoOpenAPITools:masterfrom
Closed
Conversation
The typescript-fetch generator unconditionally marks all RequestOpts functions as async, even when the endpoint has no auth methods and the function body is entirely synchronous. Only emit async/Promise when the operation has auth methods that require await calls (bearer tokens, API keys, OAuth).
The Raw method's call to RequestOpts should not use await when the function is synchronous (no auth methods).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
typescript-fetchgenerator unconditionally marks allRequestOptsfunctions asasyncand returnsPromise<runtime.RequestOpts>, even when the endpoint has no authentication methods and the function body is entirely synchronous.Generated (current):
The function body only contains
awaitexpressions inside{{#authMethods}}template blocks (bearer tokens, API keys, OAuth). When no auth is configured, there is nothing to await, yet the function is still async and wraps the return value in an unnecessary Promise.Solution
Conditionally emit
asyncandPromise<>on theRequestOptsfunction signature based on{{#hasAuthMethods}}:async listPrivateItemsRequestOpts(): Promise<runtime.RequestOpts>listPublicItemsRequestOpts(): runtime.RequestOptsThis applies to both the interface declaration and the implementation in
apis.mustache.Generated (fixed):
The calling
Rawmethod already usesawait this.{{nickname}}RequestOpts(...), which works correctly with both sync (returns value directly) and async (awaits the Promise) return types.Test plan
testRequestOptsIsAsyncOnlyWithAuthMethodstest with a new spec (api-with-and-without-auth.yaml) that has one endpoint without auth and one with API key authSummary by cubic
Make the
typescript-fetchgenerator emit synchronous*RequestOptsfor operations without auth, and keep themasynconly when auth methods are present.Rawmethods now skipawaitwhen the corresponding*RequestOptsis synchronous, removing unnecessary Promises and awaits.Bug Fixes
async/Promisefor*RequestOptsbased onhasAuthMethodsinapis.mustache(interface + class).awaitatRawcall sites only when*RequestOptsis async.Migration
*RequestOptsdirectly, removeawaitfor endpoints without auth. Callers ofRawmethods do not need changes.Written for commit 8a37c9c. Summary will update on new commits.