Skip to content

typescript-fetch: only mark RequestOpts as async when auth methods are present#23180

Closed
guizmaii wants to merge 2 commits intoOpenAPITools:masterfrom
guizmaii:fix/typescript-fetch-unnecessary-async-request-opts
Closed

typescript-fetch: only mark RequestOpts as async when auth methods are present#23180
guizmaii wants to merge 2 commits intoOpenAPITools:masterfrom
guizmaii:fix/typescript-fetch-unnecessary-async-request-opts

Conversation

@guizmaii
Copy link
Contributor

@guizmaii guizmaii commented Mar 9, 2026

Problem

The typescript-fetch generator unconditionally marks all RequestOpts functions as async and returns Promise<runtime.RequestOpts>, even when the endpoint has no authentication methods and the function body is entirely synchronous.

Generated (current):

// StoreApi -- no auth configured on these endpoints
async deleteOrderRequestOpts(requestParameters: DeleteOrderRequest): Promise<runtime.RequestOpts> {
    // ... purely synchronous body: parameter validation, URL building, header assembly
    return {
        path: urlPath,
        method: 'DELETE',
        headers: headerParameters,
        query: queryParameters,
    };
}

The function body only contains await expressions 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 async and Promise<> on the RequestOpts function signature based on {{#hasAuthMethods}}:

  • With auth methods: async listPrivateItemsRequestOpts(): Promise<runtime.RequestOpts>
  • Without auth methods: listPublicItemsRequestOpts(): runtime.RequestOpts

This applies to both the interface declaration and the implementation in apis.mustache.

Generated (fixed):

// StoreApi -- no auth: synchronous signature
deleteOrderRequestOpts(requestParameters: DeleteOrderRequest): runtime.RequestOpts {
    return {
        path: urlPath,
        method: 'DELETE',
        headers: headerParameters,
        query: queryParameters,
    };
}

// PetApi -- has OAuth: async signature preserved
async addPetRequestOpts(requestParameters: AddPetRequest): Promise<runtime.RequestOpts> {
    // ... contains: headerParameters["Authorization"] = await this.configuration.accessToken(...)
    return { ... };
}

The calling Raw method already uses await this.{{nickname}}RequestOpts(...), which works correctly with both sync (returns value directly) and async (awaits the Promise) return types.

Test plan

  • Added testRequestOptsIsAsyncOnlyWithAuthMethods test with a new spec (api-with-and-without-auth.yaml) that has one endpoint without auth and one with API key auth
  • Regenerated all typescript-fetch samples
  • All 23 TypeScriptFetchClientCodegenTest tests pass

Summary by cubic

Make the typescript-fetch generator emit synchronous *RequestOpts for operations without auth, and keep them async only when auth methods are present. Raw methods now skip await when the corresponding *RequestOpts is synchronous, removing unnecessary Promises and awaits.

  • Bug Fixes

    • Conditionally emit async/Promise for *RequestOpts based on hasAuthMethods in apis.mustache (interface + class).
    • Conditionally use await at Raw call sites only when *RequestOpts is async.
    • Added test for mixed-auth operations and regenerated samples.
  • Migration

    • If you call *RequestOpts directly, remove await for endpoints without auth. Callers of Raw methods do not need changes.

Written for commit 8a37c9c. Summary will update on new commits.

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

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 39 files

@guizmaii guizmaii marked this pull request as draft March 9, 2026 04:48
The Raw method's call to RequestOpts should not use await when
the function is synchronous (no auth methods).
@guizmaii guizmaii closed this Mar 9, 2026
@guizmaii guizmaii deleted the fix/typescript-fetch-unnecessary-async-request-opts branch March 9, 2026 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant