Skip to content

Commit 93baade

Browse files
committed
Avoid unnecessary async on RequestOpts when no auth is configured
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).
1 parent cca5dda commit 93baade

39 files changed

Lines changed: 257 additions & 185 deletions

File tree

modules/openapi-generator/src/main/resources/typescript-fetch/apis.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export interface {{classname}}Interface {
5353
* @throws {RequiredError}
5454
* @memberof {{classname}}Interface
5555
*/
56-
{{nickname}}RequestOpts({{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request{{/allParams.0}}): Promise<runtime.RequestOpts>;
56+
{{nickname}}RequestOpts({{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request{{/allParams.0}}): {{#hasAuthMethods}}Promise<{{/hasAuthMethods}}runtime.RequestOpts{{#hasAuthMethods}}>{{/hasAuthMethods}};
5757

5858
/**
5959
* {{&notes}}
@@ -113,7 +113,7 @@ export class {{classname}} extends runtime.BaseAPI {
113113
* @deprecated
114114
{{/isDeprecated}}
115115
*/
116-
async {{nickname}}RequestOpts({{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request{{/allParams.0}}): Promise<runtime.RequestOpts> {
116+
{{#hasAuthMethods}}async {{/hasAuthMethods}}{{nickname}}RequestOpts({{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request{{/allParams.0}}): {{#hasAuthMethods}}Promise<{{/hasAuthMethods}}runtime.RequestOpts{{#hasAuthMethods}}>{{/hasAuthMethods}} {
117117
{{#allParams}}
118118
{{#required}}
119119
if (requestParameters['{{paramName}}'] == null) {

modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,28 @@ public void testValidationAttributesWithWithoutRuntimeChecks() throws IOExceptio
458458
TestUtils.assertFileContains(modelsIndex, "[property: string]:");
459459
}
460460

461+
/**
462+
* Endpoints without auth methods should generate synchronous RequestOpts functions,
463+
* while endpoints with auth methods should generate async RequestOpts functions.
464+
*/
465+
@Test(description = "Verify RequestOpts is only async when auth methods are present")
466+
public void testRequestOptsIsAsyncOnlyWithAuthMethods() throws IOException {
467+
File output = generate(
468+
Collections.emptyMap(),
469+
"src/test/resources/3_0/typescript-fetch/api-with-and-without-auth.yaml"
470+
);
471+
472+
Path apiFile = Paths.get(output + "/apis/DefaultApi.ts");
473+
TestUtils.assertFileExists(apiFile);
474+
475+
// Endpoint without auth: synchronous signature
476+
TestUtils.assertFileContains(apiFile, "listPublicItemsRequestOpts(): runtime.RequestOpts {");
477+
TestUtils.assertFileNotContains(apiFile, "listPublicItemsRequestOpts(): Promise<runtime.RequestOpts>");
478+
479+
// Endpoint with auth: async signature
480+
TestUtils.assertFileContains(apiFile, "async listPrivateItemsRequestOpts(): Promise<runtime.RequestOpts> {");
481+
}
482+
461483
private static File generate(
462484
Map<String, Object> properties
463485
) throws IOException {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
openapi: 3.0.3
2+
info:
3+
title: API with mixed auth
4+
version: 1.0.0
5+
paths:
6+
/public/items:
7+
get:
8+
operationId: listPublicItems
9+
summary: List public items (no auth)
10+
responses:
11+
'200':
12+
description: OK
13+
content:
14+
application/json:
15+
schema:
16+
type: array
17+
items:
18+
$ref: '#/components/schemas/Item'
19+
/private/items:
20+
get:
21+
operationId: listPrivateItems
22+
summary: List private items (requires API key)
23+
security:
24+
- ApiKeyAuth: []
25+
responses:
26+
'200':
27+
description: OK
28+
content:
29+
application/json:
30+
schema:
31+
type: array
32+
items:
33+
$ref: '#/components/schemas/Item'
34+
components:
35+
securitySchemes:
36+
ApiKeyAuth:
37+
type: apiKey
38+
in: header
39+
name: X-API-KEY
40+
schemas:
41+
Item:
42+
type: object
43+
required:
44+
- id
45+
- name
46+
properties:
47+
id:
48+
type: string
49+
name:
50+
type: string

samples/client/petstore/typescript-fetch/builds/allOf-nullable/apis/DefaultApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class DefaultApi extends runtime.BaseAPI {
3434
/**
3535
* Creates request options for list without sending the request
3636
*/
37-
async listRequestOpts(requestParameters: ListRequest): Promise<runtime.RequestOpts> {
37+
listRequestOpts(requestParameters: ListRequest): runtime.RequestOpts {
3838
if (requestParameters['personId'] == null) {
3939
throw new runtime.RequiredError(
4040
'personId',

samples/client/petstore/typescript-fetch/builds/allOf-readonly/apis/DefaultApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class DefaultApi extends runtime.BaseAPI {
3434
/**
3535
* Creates request options for list without sending the request
3636
*/
37-
async listRequestOpts(requestParameters: ListRequest): Promise<runtime.RequestOpts> {
37+
listRequestOpts(requestParameters: ListRequest): runtime.RequestOpts {
3838
if (requestParameters['personId'] == null) {
3939
throw new runtime.RequiredError(
4040
'personId',

samples/client/petstore/typescript-fetch/builds/default-v3.0/apis/AnotherFakeApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class AnotherFakeApi extends runtime.BaseAPI {
3434
/**
3535
* Creates request options for _123testSpecialTags without sending the request
3636
*/
37-
async _123testSpecialTagsRequestOpts(requestParameters: 123testSpecialTagsRequest): Promise<runtime.RequestOpts> {
37+
_123testSpecialTagsRequestOpts(requestParameters: 123testSpecialTagsRequest): runtime.RequestOpts {
3838
if (requestParameters['client'] == null) {
3939
throw new runtime.RequiredError(
4040
'client',

samples/client/petstore/typescript-fetch/builds/default-v3.0/apis/DefaultApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class DefaultApi extends runtime.BaseAPI {
3030
/**
3131
* Creates request options for fooGet without sending the request
3232
*/
33-
async fooGetRequestOpts(): Promise<runtime.RequestOpts> {
33+
fooGetRequestOpts(): runtime.RequestOpts {
3434
const queryParameters: any = {};
3535

3636
const headerParameters: runtime.HTTPHeaders = {};

samples/client/petstore/typescript-fetch/builds/default-v3.0/apis/FakeApi.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export class FakeApi extends runtime.BaseAPI {
176176
/**
177177
* Creates request options for fakeBigDecimalMap without sending the request
178178
*/
179-
async fakeBigDecimalMapRequestOpts(): Promise<runtime.RequestOpts> {
179+
fakeBigDecimalMapRequestOpts(): runtime.RequestOpts {
180180
const queryParameters: any = {};
181181

182182
const headerParameters: runtime.HTTPHeaders = {};
@@ -213,7 +213,7 @@ export class FakeApi extends runtime.BaseAPI {
213213
/**
214214
* Creates request options for fakeHealthGet without sending the request
215215
*/
216-
async fakeHealthGetRequestOpts(): Promise<runtime.RequestOpts> {
216+
fakeHealthGetRequestOpts(): runtime.RequestOpts {
217217
const queryParameters: any = {};
218218

219219
const headerParameters: runtime.HTTPHeaders = {};
@@ -304,7 +304,7 @@ export class FakeApi extends runtime.BaseAPI {
304304
/**
305305
* Creates request options for fakeOuterBooleanSerialize without sending the request
306306
*/
307-
async fakeOuterBooleanSerializeRequestOpts(requestParameters: FakeOuterBooleanSerializeRequest): Promise<runtime.RequestOpts> {
307+
fakeOuterBooleanSerializeRequestOpts(requestParameters: FakeOuterBooleanSerializeRequest): runtime.RequestOpts {
308308
const queryParameters: any = {};
309309

310310
const headerParameters: runtime.HTTPHeaders = {};
@@ -348,7 +348,7 @@ export class FakeApi extends runtime.BaseAPI {
348348
/**
349349
* Creates request options for fakeOuterCompositeSerialize without sending the request
350350
*/
351-
async fakeOuterCompositeSerializeRequestOpts(requestParameters: FakeOuterCompositeSerializeRequest): Promise<runtime.RequestOpts> {
351+
fakeOuterCompositeSerializeRequestOpts(requestParameters: FakeOuterCompositeSerializeRequest): runtime.RequestOpts {
352352
const queryParameters: any = {};
353353

354354
const headerParameters: runtime.HTTPHeaders = {};
@@ -388,7 +388,7 @@ export class FakeApi extends runtime.BaseAPI {
388388
/**
389389
* Creates request options for fakeOuterNumberSerialize without sending the request
390390
*/
391-
async fakeOuterNumberSerializeRequestOpts(requestParameters: FakeOuterNumberSerializeRequest): Promise<runtime.RequestOpts> {
391+
fakeOuterNumberSerializeRequestOpts(requestParameters: FakeOuterNumberSerializeRequest): runtime.RequestOpts {
392392
const queryParameters: any = {};
393393

394394
const headerParameters: runtime.HTTPHeaders = {};
@@ -432,7 +432,7 @@ export class FakeApi extends runtime.BaseAPI {
432432
/**
433433
* Creates request options for fakeOuterStringSerialize without sending the request
434434
*/
435-
async fakeOuterStringSerializeRequestOpts(requestParameters: FakeOuterStringSerializeRequest): Promise<runtime.RequestOpts> {
435+
fakeOuterStringSerializeRequestOpts(requestParameters: FakeOuterStringSerializeRequest): runtime.RequestOpts {
436436
const queryParameters: any = {};
437437

438438
const headerParameters: runtime.HTTPHeaders = {};
@@ -476,7 +476,7 @@ export class FakeApi extends runtime.BaseAPI {
476476
/**
477477
* Creates request options for fakePropertyEnumIntegerSerialize without sending the request
478478
*/
479-
async fakePropertyEnumIntegerSerializeRequestOpts(requestParameters: FakePropertyEnumIntegerSerializeRequest): Promise<runtime.RequestOpts> {
479+
fakePropertyEnumIntegerSerializeRequestOpts(requestParameters: FakePropertyEnumIntegerSerializeRequest): runtime.RequestOpts {
480480
if (requestParameters['outerObjectWithEnumProperty'] == null) {
481481
throw new runtime.RequiredError(
482482
'outerObjectWithEnumProperty',
@@ -523,7 +523,7 @@ export class FakeApi extends runtime.BaseAPI {
523523
/**
524524
* Creates request options for testAdditionalPropertiesReference without sending the request
525525
*/
526-
async testAdditionalPropertiesReferenceRequestOpts(requestParameters: TestAdditionalPropertiesReferenceRequest): Promise<runtime.RequestOpts> {
526+
testAdditionalPropertiesReferenceRequestOpts(requestParameters: TestAdditionalPropertiesReferenceRequest): runtime.RequestOpts {
527527
if (requestParameters['requestBody'] == null) {
528528
throw new runtime.RequiredError(
529529
'requestBody',
@@ -571,7 +571,7 @@ export class FakeApi extends runtime.BaseAPI {
571571
/**
572572
* Creates request options for testBodyWithBinary without sending the request
573573
*/
574-
async testBodyWithBinaryRequestOpts(requestParameters: TestBodyWithBinaryRequest): Promise<runtime.RequestOpts> {
574+
testBodyWithBinaryRequestOpts(requestParameters: TestBodyWithBinaryRequest): runtime.RequestOpts {
575575
if (requestParameters['body'] == null) {
576576
throw new runtime.RequiredError(
577577
'body',
@@ -617,7 +617,7 @@ export class FakeApi extends runtime.BaseAPI {
617617
/**
618618
* Creates request options for testBodyWithFileSchema without sending the request
619619
*/
620-
async testBodyWithFileSchemaRequestOpts(requestParameters: TestBodyWithFileSchemaRequest): Promise<runtime.RequestOpts> {
620+
testBodyWithFileSchemaRequestOpts(requestParameters: TestBodyWithFileSchemaRequest): runtime.RequestOpts {
621621
if (requestParameters['fileSchemaTestClass'] == null) {
622622
throw new runtime.RequiredError(
623623
'fileSchemaTestClass',
@@ -663,7 +663,7 @@ export class FakeApi extends runtime.BaseAPI {
663663
/**
664664
* Creates request options for testBodyWithQueryParams without sending the request
665665
*/
666-
async testBodyWithQueryParamsRequestOpts(requestParameters: TestBodyWithQueryParamsRequest): Promise<runtime.RequestOpts> {
666+
testBodyWithQueryParamsRequestOpts(requestParameters: TestBodyWithQueryParamsRequest): runtime.RequestOpts {
667667
if (requestParameters['query'] == null) {
668668
throw new runtime.RequiredError(
669669
'query',
@@ -718,7 +718,7 @@ export class FakeApi extends runtime.BaseAPI {
718718
/**
719719
* Creates request options for testClientModel without sending the request
720720
*/
721-
async testClientModelRequestOpts(requestParameters: TestClientModelRequest): Promise<runtime.RequestOpts> {
721+
testClientModelRequestOpts(requestParameters: TestClientModelRequest): runtime.RequestOpts {
722722
if (requestParameters['client'] == null) {
723723
throw new runtime.RequiredError(
724724
'client',
@@ -909,7 +909,7 @@ export class FakeApi extends runtime.BaseAPI {
909909
/**
910910
* Creates request options for testEnumParameters without sending the request
911911
*/
912-
async testEnumParametersRequestOpts(requestParameters: TestEnumParametersRequest): Promise<runtime.RequestOpts> {
912+
testEnumParametersRequestOpts(requestParameters: TestEnumParametersRequest): runtime.RequestOpts {
913913
const queryParameters: any = {};
914914

915915
if (requestParameters['enumQueryStringArray'] != null) {
@@ -1089,7 +1089,7 @@ export class FakeApi extends runtime.BaseAPI {
10891089
/**
10901090
* Creates request options for testInlineAdditionalProperties without sending the request
10911091
*/
1092-
async testInlineAdditionalPropertiesRequestOpts(requestParameters: TestInlineAdditionalPropertiesRequest): Promise<runtime.RequestOpts> {
1092+
testInlineAdditionalPropertiesRequestOpts(requestParameters: TestInlineAdditionalPropertiesRequest): runtime.RequestOpts {
10931093
if (requestParameters['requestBody'] == null) {
10941094
throw new runtime.RequiredError(
10951095
'requestBody',
@@ -1137,7 +1137,7 @@ export class FakeApi extends runtime.BaseAPI {
11371137
/**
11381138
* Creates request options for testInlineFreeformAdditionalProperties without sending the request
11391139
*/
1140-
async testInlineFreeformAdditionalPropertiesRequestOpts(requestParameters: TestInlineFreeformAdditionalPropertiesOperationRequest): Promise<runtime.RequestOpts> {
1140+
testInlineFreeformAdditionalPropertiesRequestOpts(requestParameters: TestInlineFreeformAdditionalPropertiesOperationRequest): runtime.RequestOpts {
11411141
if (requestParameters['testInlineFreeformAdditionalPropertiesRequest'] == null) {
11421142
throw new runtime.RequiredError(
11431143
'testInlineFreeformAdditionalPropertiesRequest',
@@ -1185,7 +1185,7 @@ export class FakeApi extends runtime.BaseAPI {
11851185
/**
11861186
* Creates request options for testJsonFormData without sending the request
11871187
*/
1188-
async testJsonFormDataRequestOpts(requestParameters: TestJsonFormDataRequest): Promise<runtime.RequestOpts> {
1188+
testJsonFormDataRequestOpts(requestParameters: TestJsonFormDataRequest): runtime.RequestOpts {
11891189
if (requestParameters['param'] == null) {
11901190
throw new runtime.RequiredError(
11911191
'param',
@@ -1260,7 +1260,7 @@ export class FakeApi extends runtime.BaseAPI {
12601260
/**
12611261
* Creates request options for testNullable without sending the request
12621262
*/
1263-
async testNullableRequestOpts(requestParameters: TestNullableRequest): Promise<runtime.RequestOpts> {
1263+
testNullableRequestOpts(requestParameters: TestNullableRequest): runtime.RequestOpts {
12641264
if (requestParameters['childWithNullable'] == null) {
12651265
throw new runtime.RequiredError(
12661266
'childWithNullable',
@@ -1308,7 +1308,7 @@ export class FakeApi extends runtime.BaseAPI {
13081308
/**
13091309
* Creates request options for testQueryParameterCollectionFormat without sending the request
13101310
*/
1311-
async testQueryParameterCollectionFormatRequestOpts(requestParameters: TestQueryParameterCollectionFormatRequest): Promise<runtime.RequestOpts> {
1311+
testQueryParameterCollectionFormatRequestOpts(requestParameters: TestQueryParameterCollectionFormatRequest): runtime.RequestOpts {
13121312
if (requestParameters['pipe'] == null) {
13131313
throw new runtime.RequiredError(
13141314
'pipe',
@@ -1416,7 +1416,7 @@ export class FakeApi extends runtime.BaseAPI {
14161416
/**
14171417
* Creates request options for testStringMapReference without sending the request
14181418
*/
1419-
async testStringMapReferenceRequestOpts(requestParameters: TestStringMapReferenceRequest): Promise<runtime.RequestOpts> {
1419+
testStringMapReferenceRequestOpts(requestParameters: TestStringMapReferenceRequest): runtime.RequestOpts {
14201420
if (requestParameters['requestBody'] == null) {
14211421
throw new runtime.RequiredError(
14221422
'requestBody',

samples/client/petstore/typescript-fetch/builds/default-v3.0/apis/StoreApi.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class StoreApi extends runtime.BaseAPI {
4242
/**
4343
* Creates request options for deleteOrder without sending the request
4444
*/
45-
async deleteOrderRequestOpts(requestParameters: DeleteOrderRequest): Promise<runtime.RequestOpts> {
45+
deleteOrderRequestOpts(requestParameters: DeleteOrderRequest): runtime.RequestOpts {
4646
if (requestParameters['orderId'] == null) {
4747
throw new runtime.RequiredError(
4848
'orderId',
@@ -131,7 +131,7 @@ export class StoreApi extends runtime.BaseAPI {
131131
/**
132132
* Creates request options for getOrderById without sending the request
133133
*/
134-
async getOrderByIdRequestOpts(requestParameters: GetOrderByIdRequest): Promise<runtime.RequestOpts> {
134+
getOrderByIdRequestOpts(requestParameters: GetOrderByIdRequest): runtime.RequestOpts {
135135
if (requestParameters['orderId'] == null) {
136136
throw new runtime.RequiredError(
137137
'orderId',
@@ -178,7 +178,7 @@ export class StoreApi extends runtime.BaseAPI {
178178
/**
179179
* Creates request options for placeOrder without sending the request
180180
*/
181-
async placeOrderRequestOpts(requestParameters: PlaceOrderRequest): Promise<runtime.RequestOpts> {
181+
placeOrderRequestOpts(requestParameters: PlaceOrderRequest): runtime.RequestOpts {
182182
if (requestParameters['order'] == null) {
183183
throw new runtime.RequiredError(
184184
'order',

0 commit comments

Comments
 (0)